diff options
author | Laszlo Agocs <laszlo.agocs@theqtcompany.com> | 2016-06-03 12:44:44 +0200 |
---|---|---|
committer | Laszlo Agocs <laszlo.agocs@theqtcompany.com> | 2016-06-06 10:11:04 +0000 |
commit | c639588c2cfa4eac2512238cc03c7126c8c19371 (patch) | |
tree | c7f2040bbf2696f17b9a21287e2efa74dcecfc28 /src/plugins/scenegraph/d3d12 | |
parent | 446661a199563b13db56aeddb7eeecf7259786d8 (diff) |
D3D12: Add flat color material
Change-Id: I9d86f9ff4a001cbadc7368862a8ce79083be59f8
Reviewed-by: Andy Nichols <andy.nichols@qt.io>
Diffstat (limited to 'src/plugins/scenegraph/d3d12')
4 files changed, 147 insertions, 19 deletions
diff --git a/src/plugins/scenegraph/d3d12/qsgd3d12builtinmaterials.cpp b/src/plugins/scenegraph/d3d12/qsgd3d12builtinmaterials.cpp index b631dd9d6c..ca92062120 100644 --- a/src/plugins/scenegraph/d3d12/qsgd3d12builtinmaterials.cpp +++ b/src/plugins/scenegraph/d3d12/qsgd3d12builtinmaterials.cpp @@ -45,6 +45,8 @@ #include "vs_vertexcolor.hlslh" #include "ps_vertexcolor.hlslh" +#include "vs_flatcolor.hlslh" +#include "ps_flatcolor.hlslh" #include "vs_smoothcolor.hlslh" #include "ps_smoothcolor.hlslh" #include "vs_texture.hlslh" @@ -66,6 +68,31 @@ QT_BEGIN_NAMESPACE // importantly, it is packed so that it does not cross a 16-byte (float4) // boundary. Hence the need for padding in some cases. +static inline QVector4D qsg_premultiply(const QVector4D &c, float globalOpacity) +{ + const float o = c.w() * globalOpacity; + return QVector4D(c.x() * o, c.y() * o, c.z() * o, o); +} + +static inline QVector4D qsg_premultiply(const QColor &c, float globalOpacity) +{ + const float o = c.alphaF() * globalOpacity; + return QVector4D(c.redF() * o, c.greenF() * o, c.blueF() * o, o); +} + +static inline int qsg_colorDiff(const QVector4D &a, const QVector4D &b) +{ + if (a.x() != b.x()) + return a.x() > b.x() ? 1 : -1; + if (a.y() != b.y()) + return a.y() > b.y() ? 1 : -1; + if (a.z() != b.z()) + return a.z() > b.z() ? 1 : -1; + if (a.w() != b.w()) + return a.w() > b.w() ? 1 : -1; + return 0; +} + QSGMaterialType QSGD3D12VertexColorMaterial::mtype; QSGMaterialType *QSGD3D12VertexColorMaterial::type() const @@ -121,6 +148,67 @@ QSGD3D12Material::UpdateResults QSGD3D12VertexColorMaterial::updatePipeline(cons return r; } +QSGMaterialType QSGD3D12FlatColorMaterial::mtype; + +QSGMaterialType *QSGD3D12FlatColorMaterial::type() const +{ + return &QSGD3D12FlatColorMaterial::mtype; +} + +int QSGD3D12FlatColorMaterial::compare(const QSGMaterial *other) const +{ + Q_ASSERT(other && type() == other->type()); + const QSGD3D12FlatColorMaterial *o = static_cast<const QSGD3D12FlatColorMaterial *>(other); + return m_color.rgba() - o->color().rgba(); +} + +static const int FLAT_COLOR_CB_SIZE_0 = 16 * sizeof(float); // float4x4 +static const int FLAT_COLOR_CB_SIZE_1 = 4 * sizeof(float); // float4 +static const int FLAT_COLOR_CB_SIZE = FLAT_COLOR_CB_SIZE_0 + FLAT_COLOR_CB_SIZE_1; + +int QSGD3D12FlatColorMaterial::constantBufferSize() const +{ + return QSGD3D12Engine::alignedConstantBufferSize(FLAT_COLOR_CB_SIZE); +} + +void QSGD3D12FlatColorMaterial::preparePipeline(QSGD3D12PipelineState *pipelineState) +{ + pipelineState->shaders.vs = g_VS_FlatColor; + pipelineState->shaders.vsSize = sizeof(g_VS_FlatColor); + pipelineState->shaders.ps = g_PS_FlatColor; + pipelineState->shaders.psSize = sizeof(g_PS_FlatColor); +} + +QSGD3D12Material::UpdateResults QSGD3D12FlatColorMaterial::updatePipeline(const QSGD3D12MaterialRenderState &state, + QSGD3D12PipelineState *, + ExtraState *, + quint8 *constantBuffer) +{ + QSGD3D12Material::UpdateResults r = 0; + quint8 *p = constantBuffer; + + if (state.isMatrixDirty()) { + memcpy(p, state.combinedMatrix().constData(), FLAT_COLOR_CB_SIZE_0); + r |= UpdatedConstantBuffer; + } + p += FLAT_COLOR_CB_SIZE_0; + + const QVector4D color = qsg_premultiply(m_color, state.opacity()); + const float f[] = { color.x(), color.y(), color.z(), color.w() }; + if (state.isOpacityDirty() || memcmp(p, f, FLAT_COLOR_CB_SIZE_1)) { + memcpy(p, f, FLAT_COLOR_CB_SIZE_1); + r |= UpdatedConstantBuffer; + } + + return r; +} + +void QSGD3D12FlatColorMaterial::setColor(const QColor &color) +{ + m_color = color; + setFlag(Blending, m_color.alpha() != 0xFF); +} + QSGD3D12SmoothColorMaterial::QSGD3D12SmoothColorMaterial() { setFlag(RequiresFullMatrixExceptTranslate, true); @@ -356,25 +444,6 @@ QSGD3D12Material::UpdateResults QSGD3D12SmoothTextureMaterial::updatePipeline(co return r; } -static inline QVector4D qsg_premultiply(const QVector4D &c, float globalOpacity) -{ - float o = c.w() * globalOpacity; - return QVector4D(c.x() * o, c.y() * o, c.z() * o, o); -} - -static inline int qsg_colorDiff(const QVector4D &a, const QVector4D &b) -{ - if (a.x() != b.x()) - return a.x() > b.x() ? 1 : -1; - if (a.y() != b.y()) - return a.y() > b.y() ? 1 : -1; - if (a.z() != b.z()) - return a.z() > b.z() ? 1 : -1; - if (a.w() != b.w()) - return a.w() > b.w() ? 1 : -1; - return 0; -} - QSGD3D12TextMaterial::QSGD3D12TextMaterial(StyleType styleType, QSGD3D12RenderContext *rc, const QRawFont &font, QFontEngine::GlyphFormat glyphFormat) : m_styleType(styleType), diff --git a/src/plugins/scenegraph/d3d12/qsgd3d12builtinmaterials_p.h b/src/plugins/scenegraph/d3d12/qsgd3d12builtinmaterials_p.h index e3c3957160..34ae73d2d6 100644 --- a/src/plugins/scenegraph/d3d12/qsgd3d12builtinmaterials_p.h +++ b/src/plugins/scenegraph/d3d12/qsgd3d12builtinmaterials_p.h @@ -77,6 +77,27 @@ private: static QSGMaterialType mtype; }; +class QSGD3D12FlatColorMaterial : public QSGD3D12Material +{ +public: + QSGMaterialType *type() const override; + int compare(const QSGMaterial *other) const override; + + int constantBufferSize() const override; + void preparePipeline(QSGD3D12PipelineState *pipelineState) override; + UpdateResults updatePipeline(const QSGD3D12MaterialRenderState &state, + QSGD3D12PipelineState *pipelineState, + ExtraState *extraState, + quint8 *constantBuffer) override; + + void setColor(const QColor &color); + QColor color() const { return m_color; } + +private: + static QSGMaterialType mtype; + QColor m_color; +}; + class QSGD3D12SmoothColorMaterial : public QSGD3D12Material { public: diff --git a/src/plugins/scenegraph/d3d12/shaders/flatcolor.hlsl b/src/plugins/scenegraph/d3d12/shaders/flatcolor.hlsl new file mode 100644 index 0000000000..034b51435a --- /dev/null +++ b/src/plugins/scenegraph/d3d12/shaders/flatcolor.hlsl @@ -0,0 +1,27 @@ +struct VSInput +{ + float4 position : POSITION; +}; + +cbuffer ConstantBuffer : register(b0) +{ + float4x4 mvp; + float4 color; +}; + +struct PSInput +{ + float4 position : SV_POSITION; +}; + +PSInput VS_FlatColor(VSInput input) +{ + PSInput result; + result.position = mul(mvp, input.position); + return result; +} + +float4 PS_FlatColor(PSInput input) : SV_TARGET +{ + return color; +} diff --git a/src/plugins/scenegraph/d3d12/shaders/shaders.pri b/src/plugins/scenegraph/d3d12/shaders/shaders.pri index 41bac334de..41f0ee80f7 100644 --- a/src/plugins/scenegraph/d3d12/shaders/shaders.pri +++ b/src/plugins/scenegraph/d3d12/shaders/shaders.pri @@ -8,6 +8,16 @@ vertexcolor_pshader.header = ps_vertexcolor.hlslh vertexcolor_pshader.entry = PS_VertexColor vertexcolor_pshader.type = ps_5_0 +flatcolor_VSPS = $$PWD/flatcolor.hlsl +flatcolor_vshader.input = flatcolor_VSPS +flatcolor_vshader.header = vs_flatcolor.hlslh +flatcolor_vshader.entry = VS_FlatColor +flatcolor_vshader.type = vs_5_0 +flatcolor_pshader.input = flatcolor_VSPS +flatcolor_pshader.header = ps_flatcolor.hlslh +flatcolor_pshader.entry = PS_FlatColor +flatcolor_pshader.type = ps_5_0 + stencilclip_VSPS = $$PWD/stencilclip.hlsl stencilclip_vshader.input = stencilclip_VSPS stencilclip_vshader.header = vs_stencilclip.hlslh @@ -106,6 +116,7 @@ tdr_cshader.type = cs_5_0 HLSL_SHADERS = \ vertexcolor_vshader vertexcolor_pshader \ + flatcolor_vshader flatcolor_pshader \ stencilclip_vshader stencilclip_pshader \ smoothcolor_vshader smoothcolor_pshader \ texture_vshader texture_pshader \ |