diff --git a/GPU/Common/DrawEngineCommon.cpp b/GPU/Common/DrawEngineCommon.cpp index e17111fa06..9fce9a6fb5 100644 --- a/GPU/Common/DrawEngineCommon.cpp +++ b/GPU/Common/DrawEngineCommon.cpp @@ -72,6 +72,52 @@ VertexDecoder *DrawEngineCommon::GetVertexDecoder(u32 vtype) { return dec; } +int DrawEngineCommon::ComputeNumVertsToDecode() const { + int vertsToDecode = 0; + if (drawCalls[0].indexType == GE_VTYPE_IDX_NONE >> GE_VTYPE_IDX_SHIFT) { + for (int i = 0; i < numDrawCalls; i++) { + const DeferredDrawCall &dc = drawCalls[i]; + vertsToDecode += dc.vertexCount; + } + } else { + // TODO: Share this computation with DecodeVertsStep? + for (int i = 0; i < numDrawCalls; i++) { + const DeferredDrawCall &dc = drawCalls[i]; + int lastMatch = i; + const int total = numDrawCalls; + int indexLowerBound = dc.indexLowerBound; + int indexUpperBound = dc.indexUpperBound; + for (int j = i + 1; j < total; ++j) { + if (drawCalls[j].verts != dc.verts) + break; + + indexLowerBound = std::min(indexLowerBound, (int)drawCalls[j].indexLowerBound); + indexUpperBound = std::max(indexUpperBound, (int)drawCalls[j].indexUpperBound); + lastMatch = j; + } + vertsToDecode += indexUpperBound - indexLowerBound + 1; + i = lastMatch; + } + } + return vertsToDecode; +} + +void DrawEngineCommon::DecodeVerts(u8 *dest) { + const UVScale origUV = gstate_c.uv; + for (; decodeCounter_ < numDrawCalls; decodeCounter_++) { + gstate_c.uv = uvScale[decodeCounter_]; + DecodeVertsStep(dest, decodeCounter_, decodedVerts_); // NOTE! DecodeVertsStep can modify decodeCounter_! + } + gstate_c.uv = origUV; + + // Sanity check + if (indexGen.Prim() < 0) { + ERROR_LOG_REPORT(G3D, "DecodeVerts: Failed to deduce prim: %i", indexGen.Prim()); + // Force to points (0) + indexGen.AddPrim(GE_PRIM_POINTS, 0); + } +} + std::vector DrawEngineCommon::DebugGetVertexLoaderIDs() { std::vector ids; decoderMap_.Iterate([&](const uint32_t vtype, VertexDecoder *decoder) { diff --git a/GPU/Common/DrawEngineCommon.h b/GPU/Common/DrawEngineCommon.h index 39a53bb83a..961e5dad5e 100644 --- a/GPU/Common/DrawEngineCommon.h +++ b/GPU/Common/DrawEngineCommon.h @@ -77,6 +77,9 @@ public: protected: virtual void ClearTrackedVertexArrays() {} + int ComputeNumVertsToDecode() const; + void DecodeVerts(u8 *dest); + // Preprocessing for spline/bezier u32 NormalizeVertices(u8 *outPtr, u8 *bufPtr, const u8 *inPtr, int lowerBound, int upperBound, u32 vertType, int *vertexSize = nullptr); diff --git a/GPU/D3D11/DrawEngineD3D11.cpp b/GPU/D3D11/DrawEngineD3D11.cpp index 15d67838ee..7f09171a13 100644 --- a/GPU/D3D11/DrawEngineD3D11.cpp +++ b/GPU/D3D11/DrawEngineD3D11.cpp @@ -315,22 +315,6 @@ void DrawEngineD3D11::SubmitPrim(void *verts, void *inds, GEPrimitiveType prim, } } -void DrawEngineD3D11::DecodeVerts() { - const UVScale origUV = gstate_c.uv; - for (; decodeCounter_ < numDrawCalls; decodeCounter_++) { - gstate_c.uv = uvScale[decodeCounter_]; - DecodeVertsStep(decoded, decodeCounter_, decodedVerts_); - } - gstate_c.uv = origUV; - - // Sanity check - if (indexGen.Prim() < 0) { - ERROR_LOG_REPORT(G3D, "DecodeVerts: Failed to deduce prim: %i", indexGen.Prim()); - // Force to points (0) - indexGen.AddPrim(GE_PRIM_POINTS, 0); - } -} - void DrawEngineD3D11::MarkUnreliable(VertexArrayInfoD3D11 *vai) { vai->status = VertexArrayInfoD3D11::VAI_UNRELIABLE; if (vai->vbo) { @@ -438,7 +422,7 @@ void DrawEngineD3D11::DoFlush() { vai->minihash = ComputeMiniHash(); vai->status = VertexArrayInfoD3D11::VAI_HASHING; vai->drawsUntilNextFullHash = 0; - DecodeVerts(); // writes to indexGen + DecodeVerts(decoded); // writes to indexGen vai->numVerts = indexGen.VertexCount(); vai->prim = indexGen.Prim(); vai->maxIndex = indexGen.MaxIndex(); @@ -463,7 +447,7 @@ void DrawEngineD3D11::DoFlush() { } if (newMiniHash != vai->minihash || newHash != vai->hash) { MarkUnreliable(vai); - DecodeVerts(); + DecodeVerts(decoded); goto rotateVBO; } if (vai->numVerts > 64) { @@ -482,13 +466,13 @@ void DrawEngineD3D11::DoFlush() { u32 newMiniHash = ComputeMiniHash(); if (newMiniHash != vai->minihash) { MarkUnreliable(vai); - DecodeVerts(); + DecodeVerts(decoded); goto rotateVBO; } } if (vai->vbo == 0) { - DecodeVerts(); + DecodeVerts(decoded); vai->numVerts = indexGen.VertexCount(); vai->prim = indexGen.Prim(); vai->maxIndex = indexGen.MaxIndex(); @@ -554,14 +538,14 @@ void DrawEngineD3D11::DoFlush() { if (vai->lastFrame != gpuStats.numFlips) { vai->numFrames++; } - DecodeVerts(); + DecodeVerts(decoded); goto rotateVBO; } } vai->lastFrame = gpuStats.numFlips; } else { - DecodeVerts(); + DecodeVerts(decoded); rotateVBO: gpuStats.numUncachedVertsDrawn += indexGen.VertexCount(); useElements = !indexGen.SeenOnlyPurePrims() || prim == GE_PRIM_TRIANGLE_FAN; @@ -632,7 +616,7 @@ rotateVBO: } } } else { - DecodeVerts(); + DecodeVerts(decoded); bool hasColor = (lastVType_ & GE_VTYPE_COL_MASK) != GE_VTYPE_COL_NONE; if (gstate.isModeThrough()) { gstate_c.vertexFullAlpha = gstate_c.vertexFullAlpha && (hasColor || gstate.getMaterialAmbientA() == 255); diff --git a/GPU/D3D11/DrawEngineD3D11.h b/GPU/D3D11/DrawEngineD3D11.h index b0c77b3e4b..f78a794484 100644 --- a/GPU/D3D11/DrawEngineD3D11.h +++ b/GPU/D3D11/DrawEngineD3D11.h @@ -131,7 +131,7 @@ public: void FinishDeferred() { if (!numDrawCalls) return; - DecodeVerts(); + DecodeVerts(decoded); } void DispatchFlush() override { Flush(); } @@ -146,7 +146,6 @@ public: void ClearInputLayoutMap(); private: - void DecodeVerts(); void DoFlush(); void ApplyDrawState(int prim); diff --git a/GPU/Directx9/DrawEngineDX9.cpp b/GPU/Directx9/DrawEngineDX9.cpp index f452c945c0..da76b0024f 100644 --- a/GPU/Directx9/DrawEngineDX9.cpp +++ b/GPU/Directx9/DrawEngineDX9.cpp @@ -296,22 +296,6 @@ void DrawEngineDX9::SubmitPrim(void *verts, void *inds, GEPrimitiveType prim, in } } -void DrawEngineDX9::DecodeVerts() { - const UVScale origUV = gstate_c.uv; - for (; decodeCounter_ < numDrawCalls; decodeCounter_++) { - gstate_c.uv = uvScale[decodeCounter_]; - DecodeVertsStep(decoded, decodeCounter_, decodedVerts_); - } - gstate_c.uv = origUV; - - // Sanity check - if (indexGen.Prim() < 0) { - ERROR_LOG_REPORT(G3D, "DecodeVerts: Failed to deduce prim: %i", indexGen.Prim()); - // Force to points (0) - indexGen.AddPrim(GE_PRIM_POINTS, 0); - } -} - void DrawEngineDX9::MarkUnreliable(VertexArrayInfoDX9 *vai) { vai->status = VertexArrayInfoDX9::VAI_UNRELIABLE; if (vai->vbo) { @@ -424,7 +408,7 @@ void DrawEngineDX9::DoFlush() { vai->minihash = ComputeMiniHash(); vai->status = VertexArrayInfoDX9::VAI_HASHING; vai->drawsUntilNextFullHash = 0; - DecodeVerts(); // writes to indexGen + DecodeVerts(decoded); // writes to indexGen vai->numVerts = indexGen.VertexCount(); vai->prim = indexGen.Prim(); vai->maxIndex = indexGen.MaxIndex(); @@ -450,7 +434,7 @@ void DrawEngineDX9::DoFlush() { } if (newMiniHash != vai->minihash || newHash != vai->hash) { MarkUnreliable(vai); - DecodeVerts(); + DecodeVerts(decoded); goto rotateVBO; } if (vai->numVerts > 64) { @@ -469,13 +453,13 @@ void DrawEngineDX9::DoFlush() { u32 newMiniHash = ComputeMiniHash(); if (newMiniHash != vai->minihash) { MarkUnreliable(vai); - DecodeVerts(); + DecodeVerts(decoded); goto rotateVBO; } } if (vai->vbo == 0) { - DecodeVerts(); + DecodeVerts(decoded); vai->numVerts = indexGen.VertexCount(); vai->prim = indexGen.Prim(); vai->maxIndex = indexGen.MaxIndex(); @@ -544,14 +528,14 @@ void DrawEngineDX9::DoFlush() { if (vai->lastFrame != gpuStats.numFlips) { vai->numFrames++; } - DecodeVerts(); + DecodeVerts(decoded); goto rotateVBO; } } vai->lastFrame = gpuStats.numFlips; } else { - DecodeVerts(); + DecodeVerts(decoded); rotateVBO: gpuStats.numUncachedVertsDrawn += indexGen.VertexCount(); useElements = !indexGen.SeenOnlyPurePrims(); @@ -596,7 +580,7 @@ rotateVBO: } } } else { - DecodeVerts(); + DecodeVerts(decoded); bool hasColor = (lastVType_ & GE_VTYPE_COL_MASK) != GE_VTYPE_COL_NONE; if (gstate.isModeThrough()) { gstate_c.vertexFullAlpha = gstate_c.vertexFullAlpha && (hasColor || gstate.getMaterialAmbientA() == 255); diff --git a/GPU/Directx9/DrawEngineDX9.h b/GPU/Directx9/DrawEngineDX9.h index 58bbfb3888..35cc5fd18e 100644 --- a/GPU/Directx9/DrawEngineDX9.h +++ b/GPU/Directx9/DrawEngineDX9.h @@ -130,7 +130,7 @@ public: void FinishDeferred() { if (!numDrawCalls) return; - DecodeVerts(); + DecodeVerts(decoded); } void DispatchFlush() override { Flush(); } @@ -139,7 +139,6 @@ public: } private: - void DecodeVerts(); void DoFlush(); void ApplyDrawState(int prim); diff --git a/GPU/GLES/DrawEngineGLES.cpp b/GPU/GLES/DrawEngineGLES.cpp index 6034d491c3..030400479a 100644 --- a/GPU/GLES/DrawEngineGLES.cpp +++ b/GPU/GLES/DrawEngineGLES.cpp @@ -314,21 +314,6 @@ void DrawEngineGLES::SubmitPrim(void *verts, void *inds, GEPrimitiveType prim, i } } -void DrawEngineGLES::DecodeVerts() { - const UVScale origUV = gstate_c.uv; - for (; decodeCounter_ < numDrawCalls; decodeCounter_++) { - gstate_c.uv = uvScale[decodeCounter_]; - DecodeVertsStep(decoded, decodeCounter_, decodedVerts_); - } - gstate_c.uv = origUV; - // Sanity check - if (indexGen.Prim() < 0) { - ERROR_LOG_REPORT(G3D, "DecodeVerts: Failed to deduce prim: %i", indexGen.Prim()); - // Force to points (0) - indexGen.AddPrim(GE_PRIM_POINTS, 0); - } -} - void DrawEngineGLES::MarkUnreliable(VertexArrayInfo *vai) { vai->status = VertexArrayInfo::VAI_UNRELIABLE; if (vai->vbo) { @@ -495,7 +480,7 @@ void DrawEngineGLES::DoFlush() { vai->minihash = ComputeMiniHash(); vai->status = VertexArrayInfo::VAI_HASHING; vai->drawsUntilNextFullHash = 0; - DecodeVerts(); // writes to indexGen + DecodeVerts(decoded); // writes to indexGen vai->numVerts = indexGen.VertexCount(); vai->prim = indexGen.Prim(); vai->maxIndex = indexGen.MaxIndex(); @@ -521,7 +506,7 @@ void DrawEngineGLES::DoFlush() { } if (newMiniHash != vai->minihash || newHash != vai->hash) { MarkUnreliable(vai); - DecodeVerts(); + DecodeVerts(decoded); goto rotateVBO; } if (vai->numVerts > 64) { @@ -540,13 +525,13 @@ void DrawEngineGLES::DoFlush() { u32 newMiniHash = ComputeMiniHash(); if (newMiniHash != vai->minihash) { MarkUnreliable(vai); - DecodeVerts(); + DecodeVerts(decoded); goto rotateVBO; } } if (vai->vbo == 0) { - DecodeVerts(); + DecodeVerts(decoded); vai->numVerts = indexGen.VertexCount(); vai->prim = indexGen.Prim(); vai->maxIndex = indexGen.MaxIndex(); @@ -615,14 +600,14 @@ void DrawEngineGLES::DoFlush() { if (vai->lastFrame != gpuStats.numFlips) { vai->numFrames++; } - DecodeVerts(); + DecodeVerts(decoded); goto rotateVBO; } } vai->lastFrame = gpuStats.numFlips; } else { - DecodeVerts(); + DecodeVerts(decoded); rotateVBO: gpuStats.numUncachedVertsDrawn += indexGen.VertexCount(); @@ -667,7 +652,7 @@ rotateVBO: glDrawArrays(glprim[prim], 0, vertexCount); } } else { - DecodeVerts(); + DecodeVerts(decoded); bool hasColor = (lastVType_ & GE_VTYPE_COL_MASK) != GE_VTYPE_COL_NONE; if (gstate.isModeThrough()) { gstate_c.vertexFullAlpha = gstate_c.vertexFullAlpha && (hasColor || gstate.getMaterialAmbientA() == 255); diff --git a/GPU/GLES/DrawEngineGLES.h b/GPU/GLES/DrawEngineGLES.h index 02613af41c..f2be7c9a6f 100644 --- a/GPU/GLES/DrawEngineGLES.h +++ b/GPU/GLES/DrawEngineGLES.h @@ -138,7 +138,7 @@ public: void FinishDeferred() { if (!numDrawCalls) return; - DecodeVerts(); + DecodeVerts(decoded); } bool IsCodePtrVertexDecoder(const u8 *ptr) const; @@ -154,7 +154,6 @@ public: void DecimateBuffers(); private: - void DecodeVerts(); void DoFlush(); void ApplyDrawState(int prim); void ApplyDrawStateLate(); diff --git a/GPU/GLES/FragmentShaderGeneratorGLES.cpp b/GPU/GLES/FragmentShaderGeneratorGLES.cpp index 7093f87d3e..8c5486687a 100644 --- a/GPU/GLES/FragmentShaderGeneratorGLES.cpp +++ b/GPU/GLES/FragmentShaderGeneratorGLES.cpp @@ -36,9 +36,10 @@ // #define DEBUG_SHADER // Missing: Z depth range -bool GenerateFragmentShader(const ShaderID &id, char *buffer) { +bool GenerateFragmentShader(const ShaderID &id, char *buffer, uint64_t *uniformMask) { char *p = buffer; + *uniformMask = 0; // In GLSL ES 3.0, you use "in" variables instead of varying. bool glslES30 = false; @@ -180,6 +181,7 @@ bool GenerateFragmentShader(const ShaderID &id, char *buffer) { WRITE(p, "uniform sampler2D tex;\n"); if (!isModeClear && replaceBlend > REPLACE_BLEND_STANDARD) { + *uniformMask |= DIRTY_SHADERBLEND; if (!gstate_c.Supports(GPU_SUPPORTS_ANY_FRAMEBUFFER_FETCH) && replaceBlend == REPLACE_BLEND_COPY_FBO) { if (!texelFetch) { WRITE(p, "uniform vec2 u_fbotexSize;\n"); @@ -195,6 +197,7 @@ bool GenerateFragmentShader(const ShaderID &id, char *buffer) { } if (needShaderTexClamp && doTexture) { + *uniformMask |= DIRTY_TEXCLAMP; WRITE(p, "uniform vec4 u_texclamp;\n"); if (id.Bit(FS_BIT_TEXTURE_AT_OFFSET)) { WRITE(p, "uniform vec2 u_texclampoff;\n"); @@ -205,8 +208,10 @@ bool GenerateFragmentShader(const ShaderID &id, char *buffer) { if (g_Config.bFragmentTestCache) { WRITE(p, "uniform sampler2D testtex;\n"); } else { + *uniformMask |= DIRTY_ALPHACOLORREF; WRITE(p, "uniform vec4 u_alphacolorref;\n"); if (bitwiseOps && ((enableColorTest && !colorTestAgainstZero) || (enableAlphaTest && !alphaTestAgainstZero))) { + *uniformMask |= DIRTY_ALPHACOLORMASK; WRITE(p, "uniform ivec4 u_alphacolormask;\n"); } } @@ -214,15 +219,19 @@ bool GenerateFragmentShader(const ShaderID &id, char *buffer) { StencilValueType replaceAlphaWithStencilType = (StencilValueType)id.Bits(FS_BIT_REPLACE_ALPHA_WITH_STENCIL_TYPE, 4); if (stencilToAlpha && replaceAlphaWithStencilType == STENCIL_VALUE_UNIFORM) { + *uniformMask |= DIRTY_STENCILREPLACEVALUE; WRITE(p, "uniform float u_stencilReplaceValue;\n"); } - if (doTexture && texFunc == GE_TEXFUNC_BLEND) + if (doTexture && texFunc == GE_TEXFUNC_BLEND) { + *uniformMask |= DIRTY_TEXENV; WRITE(p, "uniform vec3 u_texenv;\n"); + } WRITE(p, "%s %s vec4 v_color0;\n", shading, varying); if (lmode) WRITE(p, "%s %s vec3 v_color1;\n", shading, varying); if (enableFog) { + *uniformMask |= DIRTY_FOGCOLOR; WRITE(p, "uniform vec3 u_fogcolor;\n"); WRITE(p, "%s %s float v_fogdepth;\n", varying, highpFog ? "highp" : "mediump"); } diff --git a/GPU/GLES/FragmentShaderGeneratorGLES.h b/GPU/GLES/FragmentShaderGeneratorGLES.h index 0e3f924b36..3a5bce69f5 100644 --- a/GPU/GLES/FragmentShaderGeneratorGLES.h +++ b/GPU/GLES/FragmentShaderGeneratorGLES.h @@ -19,4 +19,4 @@ struct ShaderID; -bool GenerateFragmentShader(const ShaderID &id, char *buffer); +bool GenerateFragmentShader(const ShaderID &id, char *buffer, uint64_t *uniformMask); diff --git a/GPU/GLES/ShaderManagerGLES.cpp b/GPU/GLES/ShaderManagerGLES.cpp index 33b4b46d99..cf48631d02 100644 --- a/GPU/GLES/ShaderManagerGLES.cpp +++ b/GPU/GLES/ShaderManagerGLES.cpp @@ -43,8 +43,8 @@ #include "GPU/GLES/DrawEngineGLES.h" #include "FramebufferManagerGLES.h" -Shader::Shader(const char *code, uint32_t glShaderType, bool useHWTransform) - : failed_(false), useHWTransform_(useHWTransform) { +Shader::Shader(const char *code, uint32_t glShaderType, bool useHWTransform, uint32_t attrMask, uint64_t uniformMask) + : failed_(false), useHWTransform_(useHWTransform), attrMask_(attrMask), uniformMask_(uniformMask) { PROFILE_THIS_SCOPE("shadercomp"); isFragment_ = glShaderType == GL_FRAGMENT_SHADER; source_ = code; @@ -241,62 +241,8 @@ LinkedShader::LinkedShader(ShaderID VSID, Shader *vs, ShaderID FSID, Shader *fs, u_spline_type_u = glGetUniformLocation(program, "u_spline_type_u"); u_spline_type_v = glGetUniformLocation(program, "u_spline_type_v"); - attrMask = 0; - if (-1 != glGetAttribLocation(program, "position")) attrMask |= 1 << ATTR_POSITION; - if (-1 != glGetAttribLocation(program, "texcoord")) attrMask |= 1 << ATTR_TEXCOORD; - if (-1 != glGetAttribLocation(program, "normal")) attrMask |= 1 << ATTR_NORMAL; - if (-1 != glGetAttribLocation(program, "w1")) attrMask |= 1 << ATTR_W1; - if (-1 != glGetAttribLocation(program, "w2")) attrMask |= 1 << ATTR_W2; - if (-1 != glGetAttribLocation(program, "color0")) attrMask |= 1 << ATTR_COLOR0; - if (-1 != glGetAttribLocation(program, "color1")) attrMask |= 1 << ATTR_COLOR1; - - availableUniforms = 0; - if (u_proj != -1) availableUniforms |= DIRTY_PROJMATRIX; - if (u_proj_through != -1) availableUniforms |= DIRTY_PROJTHROUGHMATRIX; - if (u_texenv != -1) availableUniforms |= DIRTY_TEXENV; - if (u_alphacolorref != -1) availableUniforms |= DIRTY_ALPHACOLORREF; - if (u_alphacolormask != -1) availableUniforms |= DIRTY_ALPHACOLORMASK; - if (u_fogcolor != -1) availableUniforms |= DIRTY_FOGCOLOR; - if (u_fogcoef != -1) availableUniforms |= DIRTY_FOGCOEF; - if (u_texenv != -1) availableUniforms |= DIRTY_TEXENV; - if (u_uvscaleoffset != -1) availableUniforms |= DIRTY_UVSCALEOFFSET; - if (u_texclamp != -1) availableUniforms |= DIRTY_TEXCLAMP; - if (u_world != -1) availableUniforms |= DIRTY_WORLDMATRIX; - if (u_view != -1) availableUniforms |= DIRTY_VIEWMATRIX; - if (u_texmtx != -1) availableUniforms |= DIRTY_TEXMATRIX; - if (u_stencilReplaceValue != -1) availableUniforms |= DIRTY_STENCILREPLACEVALUE; - if (u_blendFixA != -1 || u_blendFixB != -1 || u_fbotexSize != -1) availableUniforms |= DIRTY_SHADERBLEND; - if (u_depthRange != -1) - availableUniforms |= DIRTY_DEPTHRANGE; - - // Looping up to numBones lets us avoid checking u_bone[i] -#ifdef USE_BONE_ARRAY - if (u_bone != -1) { - for (int i = 0; i < numBones; i++) { - availableUniforms |= DIRTY_BONEMATRIX0 << i; - } - } -#else - for (int i = 0; i < numBones; i++) { - if (u_bone[i] != -1) - availableUniforms |= DIRTY_BONEMATRIX0 << i; - } -#endif - if (u_ambient != -1) availableUniforms |= DIRTY_AMBIENT; - if (u_matambientalpha != -1) availableUniforms |= DIRTY_MATAMBIENTALPHA; - if (u_matdiffuse != -1) availableUniforms |= DIRTY_MATDIFFUSE; - if (u_matemissive != -1) availableUniforms |= DIRTY_MATEMISSIVE; - if (u_matspecular != -1) availableUniforms |= DIRTY_MATSPECULAR; - for (int i = 0; i < 4; i++) { - if (u_lightdir[i] != -1 || - u_lightspecular[i] != -1 || - u_lightpos[i] != -1) - availableUniforms |= DIRTY_LIGHT0 << i; - } - if (u_spline_count_u != -1) availableUniforms |= DIRTY_BEZIERSPLINE; - if (u_spline_count_v != -1) availableUniforms |= DIRTY_BEZIERSPLINE; - if (u_spline_type_u != -1) availableUniforms |= DIRTY_BEZIERSPLINE; - if (u_spline_type_v != -1) availableUniforms |= DIRTY_BEZIERSPLINE; + attrMask = vs->GetAttrMask(); + availableUniforms = vs->GetUniformMask() | fs->GetUniformMask(); glUseProgram(program); @@ -304,7 +250,6 @@ LinkedShader::LinkedShader(ShaderID VSID, Shader *vs, ShaderID FSID, Shader *fs, glUniform1i(u_tex, 0); glUniform1i(u_fbotex, 1); glUniform1i(u_testtex, 2); - if (u_tess_pos_tex != -1) glUniform1i(u_tess_pos_tex, 4); // Texture unit 4 @@ -789,16 +734,19 @@ void ShaderManagerGLES::DirtyLastShader() { // disables vertex arrays } Shader *ShaderManagerGLES::CompileFragmentShader(ShaderID FSID) { - if (!GenerateFragmentShader(FSID, codeBuffer_)) { + uint64_t uniformMask; + if (!GenerateFragmentShader(FSID, codeBuffer_, &uniformMask)) { return nullptr; } - return new Shader(codeBuffer_, GL_FRAGMENT_SHADER, false); + return new Shader(codeBuffer_, GL_FRAGMENT_SHADER, false, 0, uniformMask); } Shader *ShaderManagerGLES::CompileVertexShader(ShaderID VSID) { bool useHWTransform = VSID.Bit(VS_BIT_USE_HW_TRANSFORM); - GenerateVertexShader(VSID, codeBuffer_); - return new Shader(codeBuffer_, GL_VERTEX_SHADER, useHWTransform); + uint32_t attrMask; + uint64_t uniformMask; + GenerateVertexShader(VSID, codeBuffer_, &attrMask, &uniformMask); + return new Shader(codeBuffer_, GL_VERTEX_SHADER, useHWTransform, attrMask, uniformMask); } Shader *ShaderManagerGLES::ApplyVertexShader(int prim, u32 vertType, ShaderID *VSID) { @@ -846,8 +794,10 @@ Shader *ShaderManagerGLES::ApplyVertexShader(int prim, u32 vertType, ShaderID *V // Can still work with software transform. ShaderID vsidTemp; ComputeVertexShaderID(&vsidTemp, vertType, false); - GenerateVertexShader(vsidTemp, codeBuffer_); - vs = new Shader(codeBuffer_, GL_VERTEX_SHADER, false); + uint32_t attrMask; + uint64_t uniformMask; + GenerateVertexShader(vsidTemp, codeBuffer_, &attrMask, &uniformMask); + vs = new Shader(codeBuffer_, GL_VERTEX_SHADER, false, attrMask, uniformMask); } vsCache_.Insert(*VSID, vs); diff --git a/GPU/GLES/ShaderManagerGLES.h b/GPU/GLES/ShaderManagerGLES.h index 0f1065a25c..510e94c286 100644 --- a/GPU/GLES/ShaderManagerGLES.h +++ b/GPU/GLES/ShaderManagerGLES.h @@ -124,7 +124,7 @@ public: class Shader { public: - Shader(const char *code, uint32_t glShaderType, bool useHWTransform); + Shader(const char *code, uint32_t glShaderType, bool useHWTransform, uint32_t attrMask, uint64_t uniformMask); ~Shader(); uint32_t shader; @@ -133,11 +133,16 @@ public: std::string GetShaderString(DebugShaderStringType type, ShaderID id) const; + uint32_t GetAttrMask() const { return attrMask_; } + uint64_t GetUniformMask() const { return uniformMask_; } + private: std::string source_; bool failed_; bool useHWTransform_; bool isFragment_; + uint32_t attrMask_; // only used in vertex shaders + uint64_t uniformMask_; }; class ShaderManagerGLES : public ShaderManagerCommon { diff --git a/GPU/GLES/VertexShaderGeneratorGLES.cpp b/GPU/GLES/VertexShaderGeneratorGLES.cpp index d4812caf5e..a6087f33e1 100644 --- a/GPU/GLES/VertexShaderGeneratorGLES.cpp +++ b/GPU/GLES/VertexShaderGeneratorGLES.cpp @@ -95,9 +95,10 @@ enum DoLightComputation { // is a bit of a rare configuration, although quite common on mobile. -void GenerateVertexShader(const ShaderID &id, char *buffer) { +void GenerateVertexShader(const ShaderID &id, char *buffer, uint32_t *attrMask, uint64_t *uniformMask) { char *p = buffer; - + *attrMask = 0; + *uniformMask = 0; // #define USE_FOR_LOOP // In GLSL ES 3.0, you use "out" variables instead. @@ -198,7 +199,7 @@ void GenerateVertexShader(const ShaderID &id, char *buffer) { if (glslES30) shading = doFlatShading ? "flat " : ""; - DoLightComputation doLight[4] = {LIGHT_OFF, LIGHT_OFF, LIGHT_OFF, LIGHT_OFF}; + DoLightComputation doLight[4] = { LIGHT_OFF, LIGHT_OFF, LIGHT_OFF, LIGHT_OFF }; if (useHWTransform) { int shadeLight0 = doShadeMapping ? ls0 : -1; int shadeLight1 = doShadeMapping ? ls1 : -1; @@ -215,15 +216,21 @@ void GenerateVertexShader(const ShaderID &id, char *buffer) { if (enableBones) { numBoneWeights = 1 + id.Bits(VS_BIT_BONES, 3); WRITE(p, "%s", boneWeightDecl[numBoneWeights]); + *attrMask |= 1 << ATTR_W1; + if (numBoneWeights >= 5) + *attrMask |= 1 << ATTR_W2; } if (useHWTransform) WRITE(p, "%s vec3 position;\n", attribute); else WRITE(p, "%s vec4 position;\n", attribute); // need to pass the fog coord in w + *attrMask |= 1 << ATTR_POSITION; - if (useHWTransform && hasNormal) + if (useHWTransform && hasNormal) { WRITE(p, "%s mediump vec3 normal;\n", attribute); + *attrMask |= 1 << ATTR_NORMAL; + } bool texcoordVec3In = false; if (doTexture && hasTexcoord) { @@ -233,17 +240,23 @@ void GenerateVertexShader(const ShaderID &id, char *buffer) { } else { WRITE(p, "%s vec2 texcoord;\n", attribute); } + *attrMask |= 1 << ATTR_TEXCOORD; } if (hasColor) { WRITE(p, "%s lowp vec4 color0;\n", attribute); - if (lmode && !useHWTransform) // only software transform supplies color1 as vertex data + *attrMask |= 1 << ATTR_COLOR0; + if (lmode && !useHWTransform) { // only software transform supplies color1 as vertex data WRITE(p, "%s lowp vec3 color1;\n", attribute); + *attrMask |= 1 << ATTR_COLOR1; + } } - if (isModeThrough) { + if (isModeThrough) { WRITE(p, "uniform mat4 u_proj_through;\n"); + *uniformMask |= DIRTY_PROJTHROUGHMATRIX; } else { WRITE(p, "uniform mat4 u_proj;\n"); + *uniformMask |= DIRTY_PROJMATRIX; // Add all the uniforms we'll need to transform properly. } @@ -253,28 +266,36 @@ void GenerateVertexShader(const ShaderID &id, char *buffer) { // When transforming by hardware, we need a great deal more uniforms... WRITE(p, "uniform mat4 u_world;\n"); WRITE(p, "uniform mat4 u_view;\n"); - if (doTextureProjection) + *uniformMask |= DIRTY_WORLDMATRIX | DIRTY_VIEWMATRIX; + if (doTextureProjection) { WRITE(p, "uniform mediump mat4 u_texmtx;\n"); + *uniformMask |= DIRTY_TEXMATRIX; + } if (enableBones) { #ifdef USE_BONE_ARRAY WRITE(p, "uniform mediump mat4 u_bone[%i];\n", numBoneWeights); + *uniformMask |= DIRTY_BONE_UNIFORMS; #else for (int i = 0; i < numBoneWeights; i++) { WRITE(p, "uniform mat4 u_bone%i;\n", i); + *uniformMask |= DIRTY_BONEMATRIX0 << i; } #endif } if (doTexture) { WRITE(p, "uniform vec4 u_uvscaleoffset;\n"); + *uniformMask |= DIRTY_UVSCALEOFFSET; } for (int i = 0; i < 4; i++) { if (doLight[i] != LIGHT_OFF) { // This is needed for shade mapping WRITE(p, "uniform vec3 u_lightpos%i;\n", i); + *uniformMask |= DIRTY_LIGHT0 << i; } if (doLight[i] == LIGHT_FULL) { - GELightType type = static_cast(id.Bits(VS_BIT_LIGHT0_TYPE + 4*i, 2)); - GELightComputation comp = static_cast(id.Bits(VS_BIT_LIGHT0_COMP + 4*i, 2)); + *uniformMask |= DIRTY_LIGHT0 << i; + GELightType type = static_cast(id.Bits(VS_BIT_LIGHT0_TYPE + 4 * i, 2)); + GELightComputation comp = static_cast(id.Bits(VS_BIT_LIGHT0_COMP + 4 * i, 2)); if (type != GE_LIGHTTYPE_DIRECTIONAL) WRITE(p, "uniform mediump vec3 u_lightatt%i;\n", i); @@ -294,22 +315,29 @@ void GenerateVertexShader(const ShaderID &id, char *buffer) { } if (enableLighting) { WRITE(p, "uniform lowp vec4 u_ambient;\n"); - if ((matUpdate & 2) == 0 || !hasColor) + *uniformMask |= DIRTY_AMBIENT; + if ((matUpdate & 2) == 0 || !hasColor) { WRITE(p, "uniform lowp vec3 u_matdiffuse;\n"); + *uniformMask |= DIRTY_MATDIFFUSE; + } WRITE(p, "uniform lowp vec4 u_matspecular;\n"); // Specular coef is contained in alpha WRITE(p, "uniform lowp vec3 u_matemissive;\n"); + *uniformMask |= DIRTY_MATSPECULAR | DIRTY_MATEMISSIVE; } } - if (useHWTransform || !hasColor) + if (useHWTransform || !hasColor) { WRITE(p, "uniform lowp vec4 u_matambientalpha;\n"); // matambient + matalpha - + *uniformMask |= DIRTY_MATAMBIENTALPHA; + } if (enableFog) { WRITE(p, "uniform highp vec2 u_fogcoef;\n"); + *uniformMask |= DIRTY_FOGCOEF; } if (!isModeThrough && gstate_c.Supports(GPU_ROUND_DEPTH_TO_16BIT)) { WRITE(p, "uniform highp vec4 u_depthRange;\n"); + *uniformMask |= DIRTY_DEPTHRANGE; } WRITE(p, "%s%s lowp vec4 v_color0;\n", shading, varying); @@ -344,6 +372,8 @@ void GenerateVertexShader(const ShaderID &id, char *buffer) { // Hardware tessellation if (doBezier || doSpline) { + *uniformMask |= DIRTY_BEZIERSPLINE; + const char *sampler = !isAllowTexture1D ? "sampler2D" : "sampler1D"; WRITE(p, "uniform %s u_tess_pos_tex;\n", sampler); WRITE(p, "uniform %s u_tess_tex_tex;\n", sampler); diff --git a/GPU/GLES/VertexShaderGeneratorGLES.h b/GPU/GLES/VertexShaderGeneratorGLES.h index a513513d8c..eb70039a56 100644 --- a/GPU/GLES/VertexShaderGeneratorGLES.h +++ b/GPU/GLES/VertexShaderGeneratorGLES.h @@ -23,4 +23,4 @@ struct ShaderID; -void GenerateVertexShader(const ShaderID &id, char *buffer); +void GenerateVertexShader(const ShaderID &id, char *buffer, uint32_t *attrMask, uint64_t *uniformMask); diff --git a/GPU/Vulkan/DrawEngineVulkan.cpp b/GPU/Vulkan/DrawEngineVulkan.cpp index ff981317ec..88d0ed4b68 100644 --- a/GPU/Vulkan/DrawEngineVulkan.cpp +++ b/GPU/Vulkan/DrawEngineVulkan.cpp @@ -427,37 +427,7 @@ void DrawEngineVulkan::SubmitPrim(void *verts, void *inds, GEPrimitiveType prim, } } -int DrawEngineVulkan::ComputeNumVertsToDecode() const { - int vertsToDecode = 0; - if (drawCalls[0].indexType == GE_VTYPE_IDX_NONE >> GE_VTYPE_IDX_SHIFT) { - for (int i = 0; i < numDrawCalls; i++) { - const DeferredDrawCall &dc = drawCalls[i]; - vertsToDecode += dc.vertexCount; - } - } else { - // TODO: Share this computation with DecodeVertsStep? - for (int i = 0; i < numDrawCalls; i++) { - const DeferredDrawCall &dc = drawCalls[i]; - int lastMatch = i; - const int total = numDrawCalls; - int indexLowerBound = dc.indexLowerBound; - int indexUpperBound = dc.indexUpperBound; - for (int j = i + 1; j < total; ++j) { - if (drawCalls[j].verts != dc.verts) - break; - - indexLowerBound = std::min(indexLowerBound, (int)drawCalls[j].indexLowerBound); - indexUpperBound = std::max(indexUpperBound, (int)drawCalls[j].indexUpperBound); - lastMatch = j; - } - vertsToDecode += indexUpperBound - indexLowerBound + 1; - i = lastMatch; - } - } - return vertsToDecode; -} - -void DrawEngineVulkan::DecodeVerts(VulkanPushBuffer *push, uint32_t *bindOffset, VkBuffer *vkbuf) { +void DrawEngineVulkan::DecodeVertsToPushBuffer(VulkanPushBuffer *push, uint32_t *bindOffset, VkBuffer *vkbuf) { u8 *dest = decoded; // Figure out how much pushbuffer space we need to allocate. @@ -465,20 +435,7 @@ void DrawEngineVulkan::DecodeVerts(VulkanPushBuffer *push, uint32_t *bindOffset, int vertsToDecode = ComputeNumVertsToDecode(); dest = (u8 *)push->Push(vertsToDecode * dec_->GetDecVtxFmt().stride, bindOffset, vkbuf); } - - const UVScale origUV = gstate_c.uv; - for (; decodeCounter_ < numDrawCalls; decodeCounter_++) { - gstate_c.uv = uvScale[decodeCounter_]; - DecodeVertsStep(dest, decodeCounter_, decodedVerts_); // NOTE! DecodeVertsStep can modify decodeCounter_! - } - gstate_c.uv = origUV; - - // Sanity check - if (indexGen.Prim() < 0) { - ERROR_LOG_REPORT(G3D, "DecodeVerts: Failed to deduce prim: %i", indexGen.Prim()); - // Force to points (0) - indexGen.AddPrim(GE_PRIM_POINTS, 0); - } + DecodeVerts(dest); } void DrawEngineVulkan::SetLineWidth(float lineWidth) { @@ -706,7 +663,7 @@ void DrawEngineVulkan::DoFlush() { vai->minihash = ComputeMiniHash(); vai->status = VertexArrayInfoVulkan::VAI_HASHING; vai->drawsUntilNextFullHash = 0; - DecodeVerts(frame->pushVertex, &vbOffset, &vbuf); // writes to indexGen + DecodeVertsToPushBuffer(frame->pushVertex, &vbOffset, &vbuf); // writes to indexGen vai->numVerts = indexGen.VertexCount(); vai->prim = indexGen.Prim(); vai->maxIndex = indexGen.MaxIndex(); @@ -732,7 +689,7 @@ void DrawEngineVulkan::DoFlush() { } if (newMiniHash != vai->minihash || newHash != vai->hash) { MarkUnreliable(vai); - DecodeVerts(frame->pushVertex, &vbOffset, &vbuf); + DecodeVertsToPushBuffer(frame->pushVertex, &vbOffset, &vbuf); goto rotateVBO; } if (vai->numVerts > 64) { @@ -751,14 +708,14 @@ void DrawEngineVulkan::DoFlush() { u32 newMiniHash = ComputeMiniHash(); if (newMiniHash != vai->minihash) { MarkUnreliable(vai); - DecodeVerts(frame->pushVertex, &vbOffset, &vbuf); + DecodeVertsToPushBuffer(frame->pushVertex, &vbOffset, &vbuf); goto rotateVBO; } } if (!vai->vb) { // Directly push to the vertex cache. - DecodeVerts(vertexCache_, &vai->vbOffset, &vai->vb); + DecodeVertsToPushBuffer(vertexCache_, &vai->vbOffset, &vai->vb); _dbg_assert_msg_(G3D, gstate_c.vertBounds.minV >= gstate_c.vertBounds.maxV, "Should not have checked UVs when caching."); vai->numVerts = indexGen.VertexCount(); vai->prim = indexGen.Prim(); @@ -819,7 +776,7 @@ void DrawEngineVulkan::DoFlush() { if (vai->lastFrame != gpuStats.numFlips) { vai->numFrames++; } - DecodeVerts(frame->pushVertex, &vbOffset, &vbuf); + DecodeVertsToPushBuffer(frame->pushVertex, &vbOffset, &vbuf); goto rotateVBO; } default: @@ -833,7 +790,7 @@ void DrawEngineVulkan::DoFlush() { memcpy(dest, decoded, size); } else { // Decode directly into the pushbuffer - DecodeVerts(frame->pushVertex, &vbOffset, &vbuf); + DecodeVertsToPushBuffer(frame->pushVertex, &vbOffset, &vbuf); } rotateVBO: @@ -916,7 +873,7 @@ void DrawEngineVulkan::DoFlush() { } else { PROFILE_THIS_SCOPE("soft"); // Decode to "decoded" - DecodeVerts(nullptr, nullptr, nullptr); + DecodeVertsToPushBuffer(nullptr, nullptr, nullptr); bool hasColor = (lastVType_ & GE_VTYPE_COL_MASK) != GE_VTYPE_COL_NONE; if (gstate.isModeThrough()) { gstate_c.vertexFullAlpha = gstate_c.vertexFullAlpha && (hasColor || gstate.getMaterialAmbientA() == 255); diff --git a/GPU/Vulkan/DrawEngineVulkan.h b/GPU/Vulkan/DrawEngineVulkan.h index d33653d2ef..e6dc71e8f1 100644 --- a/GPU/Vulkan/DrawEngineVulkan.h +++ b/GPU/Vulkan/DrawEngineVulkan.h @@ -194,8 +194,7 @@ private: void InitDeviceObjects(); void DestroyDeviceObjects(); - int ComputeNumVertsToDecode() const; - void DecodeVerts(VulkanPushBuffer *push, uint32_t *bindOffset, VkBuffer *vkbuf); + void DecodeVertsToPushBuffer(VulkanPushBuffer *push, uint32_t *bindOffset, VkBuffer *vkbuf); void DoFlush(); void UpdateUBOs(FrameData *frame);