From 22b26ffc09d290c3fe6a6ae15424293691bbbf2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 23 Oct 2020 00:44:35 +0200 Subject: [PATCH] Time for the vertex shaders. Set up a test, start eliminating differences. --- GPU/Common/ShaderCommon.h | 15 + GPU/GLES/ShaderManagerGLES.cpp | 2 +- GPU/GLES/VertexShaderGeneratorGLES.cpp | 429 +++++++++++++-------- GPU/Vulkan/PipelineManagerVulkan.h | 14 +- GPU/Vulkan/ShaderManagerVulkan.cpp | 5 +- GPU/Vulkan/VertexShaderGeneratorVulkan.cpp | 1 + unittest/TestShaderGenerators.cpp | 105 +++-- 7 files changed, 349 insertions(+), 222 deletions(-) diff --git a/GPU/Common/ShaderCommon.h b/GPU/Common/ShaderCommon.h index b7eb56e93c..acc0de74f5 100644 --- a/GPU/Common/ShaderCommon.h +++ b/GPU/Common/ShaderCommon.h @@ -31,6 +31,8 @@ enum ShaderLanguage { HLSL_DX9, HLSL_D3D11, HLSL_D3D11_LEVEL9, + + TEST_GLSL_VULKAN, }; enum DebugShaderType { @@ -157,3 +159,16 @@ struct GLSLShaderCompat { void SetupForVulkan(); }; + +// PSP vertex format. +enum class PspAttributeLocation { + POSITION = 0, + TEXCOORD = 1, + NORMAL = 2, + W1 = 3, + W2 = 4, + COLOR0 = 5, + COLOR1 = 6, + + COUNT +}; diff --git a/GPU/GLES/ShaderManagerGLES.cpp b/GPU/GLES/ShaderManagerGLES.cpp index ccae98787a..4b903f9191 100644 --- a/GPU/GLES/ShaderManagerGLES.cpp +++ b/GPU/GLES/ShaderManagerGLES.cpp @@ -264,7 +264,7 @@ static void SetFloatUniform4(GLRenderManager *render, GLint *uniform, float data static void SetMatrix4x3(GLRenderManager *render, GLint *uniform, const float *m4x3) { float m4x4[16]; - ConvertMatrix4x3To4x4(m4x4, m4x3); + ConvertMatrix4x3To4x4Transposed(m4x4, m4x3); render->SetUniformM4x4(uniform, m4x4); } diff --git a/GPU/GLES/VertexShaderGeneratorGLES.cpp b/GPU/GLES/VertexShaderGeneratorGLES.cpp index b2fde269c8..2582853992 100644 --- a/GPU/GLES/VertexShaderGeneratorGLES.cpp +++ b/GPU/GLES/VertexShaderGeneratorGLES.cpp @@ -20,15 +20,15 @@ #include #include "Common/GPU/OpenGL/GLFeatures.h" - #include "Common/StringUtils.h" +#include "Core/Config.h" #include "GPU/ge_constants.h" #include "GPU/GPUState.h" -#include "Core/Config.h" +#include "GPU/Common/ShaderId.h" +#include "GPU/Common/ShaderUniforms.h" +#include "GPU/Common/VertexDecoderCommon.h" #include "GPU/GLES/VertexShaderGeneratorGLES.h" #include "GPU/GLES/ShaderManagerGLES.h" -#include "GPU/Common/ShaderId.h" -#include "GPU/Common/VertexDecoderCommon.h" #undef WRITE @@ -87,31 +87,52 @@ static const char * const boneWeightInDecl[9] = { // TODO: Skip all this if we can actually get a 16-bit depth buffer along with stencil, which // is a bit of a rare configuration, although quite common on mobile. +static const char * const boneWeightDecl[9] = { + "#ERROR#", + "layout(location = 3) in float w1;\n", + "layout(location = 3) in vec2 w1;\n", + "layout(location = 3) in vec3 w1;\n", + "layout(location = 3) in vec4 w1;\n", + "layout(location = 3) in vec4 w1;\nlayout(location = 4) in float w2;\n", + "layout(location = 3) in vec4 w1;\nlayout(location = 4) in vec2 w2;\n", + "layout(location = 3) in vec4 w1;\nlayout(location = 4) in vec3 w2;\n", + "layout(location = 3) in vec4 w1;\nlayout(location = 4) in vec4 w2;\n", +}; + +static const char *vulkan_glsl_preamble = +"#version 450\n" +"#extension GL_ARB_separate_shader_objects : enable\n" +"#extension GL_ARB_shading_language_420pack : enable\n" +"#define splat3(x) vec3(x)\n\n"; + bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const GLSLShaderCompat &compat, uint32_t *attrMask, uint64_t *uniformMask, std::string *errorString) { *attrMask = 0; *uniformMask = 0; - char *p = buffer; - WRITE(p, "#version %d%s\n", compat.glslVersionNumber, compat.gles ? " es" : ""); - bool highpFog = false; bool highpTexcoord = false; - if (compat.gles) { - // PowerVR needs highp to do the fog in MHU correctly. - // Others don't, and some can't handle highp in the fragment shader. - highpFog = (gl_extensions.bugs & BUG_PVR_SHADER_PRECISION_BAD) ? true : false; - highpTexcoord = highpFog; + + char *p = buffer; + if (compat.vulkan) { + WRITE(p, "%s", vulkan_glsl_preamble); + } else { + if (compat.gles) { + // PowerVR needs highp to do the fog in MHU correctly. + // Others don't, and some can't handle highp in the fragment shader. + highpFog = (gl_extensions.bugs & BUG_PVR_SHADER_PRECISION_BAD) ? true : false; + highpTexcoord = highpFog; + } + WRITE(p, "#version %d%s\n", compat.glslVersionNumber, compat.gles ? " es" : ""); + WRITE(p, "#define splat3(x) vec3(x)\n"); } - if (gl_extensions.EXT_gpu_shader4) { + if (!compat.vulkan && gl_extensions.EXT_gpu_shader4) { WRITE(p, "#extension GL_EXT_gpu_shader4 : enable\n"); } - WRITE(p, "#define splat3(x) vec3(x)\n"); - if (compat.gles) { WRITE(p, "precision highp float;\n"); - } else { + } else if (!compat.vulkan) { WRITE(p, "#define lowp\n"); WRITE(p, "#define mediump\n"); WRITE(p, "#define highp\n"); @@ -120,7 +141,7 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const GLSLShade bool isModeThrough = id.Bit(VS_BIT_IS_THROUGH); bool lmode = id.Bit(VS_BIT_LMODE); bool doTexture = id.Bit(VS_BIT_DO_TEXTURE); - bool doTextureProjection = id.Bit(VS_BIT_DO_TEXTURE_TRANSFORM); + bool doTextureTransform = id.Bit(VS_BIT_DO_TEXTURE_TRANSFORM); GETexMapMode uvGenMode = static_cast(id.Bits(VS_BIT_UVGEN_MODE, 2)); @@ -148,8 +169,17 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const GLSLShade bool hasNormalTess = id.Bit(VS_BIT_HAS_NORMAL_TESS); bool flipNormalTess = id.Bit(VS_BIT_NORM_REVERSE_TESS); + if (compat.vulkan) { + WRITE(p, "\n"); + WRITE(p, "layout (std140, set = 0, binding = 3) uniform baseVars {\n%s};\n", ub_baseStr); + if (enableLighting || doShadeMapping) + WRITE(p, "layout (std140, set = 0, binding = 4) uniform lightVars {\n%s};\n", ub_vs_lightsStr); + if (enableBones) + WRITE(p, "layout (std140, set = 0, binding = 5) uniform boneVars {\n%s};\n", ub_vs_bonesStr); + } + const char *shading = ""; - if (compat.glslES30) + if (compat.glslES30 || compat.vulkan) shading = doFlatShading ? "flat " : ""; DoLightComputation doLight[4] = { LIGHT_OFF, LIGHT_OFF, LIGHT_OFF, LIGHT_OFF }; @@ -166,157 +196,204 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const GLSLShade int numBoneWeights = 0; int boneWeightScale = id.Bits(VS_BIT_WEIGHT_FMTSCALE, 2); - if (enableBones) { - numBoneWeights = 1 + id.Bits(VS_BIT_BONES, 3); - const char * const * boneWeightDecl = boneWeightAttrDecl; - if (!strcmp(compat.attribute, "in")) { - boneWeightDecl = boneWeightInDecl; - } - WRITE(p, "%s", boneWeightDecl[numBoneWeights]); - *attrMask |= 1 << ATTR_W1; - if (numBoneWeights >= 5) - *attrMask |= 1 << ATTR_W2; - } - - if (useHWTransform) - WRITE(p, "%s vec3 position;\n", compat.attribute); - else - WRITE(p, "%s vec4 position;\n", compat.attribute); // need to pass the fog coord in w - *attrMask |= 1 << ATTR_POSITION; - - if (useHWTransform && hasNormal) { - WRITE(p, "%s mediump vec3 normal;\n", compat.attribute); - *attrMask |= 1 << ATTR_NORMAL; - } - bool texcoordVec3In = false; - if (doTexture && hasTexcoord) { - if (!useHWTransform && doTextureProjection && !isModeThrough) { - WRITE(p, "%s vec3 texcoord;\n", compat.attribute); - texcoordVec3In = true; - } else { - WRITE(p, "%s vec2 texcoord;\n", compat.attribute); - } - *attrMask |= 1 << ATTR_TEXCOORD; - } - if (hasColor) { - WRITE(p, "%s lowp vec4 color0;\n", compat.attribute); - *attrMask |= 1 << ATTR_COLOR0; - if (lmode && !useHWTransform) { // only software transform supplies color1 as vertex data - WRITE(p, "%s lowp vec3 color1;\n", compat.attribute); - *attrMask |= 1 << ATTR_COLOR1; - } - } + bool scaleUV = false; - 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. - } - - bool scaleUV = !isModeThrough && (uvGenMode == GE_TEXMAP_TEXTURE_COORDS || uvGenMode == GE_TEXMAP_UNKNOWN); - - if (useHWTransform) { - // 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"); - *uniformMask |= DIRTY_WORLDMATRIX | DIRTY_VIEWMATRIX; - if (doTextureProjection) { - WRITE(p, "uniform mediump mat4 u_texmtx;\n"); - *uniformMask |= DIRTY_TEXMATRIX; - } + if (compat.vulkan) { 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 + numBoneWeights = 1 + id.Bits(VS_BIT_BONES, 3); + WRITE(p, "%s", boneWeightDecl[numBoneWeights]); } + + if (useHWTransform) + WRITE(p, "layout (location = %d) in vec3 position;\n", (int)PspAttributeLocation::POSITION); + else + // we pass the fog coord in w + WRITE(p, "layout (location = %d) in vec4 position;\n", (int)PspAttributeLocation::POSITION); + + if (useHWTransform && hasNormal) + WRITE(p, "layout (location = %d) in vec3 normal;\n", (int)PspAttributeLocation::NORMAL); + + bool texcoordInVec3 = false; + if (doTexture && hasTexcoord) { + if (!useHWTransform && doTextureTransform && !isModeThrough) { + WRITE(p, "layout (location = %d) in vec3 texcoord;\n", (int)PspAttributeLocation::TEXCOORD); + texcoordInVec3 = true; + } else + WRITE(p, "layout (location = %d) in vec2 texcoord;\n", (int)PspAttributeLocation::TEXCOORD); + } + if (hasColor) { + WRITE(p, "layout (location = %d) in vec4 color0;\n", (int)PspAttributeLocation::COLOR0); + if (lmode && !useHWTransform) // only software transform supplies color1 as vertex data + WRITE(p, "layout (location = %d) in vec3 color1;\n", (int)PspAttributeLocation::COLOR1); + } + + WRITE(p, "layout (location = 1) %sout vec4 v_color0;\n", shading); + if (lmode) { + WRITE(p, "layout (location = 2) %sout vec3 v_color1;\n", shading); + } + if (doTexture) { - WRITE(p, "uniform vec4 u_uvscaleoffset;\n"); - *uniformMask |= DIRTY_UVSCALEOFFSET; + WRITE(p, "layout (location = 0) out vec3 v_texcoord;\n"); } - 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 (enableFog) { + // See the fragment shader generator + WRITE(p, "layout (location = 3) out float v_fogdepth;\n"); + } + } else { + if (enableBones) { + numBoneWeights = 1 + id.Bits(VS_BIT_BONES, 3); + const char * const * boneWeightDecl = boneWeightAttrDecl; + if (!strcmp(compat.attribute, "in")) { + boneWeightDecl = boneWeightInDecl; } - if (doLight[i] == LIGHT_FULL) { - *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)); + WRITE(p, "%s", boneWeightDecl[numBoneWeights]); + *attrMask |= 1 << ATTR_W1; + if (numBoneWeights >= 5) + *attrMask |= 1 << ATTR_W2; + } - if (type != GE_LIGHTTYPE_DIRECTIONAL) - WRITE(p, "uniform mediump vec3 u_lightatt%i;\n", i); + if (useHWTransform) + WRITE(p, "%s vec3 position;\n", compat.attribute); + else + WRITE(p, "%s vec4 position;\n", compat.attribute); // need to pass the fog coord in w + *attrMask |= 1 << ATTR_POSITION; - if (type == GE_LIGHTTYPE_SPOT || type == GE_LIGHTTYPE_UNKNOWN) { - WRITE(p, "uniform mediump vec3 u_lightdir%i;\n", i); - WRITE(p, "uniform mediump vec2 u_lightangle_spotCoef%i;\n", i); - } - WRITE(p, "uniform lowp vec3 u_lightambient%i;\n", i); - WRITE(p, "uniform lowp vec3 u_lightdiffuse%i;\n", i); + if (useHWTransform && hasNormal) { + WRITE(p, "%s mediump vec3 normal;\n", compat.attribute); + *attrMask |= 1 << ATTR_NORMAL; + } - if (comp == GE_LIGHTCOMP_BOTH) { - WRITE(p, "uniform lowp vec3 u_lightspecular%i;\n", i); - } + if (doTexture && hasTexcoord) { + if (!useHWTransform && doTextureTransform && !isModeThrough) { + WRITE(p, "%s vec3 texcoord;\n", compat.attribute); + texcoordVec3In = true; + } else { + WRITE(p, "%s vec2 texcoord;\n", compat.attribute); + } + *attrMask |= 1 << ATTR_TEXCOORD; + } + if (hasColor) { + WRITE(p, "%s lowp vec4 color0;\n", compat.attribute); + *attrMask |= 1 << ATTR_COLOR0; + if (lmode && !useHWTransform) { // only software transform supplies color1 as vertex data + WRITE(p, "%s lowp vec3 color1;\n", compat.attribute); + *attrMask |= 1 << ATTR_COLOR1; } } - if (enableLighting) { - WRITE(p, "uniform lowp vec4 u_ambient;\n"); - *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) { - 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; - } - - if (!isModeThrough) { - WRITE(p, "uniform highp vec4 u_cullRangeMin;\n"); - WRITE(p, "uniform highp vec4 u_cullRangeMax;\n"); - *uniformMask |= DIRTY_CULLRANGE; - } - - WRITE(p, "%s%s lowp vec4 v_color0;\n", shading, compat.varying_vs); - if (lmode) { - WRITE(p, "%s%s lowp vec3 v_color1;\n", shading, compat.varying_vs); - } - - if (doTexture) { - WRITE(p, "%s %s vec3 v_texcoord;\n", compat.varying_vs, highpTexcoord ? "highp" : "mediump"); - } - - if (enableFog) { - // See the fragment shader generator - if (highpFog) { - WRITE(p, "%s highp float v_fogdepth;\n", compat.varying_vs); + if (isModeThrough) { + WRITE(p, "uniform mat4 u_proj_through;\n"); + *uniformMask |= DIRTY_PROJTHROUGHMATRIX; } else { - WRITE(p, "%s mediump float v_fogdepth;\n", compat.varying_vs); + WRITE(p, "uniform mat4 u_proj;\n"); + *uniformMask |= DIRTY_PROJMATRIX; + // Add all the uniforms we'll need to transform properly. + } + + scaleUV = !isModeThrough && (uvGenMode == GE_TEXMAP_TEXTURE_COORDS || uvGenMode == GE_TEXMAP_UNKNOWN); + + if (useHWTransform) { + // When transforming by hardware, we need a great deal more uniforms... + // TODO: Use 4x3 matrices where possible. Though probably doesn't matter much. + WRITE(p, "uniform mat4 u_world;\n"); + WRITE(p, "uniform mat4 u_view;\n"); + *uniformMask |= DIRTY_WORLDMATRIX | DIRTY_VIEWMATRIX; + if (doTextureTransform) { + 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) { + *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); + + if (type == GE_LIGHTTYPE_SPOT || type == GE_LIGHTTYPE_UNKNOWN) { + WRITE(p, "uniform mediump vec3 u_lightdir%i;\n", i); + WRITE(p, "uniform mediump vec2 u_lightangle_spotCoef%i;\n", i); + } + WRITE(p, "uniform lowp vec3 u_lightambient%i;\n", i); + WRITE(p, "uniform lowp vec3 u_lightdiffuse%i;\n", i); + + if (comp == GE_LIGHTCOMP_BOTH) { + WRITE(p, "uniform lowp vec3 u_lightspecular%i;\n", i); + } + } + } + if (enableLighting) { + WRITE(p, "uniform lowp vec4 u_ambient;\n"); + *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) { + 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; + } + + if (!isModeThrough) { + WRITE(p, "uniform highp vec4 u_cullRangeMin;\n"); + WRITE(p, "uniform highp vec4 u_cullRangeMax;\n"); + *uniformMask |= DIRTY_CULLRANGE; + } + + WRITE(p, "%s%s lowp vec4 v_color0;\n", shading, compat.varying_vs); + if (lmode) { + WRITE(p, "%s%s lowp vec3 v_color1;\n", shading, compat.varying_vs); + } + + if (doTexture) { + WRITE(p, "%s %s vec3 v_texcoord;\n", compat.varying_vs, highpTexcoord ? "highp" : "mediump"); + } + + if (enableFog) { + // See the fragment shader generator + if (highpFog) { + WRITE(p, "%s highp float v_fogdepth;\n", compat.varying_vs); + } else { + WRITE(p, "%s mediump float v_fogdepth;\n", compat.varying_vs); + } } } @@ -336,11 +413,33 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const GLSLShade if (doBezier || doSpline) { *uniformMask |= DIRTY_BEZIERSPLINE; - WRITE(p, "uniform sampler2D u_tess_points;\n"); // Control Points - WRITE(p, "uniform sampler2D u_tess_weights_u;\n"); - WRITE(p, "uniform sampler2D u_tess_weights_v;\n"); + if (compat.vulkan) { + WRITE(p, "struct TessData {\n"); + WRITE(p, " vec4 pos;\n"); + WRITE(p, " vec4 uv;\n"); + WRITE(p, " vec4 color;\n"); + WRITE(p, "};\n"); + WRITE(p, "layout (std430, set = 0, binding = 6) readonly buffer s_tess_data {\n"); + WRITE(p, " TessData data[];\n"); + WRITE(p, "} tess_data;\n"); - WRITE(p, "uniform int u_spline_counts;\n"); + WRITE(p, "layout (std430) struct TessWeight {\n"); + WRITE(p, " vec4 basis;\n"); + WRITE(p, " vec4 deriv;\n"); + WRITE(p, "};\n"); + WRITE(p, "layout (std430, set = 0, binding = 7) readonly buffer s_tess_weights_u {\n"); + WRITE(p, " TessWeight data[];\n"); + WRITE(p, "} tess_weights_u;\n"); + WRITE(p, "layout (std430, set = 0, binding = 8) readonly buffer s_tess_weights_v {\n"); + WRITE(p, " TessWeight data[];\n"); + WRITE(p, "} tess_weights_v;\n"); + } else { + WRITE(p, "uniform sampler2D u_tess_points;\n"); // Control Points + WRITE(p, "uniform sampler2D u_tess_weights_u;\n"); + WRITE(p, "uniform sampler2D u_tess_weights_v;\n"); + + WRITE(p, "uniform int u_spline_counts;\n"); + } for (int i = 2; i <= 4; i++) { // Define 3 types vec2, vec3, vec4 @@ -461,17 +560,17 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const GLSLShade WRITE(p, " Tess tess;\n"); WRITE(p, " tessellate(tess);\n"); - WRITE(p, " vec3 worldpos = (u_world * vec4(tess.pos.xyz, 1.0)).xyz;\n"); + WRITE(p, " vec3 worldpos = (vec4(tess.pos.xyz, 1.0) * u_world).xyz;\n"); if (hasNormalTess) { - WRITE(p, " mediump vec3 worldnormal = normalize((u_world * vec4(%stess.nrm, 0.0)).xyz);\n", flipNormalTess ? "-" : ""); + WRITE(p, " mediump vec3 worldnormal = normalize((vec4(%stess.nrm, 0.0) * u_world).xyz);\n", flipNormalTess ? "-" : ""); } else { WRITE(p, " mediump vec3 worldnormal = vec3(0.0, 0.0, 1.0);\n"); } } else { // No skinning, just standard T&L. - WRITE(p, " vec3 worldpos = (u_world * vec4(position.xyz, 1.0)).xyz;\n"); + WRITE(p, " vec3 worldpos = (vec4(position.xyz, 1.0) * u_world).xyz;\n"); if (hasNormal) - WRITE(p, " mediump vec3 worldnormal = normalize((u_world * vec4(%snormal, 0.0)).xyz);\n", flipNormal ? "-" : ""); + WRITE(p, " mediump vec3 worldnormal = normalize((vec4(%snormal, 0.0) * u_world).xyz);\n", flipNormal ? "-" : ""); else WRITE(p, " mediump vec3 worldnormal = vec3(0.0, 0.0, 1.0);\n"); } @@ -526,7 +625,7 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const GLSLShade WRITE(p, " mediump vec3 worldnormal = normalize((u_world * vec4(skinnednormal, 0.0)).xyz);\n"); } - WRITE(p, " vec4 viewPos = u_view * vec4(worldpos, 1.0);\n"); + WRITE(p, " vec4 viewPos = vec4(worldpos, 1.0) * u_view;\n"); // Final view and projection transforms. if (gstate_c.Supports(GPU_ROUND_DEPTH_TO_16BIT)) { @@ -742,7 +841,7 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const GLSLShade break; } // Transform by texture matrix. XYZ as we are doing projection mapping. - WRITE(p, " v_texcoord = (u_texmtx * %s).xyz * vec3(u_uvscaleoffset.xy, 1.0);\n", temp_tc.c_str()); + WRITE(p, " v_texcoord = (%s * u_texmtx).xyz * vec3(u_uvscaleoffset.xy, 1.0);\n", temp_tc.c_str()); } break; diff --git a/GPU/Vulkan/PipelineManagerVulkan.h b/GPU/Vulkan/PipelineManagerVulkan.h index bb747bd29b..8646d5b8bf 100644 --- a/GPU/Vulkan/PipelineManagerVulkan.h +++ b/GPU/Vulkan/PipelineManagerVulkan.h @@ -22,24 +22,12 @@ #include "GPU/Common/VertexDecoderCommon.h" #include "GPU/Common/ShaderId.h" +#include "GPU/Common/ShaderCommon.h" #include "GPU/Vulkan/VulkanUtil.h" #include "GPU/Vulkan/StateMappingVulkan.h" #include "GPU/Vulkan/VulkanQueueRunner.h" -// PSP vertex format. -enum class PspAttributeLocation { - POSITION = 0, - TEXCOORD = 1, - NORMAL = 2, - W1 = 3, - W2 = 4, - COLOR0 = 5, - COLOR1 = 6, - - COUNT -}; - struct VulkanPipelineKey { VulkanPipelineRasterStateKey raster; // prim is included here VkRenderPass renderPass; diff --git a/GPU/Vulkan/ShaderManagerVulkan.cpp b/GPU/Vulkan/ShaderManagerVulkan.cpp index 8d20b9ca58..2624db88e4 100644 --- a/GPU/Vulkan/ShaderManagerVulkan.cpp +++ b/GPU/Vulkan/ShaderManagerVulkan.cpp @@ -387,6 +387,8 @@ bool ShaderManagerVulkan::LoadCache(FILE *f) { if (header.featureFlags != gstate_c.featureFlags) return false; + GLSLShaderCompat compat{}; + compat.SetupForVulkan(); for (int i = 0; i < header.numVertexShaders; i++) { VShaderID id; if (fread(&id, sizeof(id), 1, f) != 1) { @@ -402,8 +404,7 @@ bool ShaderManagerVulkan::LoadCache(FILE *f) { vsCache_.Insert(id, vs); } uint32_t vendorID = vulkan_->GetPhysicalDeviceProperties().properties.vendorID; - GLSLShaderCompat compat{}; - compat.SetupForVulkan(); + for (int i = 0; i < header.numFragmentShaders; i++) { FShaderID id; if (fread(&id, sizeof(id), 1, f) != 1) { diff --git a/GPU/Vulkan/VertexShaderGeneratorVulkan.cpp b/GPU/Vulkan/VertexShaderGeneratorVulkan.cpp index f18ea66729..76a613b35d 100644 --- a/GPU/Vulkan/VertexShaderGeneratorVulkan.cpp +++ b/GPU/Vulkan/VertexShaderGeneratorVulkan.cpp @@ -205,6 +205,7 @@ bool GenerateVertexShaderVulkanGLSL(const VShaderID &id, char *buffer, std::stri WRITE(p, "}\n\n"); } + // Hardware tessellation if (doBezier || doSpline) { WRITE(p, "struct TessData {\n"); WRITE(p, " vec4 pos;\n"); diff --git a/unittest/TestShaderGenerators.cpp b/unittest/TestShaderGenerators.cpp index 2c4ffa936b..88b41f3536 100644 --- a/unittest/TestShaderGenerators.cpp +++ b/unittest/TestShaderGenerators.cpp @@ -41,6 +41,8 @@ bool GenerateFShader(FShaderID id, char *buffer, ShaderLanguage lang, std::strin case ShaderLanguage::GLSL_300: // TODO: Need a device - except that maybe glslang could be used to verify these .... return false; + case ShaderLanguage::TEST_GLSL_VULKAN: + return false; default: return false; } @@ -57,6 +59,14 @@ bool GenerateVShader(VShaderID id, char *buffer, ShaderLanguage lang, std::strin // return DX9::GenerateFragmentShaderHLSL(id, buffer, ShaderLanguage::HLSL_DX9); case ShaderLanguage::GLSL_VULKAN: return GenerateVertexShaderVulkanGLSL(id, buffer, errorString); + case ShaderLanguage::TEST_GLSL_VULKAN: + { + GLSLShaderCompat compat{}; + compat.SetupForVulkan(); + uint32_t attrMask; + uint64_t uniformMask; + return GenerateVertexShaderGLSL(id, buffer, compat, &attrMask, &uniformMask, errorString); + } default: return false; } @@ -124,6 +134,7 @@ bool TestShaderGenerators() { LoadD3DCompilerDynamic(); ShaderLanguage languages[] = { + ShaderLanguage::TEST_GLSL_VULKAN, ShaderLanguage::GLSL_VULKAN, ShaderLanguage::HLSL_D3D11, ShaderLanguage::GLSL_140, @@ -141,6 +152,59 @@ bool TestShaderGenerators() { int successes = 0; int count = 700; + // Generate a bunch of random vertex shader IDs, try to generate shader source. + // Then compile it and check that it's ok. + for (int i = 0; i < count; i++) { + uint32_t bottom = rng.R32(); + uint32_t top = rng.R32(); + VShaderID id; + id.d[0] = bottom; + id.d[1] = top; + + // Skip testing beziers for now. I'll deal with those bugs later. + id.SetBit(VS_BIT_BEZIER, false); + id.SetBit(VS_BIT_SPLINE, false); + + bool generateSuccess[numLanguages]{}; + std::string genErrorString[numLanguages]; + + for (int j = 0; j < numLanguages; j++) { + generateSuccess[j] = GenerateVShader(id, buffer[j], languages[j], &genErrorString[j]); + if (!genErrorString[j].empty()) { + printf("%s\n", genErrorString[j].c_str()); + } + } + + if (generateSuccess[0] != generateSuccess[1]) { + printf("mismatching success! %s %s\n", genErrorString[0].c_str(), genErrorString[1].c_str()); + printf("%s\n", buffer[0]); + printf("%s\n", buffer[1]); + return 1; + } + if (generateSuccess[0] && strcmp(buffer[0], buffer[1])) { + printf("mismatching shaders!\n"); + PrintDiff(buffer[0], buffer[1]); + return 1; + } + + // Now that we have the strings ready for easy comparison (buffer,4 in the watch window), + // let's try to compile them. + for (int j = 0; j < numLanguages; j++) { + if (generateSuccess[j]) { + if (!TestCompileShader(buffer[j], languages[j], true)) { + printf("Error compiling vertex shader:\n\n%s\n\n", LineNumberString(buffer[j]).c_str()); + return false; + } + successes++; + } + } + } + + printf("%d/%d vertex shaders generated (it's normal that it's not all, there are invalid bit combos)\n", successes, count * numLanguages); + + successes = 0; + count = 200; + // Generate a bunch of random fragment shader IDs, try to generate shader source. // Then compile it and check that it's ok. for (int i = 0; i < count; i++) { @@ -194,47 +258,6 @@ bool TestShaderGenerators() { successes = 0; count = 200; - // Generate a bunch of random vertex shader IDs, try to generate shader source. - // Then compile it and check that it's ok. - for (int i = 0; i < count; i++) { - uint32_t bottom = rng.R32(); - uint32_t top = rng.R32(); - VShaderID id; - id.d[0] = bottom; - id.d[1] = top; - - // Skip testing beziers for now. I'll deal with those bugs later. - id.SetBit(VS_BIT_BEZIER, false); - id.SetBit(VS_BIT_SPLINE, false); - - bool generateSuccess[numLanguages]{}; - - for (int j = 0; j < numLanguages; j++) { - std::string genErrorString; - generateSuccess[j] = GenerateVShader(id, buffer[j], languages[j], &genErrorString); - if (!genErrorString.empty()) { - printf("%s\n", genErrorString.c_str()); - } - } - - // Now that we have the strings ready for easy comparison (buffer,4 in the watch window), - // let's try to compile them. - for (int j = 0; j < numLanguages; j++) { - if (generateSuccess[j]) { - if (!TestCompileShader(buffer[j], languages[j], true)) { - printf("Error compiling vertex shader:\n\n%s\n\n", LineNumberString(buffer[j]).c_str()); - return false; - } - successes++; - } - } - } - - printf("%d/%d vertex shaders generated (it's normal that it's not all, there are invalid bit combos)\n", successes, count * numLanguages); - - successes = 0; - count = 200; - for (int i = 0; i < numLanguages; i++) { delete[] buffer[i]; }