Skip to content

Commit

Permalink
#5532: Alphatest entry box supports expressions now
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Mar 14, 2021
1 parent f553f76 commit 9179605
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 61 deletions.
6 changes: 6 additions & 0 deletions include/ishaderlayer.h
Expand Up @@ -288,6 +288,9 @@ class IShaderLayer
*/
virtual float getAlphaTest() const = 0;

// Returns the expression used to calculate the alpha test value
virtual const shaders::IShaderExpressionPtr& getAlphaTestExpression() const = 0;

/**
* Whether this stage is active. Unconditional stages always return true,
* conditional ones return the result of the most recent condition expression evaluation.
Expand Down Expand Up @@ -387,6 +390,9 @@ class IEditableShaderLayer :
// Set the translation expression [0..1] to the given string
virtual void setTranslationExpressionFromString(std::size_t index, const std::string& expression) = 0;

// Set the alpha test expression from the given string
virtual void setAlphaTestExpressionFromString(const std::string& expression) = 0;

// Update the "map" expression of this stage
virtual void setMapExpressionFromString(const std::string& expression) = 0;
};
63 changes: 28 additions & 35 deletions install/ui/materialeditor.fbp

Large diffs are not rendered by default.

30 changes: 10 additions & 20 deletions install/ui/materialeditor.xrc
Expand Up @@ -1844,37 +1844,27 @@
<object class="sizeritem">
<option>1</option>
<flag>wxEXPAND</flag>
<border>5</border>
<object class="wxFlexGridSizer">
<rows>0</rows>
<cols>2</cols>
<vgap>0</vgap>
<hgap>0</hgap>
<growablecols>1</growablecols>
<growablerows></growablerows>
<border>0</border>
<object class="wxBoxSizer">
<orient>wxHORIZONTAL</orient>
<object class="sizeritem">
<option>0</option>
<flag>wxALL</flag>
<border>0</border>
<object class="wxSpinCtrlDouble" name="MaterialStageAlphaTestValue">
<style>wxSP_ARROW_KEYS</style>
<value>0</value>
<min>0</min>
<max>1</max>
<inc>0.1</inc>
<digits>1</digits>
<option>1</option>
<flag>wxEXPAND|wxRIGHT</flag>
<border>12</border>
<object class="wxTextCtrl" name="MaterialStageAlphaTestExpression">
<value></value>
</object>
</object>
<object class="sizeritem">
<option>0</option>
<flag>wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT</flag>
<flag>wxALIGN_CENTER_VERTICAL</flag>
<border>0</border>
<object class="wxBoxSizer">
<orient>wxHORIZONTAL</orient>
<object class="sizeritem">
<option>0</option>
<flag>wxALIGN_CENTER_VERTICAL|wxLEFT</flag>
<border>24</border>
<border>0</border>
<object class="wxStaticText" name="m_staticText111">
<label>Private Polygon Offset:</label>
<wrap>-1</wrap>
Expand Down
12 changes: 10 additions & 2 deletions radiant/ui/materials/MaterialEditor.cpp
Expand Up @@ -57,7 +57,8 @@ MaterialEditor::MaterialEditor() :
DialogBase(DIALOG_TITLE),
_treeView(nullptr),
_stageList(new wxutil::TreeModel(STAGE_COLS(), true)),
_stageView(nullptr)
_stageView(nullptr),
_stageUpdateInProgress(false)
{
loadNamedPanel(this, "MaterialEditorMainPanel");

Expand Down Expand Up @@ -390,6 +391,10 @@ void MaterialEditor::setupMaterialStageProperties()
return layer->hasAlphaTest();
}));

createExpressionBinding("MaterialStageAlphaTestExpression",
[](const IShaderLayer::Ptr& layer) { return layer->getAlphaTestExpression(); },
[](const IEditableShaderLayer::Ptr& layer, const std::string& value) { layer->setAlphaTestExpressionFromString(value); });

for (const auto& value : {
"diffusemap", "bumpmap", "specularmap", "blend", "add", "filter", "modulate", "none", "Custom"
})
Expand Down Expand Up @@ -997,6 +1002,8 @@ void MaterialEditor::updateStageProgramControls()

void MaterialEditor::updateStageControls()
{
util::ScopedBoolLock lock(_stageUpdateInProgress);

auto selectedStage = getSelectedStage();

// Update all registered bindings
Expand All @@ -1016,7 +1023,6 @@ void MaterialEditor::updateStageControls()
{
selectedStage->evaluateExpressions(0); // initialise the values of this stage

getControl<wxSpinCtrlDouble>("MaterialStageAlphaTestValue")->SetValue(selectedStage->getAlphaTest());
getControl<wxSpinCtrlDouble>("MaterialStagePrivatePolygonOffset")->SetValue(selectedStage->getPrivatePolygonOffset());

auto mapExpr = selectedStage->getMapExpression();
Expand All @@ -1025,6 +1031,8 @@ void MaterialEditor::updateStageControls()

imageMap->Bind(wxEVT_TEXT, [imageMap, this](wxCommandEvent&)
{
if (_stageUpdateInProgress) return;

auto stage = getEditableStageForSelection();
stage->setMapExpressionFromString(imageMap->GetValue().ToStdString());
onMaterialChanged();
Expand Down
2 changes: 2 additions & 0 deletions radiant/ui/materials/MaterialEditor.h
Expand Up @@ -55,6 +55,8 @@ class MaterialEditor :
StageProgramParmsColumns _stageProgramColumns;
wxutil::TreeModel::Ptr _stageProgramParameters;

bool _stageUpdateInProgress;

private:
MaterialEditor();

Expand Down
61 changes: 60 additions & 1 deletion radiantcore/shaders/Doom3ShaderLayer.cpp
Expand Up @@ -92,6 +92,7 @@ Doom3ShaderLayer::Doom3ShaderLayer(ShaderTemplate& material, IShaderLayer::Type
_stageFlags(0),
_clampType(CLAMP_REPEAT),
_alphaTest(REG_ZERO),
_alphaTestExpression(NOT_DEFINED),
_texGenType(TEXGEN_NORMAL),
_privatePolygonOffset(0),
_parseFlags(0)
Expand Down Expand Up @@ -139,6 +140,7 @@ Doom3ShaderLayer::Doom3ShaderLayer(const Doom3ShaderLayer& other, ShaderTemplate
_stageFlags(other._stageFlags),
_clampType(other._clampType),
_alphaTest(other._alphaTest),
_alphaTestExpression(other._alphaTestExpression),
_texGenType(other._texGenType),
_rotation(other._rotation),
_rotationExpression(other._rotationExpression),
Expand Down Expand Up @@ -351,14 +353,19 @@ void Doom3ShaderLayer::setRenderMapSize(const Vector2& size)

bool Doom3ShaderLayer::hasAlphaTest() const
{
return _alphaTest != REG_ZERO;
return _alphaTestExpression != NOT_DEFINED;
}

float Doom3ShaderLayer::getAlphaTest() const
{
return _registers[_alphaTest];
}

const shaders::IShaderExpressionPtr& Doom3ShaderLayer::getAlphaTestExpression() const
{
return _alphaTestExpression != NOT_DEFINED ? _expressions[_alphaTestExpression] : NULL_EXPRESSION;
}

TexturePtr Doom3ShaderLayer::getFragmentMapTexture(int index) const
{
if (index < 0 || index >= static_cast<int>(_fragmentMaps.size()))
Expand Down Expand Up @@ -507,12 +514,60 @@ void Doom3ShaderLayer::addVertexParm(const VertexParm& parm)
assert(_vertexParms.size() % 4 == 0);
}

void Doom3ShaderLayer::assignExpression(const IShaderExpressionPtr& expression,
std::size_t& expressionIndex, std::size_t& registerIndex, std::size_t defaultRegisterIndex)
{
if (!expression)
{
// Assigning an empty expression will reset the slot to NOT_DEFINED
if (expressionIndex != NOT_DEFINED)
{
// Release the previous expression
assert(_expressions[expressionIndex]);

_expressions[expressionIndex]->unlinkFromRegisters();
_expressions[expressionIndex].reset();
}

expressionIndex = NOT_DEFINED;
registerIndex = defaultRegisterIndex;
return;
}

// Non-empty expression, overwrite if we have an existing expression in the slot
if (expressionIndex != NOT_DEFINED)
{
// Try to re-use the previous register position
auto previousExpression = _expressions[expressionIndex];
_expressions[expressionIndex] = expression;

if (previousExpression->isLinked())
{
registerIndex = previousExpression->unlinkFromRegisters();
expression->linkToSpecificRegister(_registers, registerIndex);
}
else
{
registerIndex = expression->linkToRegister(_registers);
}
}
else
{
expressionIndex = _expressions.size();
_expressions.emplace_back(expression);

registerIndex = expression->linkToRegister(_registers);
}
}

void Doom3ShaderLayer::assignExpressionFromString(const std::string& expressionString,
std::size_t& expressionIndex, std::size_t& registerIndex, std::size_t defaultRegisterIndex)
{
// An empty string will clear the expression
if (expressionString.empty())
{
assignExpression(IShaderExpressionPtr(), expressionIndex, registerIndex, defaultRegisterIndex);
#if 0
if (expressionIndex != NOT_DEFINED)
{
assert(_expressions[expressionIndex]);
Expand All @@ -523,6 +578,7 @@ void Doom3ShaderLayer::assignExpressionFromString(const std::string& expressionS

registerIndex = defaultRegisterIndex;
expressionIndex = NOT_DEFINED;
#endif
return;
}

Expand All @@ -534,6 +590,8 @@ void Doom3ShaderLayer::assignExpressionFromString(const std::string& expressionS
return; // parsing failures will not overwrite the expression slot
}

assignExpression(expression, expressionIndex, registerIndex, defaultRegisterIndex);
#if 0
if (expressionIndex != NOT_DEFINED)
{
// Try to re-use the previous register position
Expand All @@ -557,6 +615,7 @@ void Doom3ShaderLayer::assignExpressionFromString(const std::string& expressionS

registerIndex = expression->linkToRegister(_registers);
}
#endif
}

}
15 changes: 12 additions & 3 deletions radiantcore/shaders/Doom3ShaderLayer.h
Expand Up @@ -74,6 +74,7 @@ class Doom3ShaderLayer :

// Alpha test value, pointing into the register array. 0 means no test, otherwise must be within (0 - 1]
std::size_t _alphaTest;
std::size_t _alphaTestExpression;

// texgen normal, reflect, skybox, wobblesky
TexGenType _texGenType;
Expand Down Expand Up @@ -140,6 +141,12 @@ class Doom3ShaderLayer :

bool hasAlphaTest() const override;
float getAlphaTest() const override;
const shaders::IShaderExpressionPtr& getAlphaTestExpression() const override;

void setAlphaTestExpressionFromString(const std::string& expression) override
{
assignExpressionFromString(expression, _alphaTestExpression, _alphaTest, REG_ZERO);
}

// True if the condition for this stage is fulfilled
// (expressions must have been evaluated before this call)
Expand Down Expand Up @@ -470,10 +477,9 @@ class Doom3ShaderLayer :
* \brief
* Set alphatest expression
*/
void setAlphaTest(const IShaderExpressionPtr& expr)
void setAlphaTest(const IShaderExpressionPtr& expression)
{
_expressions.push_back(expr);
_alphaTest = expr->linkToRegister(_registers);
assignExpression(expression, _alphaTestExpression, _alphaTest, REG_ZERO);
}

// Returns the value of the given register
Expand Down Expand Up @@ -555,6 +561,9 @@ class Doom3ShaderLayer :
void setParseFlag(ParseFlags flag);

private:
void assignExpression(const IShaderExpressionPtr& expression,
std::size_t& expressionIndex, std::size_t& registerIndex, std::size_t defaultRegisterIndex);

void assignExpressionFromString(const std::string& expressionString,
std::size_t& expressionIndex, std::size_t& registerIndex, std::size_t defaultRegisterIndex);
};
Expand Down

0 comments on commit 9179605

Please sign in to comment.