diff --git a/CMakeLists.txt b/CMakeLists.txt index 2164c02293..dad85680cb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1250,8 +1250,6 @@ set(GPU_GLES GPU/GLES/TextureScalerGLES.h GPU/GLES/DrawEngineGLES.cpp GPU/GLES/DrawEngineGLES.h - GPU/GLES/VertexShaderGeneratorGLES.cpp - GPU/GLES/VertexShaderGeneratorGLES.h ) set(GPU_VULKAN @@ -1299,7 +1297,7 @@ set(GPU_D3D9 GPU/Directx9/TextureScalerDX9.cpp GPU/Directx9/TextureScalerDX9.h GPU/Directx9/VertexShaderGeneratorHLSL.cpp - GPU/Directx9/VertexShaderGeneratorHLSL.h + GPU/GLES/VertexShaderGeneratorGLES.h ) set(GPU_D3D11 @@ -1340,6 +1338,8 @@ set(GPU_SOURCES GPU/Common/DepalettizeShaderCommon.h GPU/Common/FragmentShaderGenerator.cpp GPU/Common/FragmentShaderGenerator.h + GPU/Common/VertexShaderGenerator.cpp + GPU/Common/VertexShaderGenerator.h GPU/Common/FramebufferManagerCommon.cpp GPU/Common/FramebufferManagerCommon.h GPU/Common/GPUDebugInterface.cpp diff --git a/GPU/Common/FragmentShaderGenerator.cpp b/GPU/Common/FragmentShaderGenerator.cpp index f593c82115..bb51a14c36 100644 --- a/GPU/Common/FragmentShaderGenerator.cpp +++ b/GPU/Common/FragmentShaderGenerator.cpp @@ -51,6 +51,9 @@ const char *hlsl_preamble_fs = "#define vec4 float4\n" "#define uvec3 uint3\n" "#define ivec3 int3\n" +"#define ivec4 int4\n" +"#define mat4 float4x4\n" +"#define mat3x4 float4x3\n" // note how the conventions are backwards "#define splat3(x) float3(x, x, x)\n" "#define mix lerp\n" "#define mod(x, y) fmod(x, y)\n"; @@ -246,7 +249,7 @@ bool GenerateFragmentShader(const FShaderID &id, char *buffer, const ShaderLangu WRITE(p, "Texture2D fboTex : register(t1);\n"); } } - WRITE(p, "cbuffer base : register(b0) {\n%s};\n", cb_baseStr); + WRITE(p, "cbuffer base : register(b0) {\n%s};\n", ub_baseStr); } if (enableAlphaTest) { diff --git a/GPU/Common/ShaderCommon.cpp b/GPU/Common/ShaderCommon.cpp index 43cd7c1a17..1f13458b93 100644 --- a/GPU/Common/ShaderCommon.cpp +++ b/GPU/Common/ShaderCommon.cpp @@ -171,6 +171,7 @@ ShaderLanguageDesc::ShaderLanguageDesc(ShaderLanguage lang) { texelFetch = "texelFetch"; forceMatrix4x4 = false; coefsFromBuffers = true; + vsOutPrefix = "Out."; break; } } diff --git a/GPU/Common/ShaderCommon.h b/GPU/Common/ShaderCommon.h index c2e77563ad..34594dfba7 100644 --- a/GPU/Common/ShaderCommon.h +++ b/GPU/Common/ShaderCommon.h @@ -157,6 +157,7 @@ struct ShaderLanguageDesc { const char *texelFetch = nullptr; const char *lastFragData = nullptr; const char *framebufferFetchExtension = nullptr; + const char *vsOutPrefix = ""; bool glslES30 = false; bool bitwiseOps = false; bool forceMatrix4x4 = false; @@ -175,3 +176,16 @@ enum class PspAttributeLocation { COUNT }; + +// Pre-fetched attrs and uniforms (used by GL only). +enum { + ATTR_POSITION = 0, + ATTR_TEXCOORD = 1, + ATTR_NORMAL = 2, + ATTR_W1 = 3, + ATTR_W2 = 4, + ATTR_COLOR0 = 5, + ATTR_COLOR1 = 6, + + ATTR_COUNT, +}; diff --git a/GPU/Common/ShaderUniforms.h b/GPU/Common/ShaderUniforms.h index fd13956eec..a2ecf4a71b 100644 --- a/GPU/Common/ShaderUniforms.h +++ b/GPU/Common/ShaderUniforms.h @@ -72,34 +72,6 @@ R"( mat4 u_proj; vec2 u_texclampoff; )"; -// HLSL code is shared so these names are changed to match those in DX9. -static const char *cb_baseStr = -R"( float4x4 u_proj; - float4x4 u_proj_through; - float4x3 u_view; - float4x3 u_world; - float4x3 u_texmtx; - float4 u_uvscaleoffset; - float4 u_depthRange; - float2 u_fogcoef; - float u_stencilReplaceValue; - float4 u_matambientalpha; - uint u_spline_counts; - uint u_depal_mask_shift_off_fmt; - int pad2; - int pad3; - float4 u_cullRangeMin; - float4 u_cullRangeMax; - float3 u_fogcolor; - float3 u_texenv; - uint4 u_alphacolorref; - uint4 u_alphacolormask; - float3 u_blendFixA; - float3 u_blendFixB; - float4 u_texclamp; - float2 u_texclampoff; -)"; - // 512 bytes. Would like to shrink more. Some colors only have 8-bit precision and we expand // them to float unnecessarily, could just as well expand in the shader. struct UB_VS_Lights { @@ -151,42 +123,6 @@ R"( vec4 u_ambient; vec3 u_lightspecular3; )"; -// HLSL code is shared so these names are changed to match those in DX9. -static const char *cb_vs_lightsStr = -R"( float4 u_ambient; - float3 u_matdiffuse; - float4 u_matspecular; - float3 u_matemissive; - float3 u_lightpos0; - float3 u_lightpos1; - float3 u_lightpos2; - float3 u_lightpos3; - float3 u_lightdir0; - float3 u_lightdir1; - float3 u_lightdir2; - float3 u_lightdir3; - float3 u_lightatt0; - float3 u_lightatt1; - float3 u_lightatt2; - float3 u_lightatt3; - float4 u_lightangle_spotCoef0; - float4 u_lightangle_spotCoef1; - float4 u_lightangle_spotCoef2; - float4 u_lightangle_spotCoef3; - float3 u_lightambient0; - float3 u_lightambient1; - float3 u_lightambient2; - float3 u_lightambient3; - float3 u_lightdiffuse0; - float3 u_lightdiffuse1; - float3 u_lightdiffuse2; - float3 u_lightdiffuse3; - float3 u_lightspecular0; - float3 u_lightspecular1; - float3 u_lightspecular2; - float3 u_lightspecular3; -)"; - // With some cleverness, we could get away with uploading just half this when only the four or five first // bones are being used. This is 384b. struct UB_VS_Bones { @@ -197,10 +133,6 @@ static const char *ub_vs_bonesStr = R"( mat3x4 u_bone0; mat3x4 u_bone1; mat3x4 u_bone2; mat3x4 u_bone3; mat3x4 u_bone4; mat3x4 u_bone5; mat3x4 u_bone6; mat3x4 u_bone7; mat3x4 u_bone8; )"; -static const char *cb_vs_bonesStr = -R"( float4x3 u_bone[8]; -)"; - void CalcCullRange(float minValues[4], float maxValues[4], bool flipViewport, bool hasNegZ); void BaseUpdateUniforms(UB_VS_FS_Base *ub, uint64_t dirtyUniforms, bool flipViewport, bool useBufferedRendering); diff --git a/GPU/GLES/VertexShaderGeneratorGLES.cpp b/GPU/Common/VertexShaderGenerator.cpp similarity index 65% rename from GPU/GLES/VertexShaderGeneratorGLES.cpp rename to GPU/Common/VertexShaderGenerator.cpp index e412f50232..f2116cd105 100644 --- a/GPU/GLES/VertexShaderGeneratorGLES.cpp +++ b/GPU/Common/VertexShaderGenerator.cpp @@ -27,8 +27,7 @@ #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/VertexShaderGenerator.h" #undef WRITE @@ -58,6 +57,30 @@ static const char * const boneWeightInDecl[9] = { "in mediump vec4 w1, w2;\n", }; +const char *boneWeightAttrDeclHLSL[9] = { + "#ERROR boneWeightAttrDecl#\n", + "float a_w1:TEXCOORD1;\n", + "vec2 a_w1:TEXCOORD1;\n", + "vec3 a_w1:TEXCOORD1;\n", + "vec4 a_w1:TEXCOORD1;\n", + "vec4 a_w1:TEXCOORD1;\n float a_w2:TEXCOORD2;\n", + "vec4 a_w1:TEXCOORD1;\n vec2 a_w2:TEXCOORD2;\n", + "vec4 a_w1:TEXCOORD1;\n vec3 a_w2:TEXCOORD2;\n", + "vec4 a_w1:TEXCOORD1;\n vec4 a_w2:TEXCOORD2;\n", +}; + +const char *boneWeightAttrInitHLSL[9] = { + " #ERROR#\n", + " vec4 w1 = vec4(In.a_w1, 0.0, 0.0, 0.0);\n", + " vec4 w1 = vec4(In.a_w1.xy, 0.0, 0.0);\n", + " vec4 w1 = vec4(In.a_w1.xyz, 0.0);\n", + " vec4 w1 = In.a_w1;\n", + " vec4 w1 = In.a_w1;\n vec4 w2 = vec4(In.a_w2, 0.0, 0.0, 0.0);\n", + " vec4 w1 = In.a_w1;\n vec4 w2 = vec4(In.a_w2.xy, 0.0, 0.0);\n", + " vec4 w1 = In.a_w1;\n vec4 w2 = vec4(In.a_w2.xyz, 0.0);\n", + " vec4 w1 = In.a_w1;\n vec4 w2 = In.a_w2;\n", +}; + // Depth range and viewport // // After the multiplication with the projection matrix, we have a 4D vector in clip space. @@ -103,9 +126,28 @@ const char *vulkan_glsl_preamble_vs = "#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"; +"#define mul(x, y) ((x) * (y))\n" +"#define splat3(x) vec3(x)\n" +"#define lowp\n" +"#define mediump\n" +"#define highp\n" +"\n"; -bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLanguageDesc &compat, uint32_t *attrMask, uint64_t *uniformMask, std::string *errorString) { +const char *hlsl_preamble_vs = +"#define vec2 float2\n" +"#define vec3 float3\n" +"#define vec4 float4\n" +"#define ivec2 int2\n" +"#define ivec4 int4\n" +"#define mat4 float4x4\n" +"#define mat3x4 float4x3\n" // note how the conventions are backwards +"#define splat3(x) vec3(x, x, x)\n" +"#define lowp\n" +"#define mediump\n" +"#define highp\n" +"\n"; + +bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguageDesc &compat, uint32_t *attrMask, uint64_t *uniformMask, std::string *errorString) { *attrMask = 0; *uniformMask = 0; @@ -115,7 +157,9 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan char *p = buffer; if (compat.shaderLanguage == GLSL_VULKAN) { WRITE(p, "%s", vulkan_glsl_preamble_vs); - } else { + } else if (compat.shaderLanguage == HLSL_D3D11 || compat.shaderLanguage == HLSL_D3D9) { + WRITE(p, "%s", hlsl_preamble_vs); + } else if (ShaderLanguageIsOpenGL(compat.shaderLanguage)) { 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. @@ -123,19 +167,18 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan highpTexcoord = highpFog; } WRITE(p, "#version %d%s\n", compat.glslVersionNumber, compat.gles && compat.glslES30 ? " es" : ""); + if (gl_extensions.EXT_gpu_shader4) { + WRITE(p, "#extension GL_EXT_gpu_shader4 : enable\n"); + } + if (compat.gles) { + WRITE(p, "precision highp float;\n"); + } else { + WRITE(p, "#define lowp\n"); + WRITE(p, "#define mediump\n"); + WRITE(p, "#define highp\n"); + } WRITE(p, "#define splat3(x) vec3(x)\n"); - } - - if (ShaderLanguageIsOpenGL(compat.shaderLanguage) && gl_extensions.EXT_gpu_shader4) { - WRITE(p, "#extension GL_EXT_gpu_shader4 : enable\n"); - } - - if (compat.gles) { - WRITE(p, "precision highp float;\n"); - } else if (compat.shaderLanguage != GLSL_VULKAN) { - WRITE(p, "#define lowp\n"); - WRITE(p, "#define mediump\n"); - WRITE(p, "#define highp\n"); + WRITE(p, "#define mul(x, y) ((x) * (y))\n"); } bool isModeThrough = id.Bit(VS_BIT_IS_THROUGH); @@ -158,32 +201,29 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan bool flipNormal = id.Bit(VS_BIT_NORM_REVERSE); int ls0 = id.Bits(VS_BIT_LS0, 2); int ls1 = id.Bits(VS_BIT_LS1, 2); - bool enableBones = id.Bit(VS_BIT_ENABLE_BONES); + bool enableBones = id.Bit(VS_BIT_ENABLE_BONES) && useHWTransform; bool enableLighting = id.Bit(VS_BIT_LIGHTING_ENABLE); int matUpdate = id.Bits(VS_BIT_MATERIAL_UPDATE, 3); // Apparently we don't support bezier/spline together with bones. bool doBezier = id.Bit(VS_BIT_BEZIER) && !enableBones && useHWTransform; bool doSpline = id.Bit(VS_BIT_SPLINE) && !enableBones && useHWTransform; - if ((doBezier || doSpline) && !hasNormal) { - // Bad usage. - *errorString = "Invalid flags - tess requires normal."; - return false; + if (doBezier || doSpline) { + if (!hasNormal) { + // Bad usage. + *errorString = "Invalid flags - tess requires normal."; + return false; + } + if (compat.shaderLanguage == HLSL_D3D9 || compat.texelFetch == nullptr) { + *errorString = "Tess not supported on this shader language version"; + return false; + } } bool hasColorTess = id.Bit(VS_BIT_HAS_COLOR_TESS); bool hasTexcoordTess = id.Bit(VS_BIT_HAS_TEXCOORD_TESS); bool hasNormalTess = id.Bit(VS_BIT_HAS_NORMAL_TESS); bool flipNormalTess = id.Bit(VS_BIT_NORM_REVERSE_TESS); - if (compat.shaderLanguage == GLSL_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 || compat.shaderLanguage == GLSL_VULKAN) shading = doFlatShading ? "flat " : ""; @@ -202,11 +242,20 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan int numBoneWeights = 0; int boneWeightScale = id.Bits(VS_BIT_WEIGHT_FMTSCALE, 2); - bool texcoordInVec3 = false; + if (enableBones) { + numBoneWeights = 1 + id.Bits(VS_BIT_BONES, 3); + } + bool texCoordInVec3 = false; if (compat.shaderLanguage == GLSL_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); + if (enableBones) { - numBoneWeights = 1 + id.Bits(VS_BIT_BONES, 3); WRITE(p, "%s", boneWeightDecl[numBoneWeights]); } @@ -222,7 +271,7 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan if (doTexture && hasTexcoord) { if (!useHWTransform && doTextureTransform && !isModeThrough) { WRITE(p, "layout (location = %d) in vec3 texcoord;\n", (int)PspAttributeLocation::TEXCOORD); - texcoordInVec3 = true; + texCoordInVec3 = true; } else WRITE(p, "layout (location = %d) in vec2 texcoord;\n", (int)PspAttributeLocation::TEXCOORD); } @@ -245,9 +294,148 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan // See the fragment shader generator WRITE(p, "layout (location = 3) out float v_fogdepth;\n"); } + } else if (compat.shaderLanguage == HLSL_D3D11 || compat.shaderLanguage == HLSL_D3D9) { + // Note: These two share some code after this hellishly large if/else. + if (compat.shaderLanguage == HLSL_D3D11) { + WRITE(p, "cbuffer base : register(b0) {\n%s};\n", ub_baseStr); + WRITE(p, "cbuffer lights: register(b1) {\n%s};\n", ub_vs_lightsStr); + WRITE(p, "cbuffer bones : register(b2) {\n%s};\n", ub_vs_bonesStr); + } else { + WRITE(p, "#pragma warning( disable : 3571 )\n"); + if (isModeThrough) { + WRITE(p, "mat4 u_proj_through : register(c%i);\n", CONST_VS_PROJ_THROUGH); + } else { + WRITE(p, "mat4 u_proj : register(c%i);\n", CONST_VS_PROJ); + // Add all the uniforms we'll need to transform properly. + } + + if (enableFog) { + WRITE(p, "vec2 u_fogcoef : register(c%i);\n", CONST_VS_FOGCOEF); + } + if (useHWTransform || !hasColor) + WRITE(p, "vec4 u_matambientalpha : register(c%i);\n", CONST_VS_MATAMBIENTALPHA); // matambient + matalpha + + if (useHWTransform) { + // When transforming by hardware, we need a great deal more uniforms... + WRITE(p, "mat3x4 u_world : register(c%i);\n", CONST_VS_WORLD); + WRITE(p, "mat3x4 u_view : register(c%i);\n", CONST_VS_VIEW); + if (doTextureTransform) + WRITE(p, "mat3x4 u_texmtx : register(c%i);\n", CONST_VS_TEXMTX); + if (enableBones) { +#ifdef USE_BONE_ARRAY + WRITE(p, "mat3x4 u_bone[%i] : register(c%i);\n", numBones, CONST_VS_BONE0); +#else + for (int i = 0; i < numBoneWeights; i++) { + WRITE(p, "mat3x4 u_bone%i : register(c%i);\n", i, CONST_VS_BONE0 + i * 3); + } +#endif + } + if (doTexture) { + WRITE(p, "vec4 u_uvscaleoffset : register(c%i);\n", CONST_VS_UVSCALEOFFSET); + } + for (int i = 0; i < 4; i++) { + if (doLight[i] != LIGHT_OFF) { + // This is needed for shade mapping + WRITE(p, "vec3 u_lightpos%i : register(c%i);\n", i, CONST_VS_LIGHTPOS + 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)); + + if (type != GE_LIGHTTYPE_DIRECTIONAL) + WRITE(p, "vec3 u_lightatt%i : register(c%i);\n", i, CONST_VS_LIGHTATT + i); + + if (type == GE_LIGHTTYPE_SPOT || type == GE_LIGHTTYPE_UNKNOWN) { + WRITE(p, "vec3 u_lightdir%i : register(c%i);\n", i, CONST_VS_LIGHTDIR + i); + WRITE(p, "vec4 u_lightangle_spotCoef%i : register(c%i);\n", i, CONST_VS_LIGHTANGLE_SPOTCOEF + i); + } + WRITE(p, "vec3 u_lightambient%i : register(c%i);\n", i, CONST_VS_LIGHTAMBIENT + i); + WRITE(p, "vec3 u_lightdiffuse%i : register(c%i);\n", i, CONST_VS_LIGHTDIFFUSE + i); + + if (comp == GE_LIGHTCOMP_BOTH) { + WRITE(p, "vec3 u_lightspecular%i : register(c%i);\n", i, CONST_VS_LIGHTSPECULAR + i); + } + } + } + if (enableLighting) { + WRITE(p, "vec4 u_ambient : register(c%i);\n", CONST_VS_AMBIENT); + if ((matUpdate & 2) == 0 || !hasColor) + WRITE(p, "vec3 u_matdiffuse : register(c%i);\n", CONST_VS_MATDIFFUSE); + // if ((matUpdate & 4) == 0) + WRITE(p, "vec4 u_matspecular : register(c%i);\n", CONST_VS_MATSPECULAR); // Specular coef is contained in alpha + WRITE(p, "vec3 u_matemissive : register(c%i);\n", CONST_VS_MATEMISSIVE); + } + } + + if (!isModeThrough && gstate_c.Supports(GPU_ROUND_DEPTH_TO_16BIT)) { + WRITE(p, "vec4 u_depthRange : register(c%i);\n", CONST_VS_DEPTHRANGE); + } + if (!isModeThrough) { + WRITE(p, "vec4 u_cullRangeMin : register(c%i);\n", CONST_VS_CULLRANGEMIN); + WRITE(p, "vec4 u_cullRangeMax : register(c%i);\n", CONST_VS_CULLRANGEMAX); + } + } + + // And the "varyings". + if (useHWTransform) { + WRITE(p, "struct VS_IN { \n"); + if ((doSpline || doBezier) && compat.shaderLanguage == HLSL_D3D11) { + WRITE(p, " uint instanceId : SV_InstanceID;\n"); + } + if (enableBones) { + WRITE(p, " %s", boneWeightAttrDeclHLSL[numBoneWeights]); + } + if (doTexture && hasTexcoord) { + WRITE(p, " vec2 texcoord : TEXCOORD0;\n"); + } + if (hasColor) { + WRITE(p, " vec4 color0 : COLOR0;\n"); + } + if (hasNormal) { + WRITE(p, " vec3 normal : NORMAL;\n"); + } + WRITE(p, " vec3 position : POSITION;\n"); + WRITE(p, "};\n"); + } else { + WRITE(p, "struct VS_IN {\n"); + WRITE(p, " vec4 position : POSITION;\n"); + if (doTexture && hasTexcoord) { + if (doTextureTransform && !isModeThrough) { + texCoordInVec3 = true; + WRITE(p, " vec3 texcoord : TEXCOORD0;\n"); + } else + WRITE(p, " vec2 texcoord : TEXCOORD0;\n"); + } + if (hasColor) { + WRITE(p, " vec4 color0 : COLOR0;\n"); + } + // only software transform supplies color1 as vertex data + if (lmode) { + WRITE(p, " vec3 color1 : COLOR1;\n"); + } + WRITE(p, "};\n"); + } + + WRITE(p, "struct VS_OUT {\n"); + if (doTexture) { + WRITE(p, " vec3 v_texcoord : TEXCOORD0;\n"); + } + const char *colorInterpolation = doFlatShading && compat.shaderLanguage == HLSL_D3D11 ? "nointerpolation " : ""; + WRITE(p, " %svec4 v_color0 : COLOR0;\n", colorInterpolation); + if (lmode) + WRITE(p, " vec3 v_color1 : COLOR1;\n"); + + if (enableFog) { + WRITE(p, " float v_fogdepth: TEXCOORD1;\n"); + } + if (compat.shaderLanguage == HLSL_D3D9) { + WRITE(p, " vec4 gl_Position : POSITION;\n"); + } else { + WRITE(p, " vec4 gl_Position : SV_Position;\n"); + } + WRITE(p, "};\n"); } else { if (enableBones) { - numBoneWeights = 1 + id.Bits(VS_BIT_BONES, 3); const char * const * boneWeightDecl = boneWeightAttrDecl; if (!strcmp(compat.attribute, "in")) { boneWeightDecl = boneWeightInDecl; @@ -272,7 +460,7 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan if (doTexture && hasTexcoord) { if (!useHWTransform && doTextureTransform && !isModeThrough) { WRITE(p, "%s vec3 texcoord;\n", compat.attribute); - texcoordInVec3 = true; + texCoordInVec3 = true; } else { WRITE(p, "%s vec2 texcoord;\n", compat.attribute); } @@ -413,35 +601,52 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan if (compat.shaderLanguage == GLSL_VULKAN) { WRITE(p, "struct TessData {\n"); WRITE(p, " vec4 pos;\n"); - WRITE(p, " vec4 uv;\n"); - WRITE(p, " vec4 color;\n"); + WRITE(p, " vec4 tex;\n"); + WRITE(p, " vec4 col;\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, " TessData tess_data[];\n"); + WRITE(p, "};\n"); WRITE(p, "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, " TessWeight tess_weights_u[];\n"); + WRITE(p, "};\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, " TessWeight tess_weights_v[];\n"); + WRITE(p, "};\n"); + } else if (ShaderLanguageIsOpenGL(compat.shaderLanguage)) { 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"); + } else if (compat.shaderLanguage == HLSL_D3D11) { + WRITE(p, "struct TessData {\n"); + WRITE(p, " vec3 pos; float pad1;\n"); + WRITE(p, " vec2 tex; vec2 pad2;\n"); + WRITE(p, " vec4 col;\n"); + WRITE(p, "};\n"); + WRITE(p, "StructuredBuffer tess_data : register(t0);\n"); + + WRITE(p, "struct TessWeight {\n"); + WRITE(p, " vec4 basis;\n"); + WRITE(p, " vec4 deriv;\n"); + WRITE(p, "};\n"); + WRITE(p, "StructuredBuffer tess_weights_u : register(t1);\n"); + WRITE(p, "StructuredBuffer tess_weights_v : register(t2);\n"); + } else if (compat.shaderLanguage == HLSL_D3D9) { + } + const char *init[3] = { "0.0, 0.0", "0.0, 0.0, 0.0", "0.0, 0.0, 0.0, 0.0" }; for (int i = 2; i <= 4; i++) { // Define 3 types vec2, vec3, vec4 WRITE(p, "vec%d tess_sample(in vec%d points[16], mat4 weights) {\n", i, i); - WRITE(p, " vec%d pos = vec%d(0.0);\n", i, i); + WRITE(p, " vec%d pos = vec%d(%s);\n", i, i, init[i - 2]); for (int v = 0; v < 4; ++v) { for (int u = 0; u < 4; ++u) { WRITE(p, " pos += weights[%i][%i] * points[%i];\n", v, u, v * 4 + u); @@ -451,10 +656,14 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan WRITE(p, "}\n"); } - if (compat.glslVersionNumber < 130) { // For glsl version 1.10 + if (ShaderLanguageIsOpenGL(compat.shaderLanguage) && compat.glslVersionNumber < 130) { // For glsl version 1.10 WRITE(p, "mat4 outerProduct(vec4 u, vec4 v) {\n"); WRITE(p, " return mat4(u * v[0], u * v[1], u * v[2], u * v[3]);\n"); WRITE(p, "}\n"); + } else if (compat.shaderLanguage == HLSL_D3D9 || compat.shaderLanguage == HLSL_D3D11) { + WRITE(p, "mat4 outerProduct(vec4 u, vec4 v) {\n"); + WRITE(p, " return mul((float4x1)v, (float1x4)u);\n"); + WRITE(p, "}\n"); } WRITE(p, "struct Tess {\n"); @@ -466,7 +675,13 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan WRITE(p, " vec3 nrm;\n"); WRITE(p, "};\n"); - WRITE(p, "void tessellate(out Tess tess) {\n"); + if (compat.shaderLanguage == HLSL_D3D9 || compat.shaderLanguage == HLSL_D3D11) { + WRITE(p, "void tessellate(in VS_IN In, out Tess tess) {\n"); + WRITE(p, " vec3 position = In.position;\n"); + WRITE(p, " vec3 normal = In.normal;\n"); + } else { + WRITE(p, "void tessellate(out Tess tess) {\n"); + } WRITE(p, " ivec2 point_pos = ivec2(position.z, normal.z)%s;\n", doBezier ? " * 3" : ""); WRITE(p, " ivec2 weight_idx = ivec2(position.xy);\n"); @@ -479,17 +694,17 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { WRITE(p, " index = (%i + point_pos.y) * int(u_spline_counts) + (%i + point_pos.x);\n", i, j); - WRITE(p, " _pos[%i] = tess_data.data[index].pos.xyz;\n", i * 4 + j); + WRITE(p, " _pos[%i] = tess_data[index].pos.xyz;\n", i * 4 + j); if (doTexture && hasTexcoordTess) - WRITE(p, " _tex[%i] = tess_data.data[index].uv.xy;\n", i * 4 + j); + WRITE(p, " _tex[%i] = tess_data[index].tex.xy;\n", i * 4 + j); if (hasColorTess) - WRITE(p, " _col[%i] = tess_data.data[index].color;\n", i * 4 + j); + WRITE(p, " _col[%i] = tess_data[index].col;\n", i * 4 + j); } } // Basis polynomials as weight coefficients - WRITE(p, " vec4 basis_u = tess_weights_u.data[weight_idx.x].basis;\n"); - WRITE(p, " vec4 basis_v = tess_weights_v.data[weight_idx.y].basis;\n"); + WRITE(p, " vec4 basis_u = tess_weights_u[weight_idx.x].basis;\n"); + WRITE(p, " vec4 basis_v = tess_weights_v[weight_idx.y].basis;\n"); WRITE(p, " mat4 basis = outerProduct(basis_u, basis_v);\n"); } else { @@ -527,8 +742,8 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan if (hasNormalTess) { if (compat.coefsFromBuffers) { // Derivatives as weight coefficients - WRITE(p, " vec4 deriv_u = tess_weights_u.data[weight_idx.x].deriv;\n"); - WRITE(p, " vec4 deriv_v = tess_weights_v.data[weight_idx.y].deriv;\n"); + WRITE(p, " vec4 deriv_u = tess_weights_u[weight_idx.x].deriv;\n"); + WRITE(p, " vec4 deriv_v = tess_weights_v[weight_idx.y].deriv;\n"); } else { // Derivatives as weight coefficients WRITE(p, " vec4 deriv_u = %s(u_tess_weights_u, %s, 0);\n", compat.texelFetch, "ivec2(weight_idx.x * 2 + 1, 0)"); @@ -542,37 +757,66 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan WRITE(p, "}\n"); } - WRITE(p, "void main() {\n"); + if (ShaderLanguageIsOpenGL(compat.shaderLanguage) || compat.shaderLanguage == GLSL_VULKAN) { + WRITE(p, "void main() {\n"); + } else if (compat.shaderLanguage == HLSL_D3D9 || compat.shaderLanguage == HLSL_D3D11) { + WRITE(p, "VS_OUT main(VS_IN In) {\n"); + WRITE(p, " VS_OUT Out;\n"); + if (doTexture && hasTexcoord) { + if (texCoordInVec3) { + WRITE(p, " vec3 texcoord = In.texcoord;\n"); + } else { + WRITE(p, " vec2 texcoord = In.texcoord;\n"); + } + } + if (hasColor) { + WRITE(p, " vec4 color0 = In.color0;\n"); + if (lmode && !useHWTransform) { + WRITE(p, " vec3 color1 = In.color1;\n"); + } + } + if (hasNormal) { + WRITE(p, " vec3 normal = In.normal;\n"); + } + if (useHWTransform) { + WRITE(p, " vec3 position = In.position;\n"); + } else { + WRITE(p, " vec4 position = In.position;\n"); + } + if (enableBones) { + WRITE(p, "%s", boneWeightAttrInitHLSL[numBoneWeights]); + } + } if (!useHWTransform) { // Simple pass-through of vertex data to fragment shader if (doTexture) { - if (texcoordInVec3) { - WRITE(p, " v_texcoord = texcoord;\n"); + if (texCoordInVec3) { + WRITE(p, " %sv_texcoord = texcoord;\n", compat.vsOutPrefix); } else { - WRITE(p, " v_texcoord = vec3(texcoord, 1.0);\n"); + WRITE(p, " %sv_texcoord = vec3(texcoord, 1.0);\n", compat.vsOutPrefix); } } if (hasColor) { - WRITE(p, " v_color0 = color0;\n"); + WRITE(p, " %sv_color0 = color0;\n", compat.vsOutPrefix); if (lmode) - WRITE(p, " v_color1 = color1;\n"); + WRITE(p, " %sv_color1 = color1;\n", compat.vsOutPrefix); } else { - WRITE(p, " v_color0 = u_matambientalpha;\n"); + WRITE(p, " %sv_color0 = u_matambientalpha;\n", compat.vsOutPrefix); if (lmode) - WRITE(p, " v_color1 = vec3(0.0);\n"); + WRITE(p, " %sv_color1 = splat3(0.0);\n", compat.vsOutPrefix); } if (enableFog) { - WRITE(p, " v_fogdepth = position.w;\n"); + WRITE(p, " %sv_fogdepth = position.w;\n", compat.vsOutPrefix); } if (isModeThrough) { - WRITE(p, " vec4 outPos = u_proj_through * vec4(position.xyz, 1.0);\n"); + WRITE(p, " vec4 outPos = mul(u_proj_through, vec4(position.xyz, 1.0));\n"); } else { // The viewport is used in this case, so need to compensate for that. if (gstate_c.Supports(GPU_ROUND_DEPTH_TO_16BIT)) { - WRITE(p, " vec4 outPos = depthRoundZVP(u_proj * vec4(position.xyz, 1.0));\n"); + WRITE(p, " vec4 outPos = depthRoundZVP(mul(u_proj, vec4(position.xyz, 1.0)));\n"); } else { - WRITE(p, " vec4 outPos = u_proj * vec4(position.xyz, 1.0);\n"); + WRITE(p, " vec4 outPos = mul(u_proj, vec4(position.xyz, 1.0));\n"); } } } else { @@ -581,19 +825,23 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan if (doBezier || doSpline) { // Hardware tessellation WRITE(p, " Tess tess;\n"); - WRITE(p, " tessellate(tess);\n"); + if (compat.shaderLanguage == HLSL_D3D9 || compat.shaderLanguage == HLSL_D3D11) { + WRITE(p, " tessellate(In, tess);\n"); + } else { + WRITE(p, " tessellate(tess);\n"); + } - WRITE(p, " vec3 worldpos = (vec4(tess.pos.xyz, 1.0) * u_world).xyz;\n"); + WRITE(p, " vec3 worldpos = mul(vec4(tess.pos.xyz, 1.0), u_world).xyz;\n"); if (hasNormalTess) { - WRITE(p, " mediump vec3 worldnormal = normalize((vec4(%stess.nrm, 0.0) * u_world).xyz);\n", flipNormalTess ? "-" : ""); + WRITE(p, " mediump vec3 worldnormal = normalize(mul(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 = (vec4(position.xyz, 1.0) * u_world).xyz;\n"); + WRITE(p, " vec3 worldpos = mul(vec4(position, 1.0), u_world).xyz;\n"); if (hasNormal) - WRITE(p, " mediump vec3 worldnormal = normalize((vec4(%snormal, 0.0) * u_world).xyz);\n", flipNormal ? "-" : ""); + WRITE(p, " mediump vec3 worldnormal = normalize(mul(vec4(%snormal, 0.0), u_world).xyz);\n", flipNormal ? "-" : ""); else WRITE(p, " mediump vec3 worldnormal = vec3(0.0, 0.0, 1.0);\n"); } @@ -626,24 +874,24 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan WRITE(p, ";\n"); - WRITE(p, " vec3 skinnedpos = (vec4(position, 1.0) * skinMatrix).xyz %s;\n", factor); - WRITE(p, " vec3 worldpos = (vec4(skinnedpos, 1.0) * u_world).xyz;\n"); + WRITE(p, " vec3 skinnedpos = mul(vec4(position, 1.0), skinMatrix).xyz%s;\n", factor); + WRITE(p, " vec3 worldpos = mul(vec4(skinnedpos, 1.0), u_world).xyz;\n"); if (hasNormal) { - WRITE(p, " mediump vec3 skinnednormal = (vec4(%snormal, 0.0) * skinMatrix).xyz %s;\n", flipNormal ? "-" : "", factor); + WRITE(p, " mediump vec3 skinnednormal = mul(vec4(%snormal, 0.0), skinMatrix).xyz%s;\n", flipNormal ? "-" : "", factor); } else { - WRITE(p, " mediump vec3 skinnednormal = (vec4(0.0, 0.0, %s1.0, 0.0) * skinMatrix).xyz %s;\n", flipNormal ? "-" : "", factor); + WRITE(p, " mediump vec3 skinnednormal = mul(vec4(0.0, 0.0, %s1.0, 0.0), skinMatrix).xyz%s;\n", flipNormal ? "-" : "", factor); } - WRITE(p, " mediump vec3 worldnormal = normalize((vec4(skinnednormal, 0.0) * u_world).xyz);\n"); + WRITE(p, " mediump vec3 worldnormal = normalize(mul(vec4(skinnednormal, 0.0), u_world).xyz);\n"); } - WRITE(p, " vec4 viewPos = vec4((vec4(worldpos, 1.0) * u_view).xyz, 1.0);\n"); + WRITE(p, " vec4 viewPos = vec4(mul(vec4(worldpos, 1.0), u_view).xyz, 1.0);\n"); // Final view and projection transforms. if (gstate_c.Supports(GPU_ROUND_DEPTH_TO_16BIT)) { - WRITE(p, " vec4 outPos = depthRoundZVP(u_proj * viewPos);\n"); + WRITE(p, " vec4 outPos = depthRoundZVP(mul(u_proj, viewPos));\n"); } else { - WRITE(p, " vec4 outPos = u_proj * viewPos;\n"); + WRITE(p, " vec4 outPos = mul(u_proj, viewPos);\n"); } // TODO: Declare variables for dots for shade mapping if needed. @@ -680,7 +928,7 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan } if (!specularIsZero) { - WRITE(p, " lowp vec3 lightSum1 = vec3(0.0);\n"); + WRITE(p, " lowp vec3 lightSum1 = splat3(0.0);\n"); } if (!diffuseIsZero) { WRITE(p, " vec3 toLight;\n"); @@ -771,32 +1019,32 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan if (enableLighting) { // Sum up ambient, emissive here. if (lmode) { - WRITE(p, " v_color0 = clamp(lightSum0, 0.0, 1.0);\n"); + WRITE(p, " %sv_color0 = clamp(lightSum0, 0.0, 1.0);\n", compat.vsOutPrefix); // v_color1 only exists when lmode = 1. if (specularIsZero) { - WRITE(p, " v_color1 = vec3(0.0);\n"); + WRITE(p, " %sv_color1 = splat3(0.0);\n", compat.vsOutPrefix); } else { - WRITE(p, " v_color1 = clamp(lightSum1, 0.0, 1.0);\n"); + WRITE(p, " %sv_color1 = clamp(lightSum1, 0.0, 1.0);\n", compat.vsOutPrefix); } } else { if (specularIsZero) { - WRITE(p, " v_color0 = clamp(lightSum0, 0.0, 1.0);\n"); + WRITE(p, " %sv_color0 = clamp(lightSum0, 0.0, 1.0);\n", compat.vsOutPrefix); } else { - WRITE(p, " v_color0 = clamp(clamp(lightSum0, 0.0, 1.0) + vec4(lightSum1, 0.0), 0.0, 1.0);\n"); + WRITE(p, " %sv_color0 = clamp(clamp(lightSum0, 0.0, 1.0) + vec4(lightSum1, 0.0), 0.0, 1.0);\n", compat.vsOutPrefix); } } } else { // Lighting doesn't affect color. if (hasColor) { if (doBezier || doSpline) - WRITE(p, " v_color0 = tess.col;\n"); + WRITE(p, " %sv_color0 = tess.col;\n", compat.vsOutPrefix); else - WRITE(p, " v_color0 = color0;\n"); + WRITE(p, " %sv_color0 = color0;\n", compat.vsOutPrefix); } else { - WRITE(p, " v_color0 = u_matambientalpha;\n"); + WRITE(p, " %sv_color0 = u_matambientalpha;\n", compat.vsOutPrefix); } if (lmode) - WRITE(p, " v_color1 = vec3(0.0);\n"); + WRITE(p, " %sv_color1 = splat3(0.0);\n", compat.vsOutPrefix); } bool scaleUV = !isModeThrough && (uvGenMode == GE_TEXMAP_TEXTURE_COORDS || uvGenMode == GE_TEXMAP_UNKNOWN); @@ -809,17 +1057,17 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan if (scaleUV) { if (hasTexcoord) { if (doBezier || doSpline) - WRITE(p, " v_texcoord = vec3(tess.tex.xy * u_uvscaleoffset.xy + u_uvscaleoffset.zw, 0.0);\n"); + WRITE(p, " %sv_texcoord = vec3(tess.tex.xy * u_uvscaleoffset.xy + u_uvscaleoffset.zw, 0.0);\n", compat.vsOutPrefix); else - WRITE(p, " v_texcoord = vec3(texcoord.xy * u_uvscaleoffset.xy, 0.0);\n"); + WRITE(p, " %sv_texcoord = vec3(texcoord.xy * u_uvscaleoffset.xy, 0.0);\n", compat.vsOutPrefix); } else { - WRITE(p, " v_texcoord = vec3(0.0);\n"); + WRITE(p, " %sv_texcoord = splat3(0.0);\n", compat.vsOutPrefix); } } else { if (hasTexcoord) { - WRITE(p, " v_texcoord = vec3(texcoord.xy * u_uvscaleoffset.xy + u_uvscaleoffset.zw, 0.0);\n"); + WRITE(p, " %sv_texcoord = vec3(texcoord.xy * u_uvscaleoffset.xy + u_uvscaleoffset.zw, 0.0);\n", compat.vsOutPrefix); } else { - WRITE(p, " v_texcoord = vec3(u_uvscaleoffset.zw, 0.0);\n"); + WRITE(p, " %sv_texcoord = vec3(u_uvscaleoffset.zw, 0.0);\n", compat.vsOutPrefix); } } break; @@ -829,7 +1077,7 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan std::string temp_tc; switch (uvProjMode) { case GE_PROJMAP_POSITION: // Use model space XYZ as source - temp_tc = "vec4(position.xyz, 1.0)"; + temp_tc = "vec4(position, 1.0)"; break; case GE_PROJMAP_UV: // Use unscaled UV as source { @@ -855,7 +1103,7 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan break; } // Transform by texture matrix. XYZ as we are doing projection mapping. - WRITE(p, " v_texcoord = (%s * u_texmtx).xyz * vec3(u_uvscaleoffset.xy, 1.0);\n", temp_tc.c_str()); + WRITE(p, " %sv_texcoord = mul(%s, u_texmtx).xyz * vec3(u_uvscaleoffset.xy, 1.0);\n", compat.vsOutPrefix, temp_tc.c_str()); } break; @@ -863,7 +1111,7 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan { std::string lightFactor0 = StringFromFormat("(length(u_lightpos%i) == 0.0 ? worldnormal.z : dot(normalize(u_lightpos%i), worldnormal))", ls0, ls0); std::string lightFactor1 = StringFromFormat("(length(u_lightpos%i) == 0.0 ? worldnormal.z : dot(normalize(u_lightpos%i), worldnormal))", ls1, ls1); - WRITE(p, " v_texcoord = vec3(u_uvscaleoffset.xy * vec2(1.0 + %s, 1.0 + %s) * 0.5, 1.0);\n", lightFactor0.c_str(), lightFactor1.c_str()); + WRITE(p, " %sv_texcoord = vec3(u_uvscaleoffset.xy * vec2(1.0 + %s, 1.0 + %s) * 0.5, 1.0);\n", compat.vsOutPrefix, lightFactor0.c_str(), lightFactor1.c_str()); } break; @@ -875,7 +1123,7 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan // Compute fogdepth if (enableFog) - WRITE(p, " v_fogdepth = (viewPos.z + u_fogcoef.x) * u_fogcoef.y;\n"); + WRITE(p, " %sv_fogdepth = (viewPos.z + u_fogcoef.x) * u_fogcoef.y;\n", compat.vsOutPrefix); } if (!isModeThrough && gstate_c.Supports(GPU_SUPPORTS_VS_RANGE_CULLING)) { @@ -885,15 +1133,19 @@ bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLan const char *outMin = "projPos.x < u_cullRangeMin.x || projPos.y < u_cullRangeMin.y || projPos.z < u_cullRangeMin.z"; const char *outMax = "projPos.x > u_cullRangeMax.x || projPos.y > u_cullRangeMax.y || projPos.z > u_cullRangeMax.z"; WRITE(p, " if (%s || %s) {\n", outMin, outMax); - WRITE(p, " outPos.xyzw = vec4(u_cullRangeMax.w);\n"); + WRITE(p, " outPos.xyzw = u_cullRangeMax.wwww;\n"); WRITE(p, " }\n"); WRITE(p, " }\n"); } - WRITE(p, " gl_Position = outPos;\n"); - if (compat.shaderLanguage == GLSL_VULKAN) { - WRITE(p, " gl_PointSize = 1.0;\n"); - } + // We've named the output gl_Position in HLSL as well. + WRITE(p, " %sgl_Position = outPos;\n", compat.vsOutPrefix); + if (compat.shaderLanguage == GLSL_VULKAN) { + WRITE(p, " gl_PointSize = 1.0;\n"); + } + if (compat.shaderLanguage == HLSL_D3D11 || compat.shaderLanguage == HLSL_D3D9) { + WRITE(p, " return Out;\n"); + } WRITE(p, "}\n"); return true; } diff --git a/GPU/Directx9/VertexShaderGeneratorHLSL.h b/GPU/Common/VertexShaderGenerator.h similarity index 86% rename from GPU/Directx9/VertexShaderGeneratorHLSL.h rename to GPU/Common/VertexShaderGenerator.h index 08adab846c..8f69f5bcd8 100644 --- a/GPU/Directx9/VertexShaderGeneratorHLSL.h +++ b/GPU/Common/VertexShaderGenerator.h @@ -17,11 +17,13 @@ #pragma once -#include -#include "GPU/Common/ShaderID.h" +#include "Common/CommonTypes.h" -bool GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage lang, std::string *errorString); +struct VShaderID; +bool GenerateVertexShader(const VShaderID &id, char *buffer, const ShaderLanguageDesc &compat, uint32_t *attrMask, uint64_t *uniformMask, std::string *errorString); + +// D3D9 constants. enum { CONST_VS_PROJ = 0, CONST_VS_PROJ_THROUGH = 4, diff --git a/GPU/D3D11/D3D11Util.cpp b/GPU/D3D11/D3D11Util.cpp index acbdfc8e35..d8a68c5f14 100644 --- a/GPU/D3D11/D3D11Util.cpp +++ b/GPU/D3D11/D3D11Util.cpp @@ -26,8 +26,13 @@ std::vector CompileShaderToBytecodeD3D11(const char *code, size_t codeS std::string errors; if (errorMsgs) { errors = std::string((const char *)errorMsgs->GetBufferPointer(), errorMsgs->GetBufferSize()); - ERROR_LOG(G3D, "%s: %s", SUCCEEDED(result) ? "warnings" : "errors", errors.c_str()); + if (SUCCEEDED(result)) { + WARN_LOG(G3D, "%s: %s", "warnings", errors.c_str()); + } else { + ERROR_LOG(G3D, "%s: %s", "errors", errors.c_str()); + } OutputDebugStringA(LineNumberString(code).c_str()); + OutputDebugStringA(errors.c_str()); errorMsgs->Release(); } if (compiledCode) { diff --git a/GPU/D3D11/ShaderManagerD3D11.cpp b/GPU/D3D11/ShaderManagerD3D11.cpp index 9f13a9934f..88730eec70 100644 --- a/GPU/D3D11/ShaderManagerD3D11.cpp +++ b/GPU/D3D11/ShaderManagerD3D11.cpp @@ -34,8 +34,8 @@ #include "GPU/Math3D.h" #include "GPU/GPUState.h" #include "GPU/ge_constants.h" +#include "GPU/Common/VertexShaderGenerator.h" #include "GPU/D3D11/ShaderManagerD3D11.h" -#include "GPU/Directx9/VertexShaderGeneratorHLSL.h" #include "GPU/D3D11/D3D11Util.h" D3D11FragmentShader::D3D11FragmentShader(ID3D11Device *device, D3D_FEATURE_LEVEL featureLevel, FShaderID id, const char *code, bool useHWTransform) @@ -208,7 +208,9 @@ void ShaderManagerD3D11::GetShaders(int prim, u32 vertType, D3D11VertexShader ** if (vsIter == vsCache_.end()) { // Vertex shader not in cache. Let's compile it. std::string genErrorString; - GenerateVertexShaderHLSL(VSID, codeBuffer_, HLSL_D3D11, &genErrorString); + uint32_t attrMask; + uint64_t uniformMask; + GenerateVertexShader(VSID, codeBuffer_, compat_, &attrMask, &uniformMask, &genErrorString); vs = new D3D11VertexShader(device_, featureLevel_, VSID, codeBuffer_, vertType, useHWTransform); vsCache_[VSID] = vs; } else { diff --git a/GPU/Directx9/ShaderManagerDX9.cpp b/GPU/Directx9/ShaderManagerDX9.cpp index 14ddf01540..4aafbcb290 100644 --- a/GPU/Directx9/ShaderManagerDX9.cpp +++ b/GPU/Directx9/ShaderManagerDX9.cpp @@ -580,7 +580,9 @@ VSShader *ShaderManagerDX9::ApplyShader(bool useHWTransform, bool useHWTessellat if (vsIter == vsCache_.end()) { // Vertex shader not in cache. Let's compile it. std::string genErrorString; - if (GenerateVertexShaderHLSL(VSID, codeBuffer_, HLSL_D3D9, &genErrorString)) { + uint32_t attrMask; + uint64_t uniformMask; + if (GenerateVertexShader(VSID, codeBuffer_, compat_, &attrMask, &uniformMask, &genErrorString)) { vs = new VSShader(device_, VSID, codeBuffer_, useHWTransform); } if (!vs || vs->Failed()) { @@ -603,7 +605,9 @@ VSShader *ShaderManagerDX9::ApplyShader(bool useHWTransform, bool useHWTessellat // next time and we'll do this over and over... // Can still work with software transform. - bool success = GenerateVertexShaderHLSL(VSID, codeBuffer_, HLSL_D3D9, &genErrorString); + uint32_t attrMask; + uint64_t uniformMask; + bool success = GenerateVertexShader(VSID, codeBuffer_, compat_, &attrMask, &uniformMask, &genErrorString); _assert_(success); vs = new VSShader(device_, VSID, codeBuffer_, false); } diff --git a/GPU/Directx9/ShaderManagerDX9.h b/GPU/Directx9/ShaderManagerDX9.h index 4f5be85150..8e132e2a31 100644 --- a/GPU/Directx9/ShaderManagerDX9.h +++ b/GPU/Directx9/ShaderManagerDX9.h @@ -21,7 +21,7 @@ #include #include "Common/Common.h" -#include "GPU/Directx9/VertexShaderGeneratorHLSL.h" +#include "GPU/Common/VertexShaderGenerator.h" #include "GPU/Common/FragmentShaderGenerator.h" #include "GPU/Common/ShaderCommon.h" #include "GPU/Common/ShaderId.h" diff --git a/GPU/Directx9/VertexShaderGeneratorHLSL.cpp b/GPU/Directx9/VertexShaderGeneratorHLSL.cpp deleted file mode 100644 index e0a4b57887..0000000000 --- a/GPU/Directx9/VertexShaderGeneratorHLSL.cpp +++ /dev/null @@ -1,717 +0,0 @@ -// Copyright (c) 2012- PPSSPP Project. - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, version 2.0 or later versions. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License 2.0 for more details. - -// A copy of the GPL 2.0 should have been included with the program. -// If not, see http://www.gnu.org/licenses/ - -// Official git repository and contact information can be found at -// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. - -#include -#include - -#include "Common/StringUtils.h" -#include "GPU/ge_constants.h" -#include "GPU/GPUState.h" -#include "Core/Config.h" - -#include "GPU/Directx9/VertexShaderGeneratorHLSL.h" -#include "GPU/Common/VertexDecoderCommon.h" -#include "GPU/Common/ShaderUniforms.h" - -#undef WRITE - -#define WRITE p+=sprintf - -static const char * const boneWeightAttrDecl[9] = { - "#ERROR#", - "float a_w1:TEXCOORD1;\n", - "vec2 a_w1:TEXCOORD1;\n", - "vec3 a_w1:TEXCOORD1;\n", - "vec4 a_w1:TEXCOORD1;\n", - "vec4 a_w1:TEXCOORD1;\n float a_w2:TEXCOORD2;\n", - "vec4 a_w1:TEXCOORD1;\n vec2 a_w2:TEXCOORD2;\n", - "vec4 a_w1:TEXCOORD1;\n vec3 a_w2:TEXCOORD2;\n", - "vec4 a_w1:TEXCOORD1;\n vec4 a_w2:TEXCOORD2;\n", -}; - -bool GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage lang, std::string *errorString) { - char *p = buffer; - - bool isModeThrough = id.Bit(VS_BIT_IS_THROUGH); - bool lmode = id.Bit(VS_BIT_LMODE); - bool doTexture = id.Bit(VS_BIT_DO_TEXTURE); - - GETexMapMode uvGenMode = static_cast(id.Bits(VS_BIT_UVGEN_MODE, 2)); - bool doTextureTransform = uvGenMode == GE_TEXMAP_TEXTURE_MATRIX; - - // this is only valid for some settings of uvGenMode - GETexProjMapMode uvProjMode = static_cast(id.Bits(VS_BIT_UVPROJ_MODE, 2)); - bool doShadeMapping = uvGenMode == GE_TEXMAP_ENVIRONMENT_MAP; - bool doFlatShading = id.Bit(VS_BIT_FLATSHADE); - - bool useHWTransform = id.Bit(VS_BIT_USE_HW_TRANSFORM); - bool hasColor = id.Bit(VS_BIT_HAS_COLOR) || !useHWTransform; - bool hasNormal = id.Bit(VS_BIT_HAS_NORMAL) && useHWTransform; - bool hasTexcoord = id.Bit(VS_BIT_HAS_TEXCOORD) || !useHWTransform; - bool enableFog = id.Bit(VS_BIT_ENABLE_FOG); - bool flipNormal = id.Bit(VS_BIT_NORM_REVERSE); - int ls0 = id.Bits(VS_BIT_LS0, 2); - int ls1 = id.Bits(VS_BIT_LS1, 2); - bool enableBones = id.Bit(VS_BIT_ENABLE_BONES); - bool enableLighting = id.Bit(VS_BIT_LIGHTING_ENABLE); - int matUpdate = id.Bits(VS_BIT_MATERIAL_UPDATE, 3); - - bool doBezier = id.Bit(VS_BIT_BEZIER) && !enableBones && useHWTransform; - bool doSpline = id.Bit(VS_BIT_SPLINE) && !enableBones && useHWTransform; - if ((doBezier || doSpline) && !hasNormal) { - // Bad usage. - *errorString = "Invalid flags - tess requires normal."; - return false; - } - bool hasColorTess = id.Bit(VS_BIT_HAS_COLOR_TESS); - bool hasTexcoordTess = id.Bit(VS_BIT_HAS_TEXCOORD_TESS); - bool hasNormalTess = id.Bit(VS_BIT_HAS_NORMAL_TESS); - bool flipNormalTess = id.Bit(VS_BIT_NORM_REVERSE_TESS); - - DoLightComputation doLight[4] = { LIGHT_OFF, LIGHT_OFF, LIGHT_OFF, LIGHT_OFF }; - if (useHWTransform) { - int shadeLight0 = doShadeMapping ? ls0 : -1; - int shadeLight1 = doShadeMapping ? ls1 : -1; - for (int i = 0; i < 4; i++) { - if (i == shadeLight0 || i == shadeLight1) - doLight[i] = LIGHT_SHADE; - if (enableLighting && id.Bit(VS_BIT_LIGHT0_ENABLE + i)) - doLight[i] = LIGHT_FULL; - } - } - - int numBoneWeights = 0; - int boneWeightScale = id.Bits(VS_BIT_WEIGHT_FMTSCALE, 2); - if (enableBones) { - numBoneWeights = 1 + id.Bits(VS_BIT_BONES, 3); - } - - // Output some compatibility defines - WRITE(p, "#define vec2 float2\n"); - WRITE(p, "#define vec3 float3\n"); - WRITE(p, "#define vec4 float4\n"); - WRITE(p, "#define mat4 float4x4\n"); - WRITE(p, "#define mat3x4 float4x3\n"); // note how the conventions are backwards - WRITE(p, "#define splat3(x) vec3(x, x, x)\n"); - - if (lang == HLSL_D3D9) { - WRITE(p, "#pragma warning( disable : 3571 )\n"); - if (isModeThrough) { - WRITE(p, "float4x4 u_proj_through : register(c%i);\n", CONST_VS_PROJ_THROUGH); - } else { - WRITE(p, "float4x4 u_proj : register(c%i);\n", CONST_VS_PROJ); - // Add all the uniforms we'll need to transform properly. - } - - if (enableFog) { - WRITE(p, "vec2 u_fogcoef : register(c%i);\n", CONST_VS_FOGCOEF); - } - if (useHWTransform || !hasColor) - WRITE(p, "vec4 u_matambientalpha : register(c%i);\n", CONST_VS_MATAMBIENTALPHA); // matambient + matalpha - - if (useHWTransform) { - // When transforming by hardware, we need a great deal more uniforms... - WRITE(p, "mat3x4 u_world : register(c%i);\n", CONST_VS_WORLD); - WRITE(p, "mat3x4 u_view : register(c%i);\n", CONST_VS_VIEW); - if (doTextureTransform) - WRITE(p, "mat3x4 u_texmtx : register(c%i);\n", CONST_VS_TEXMTX); - if (enableBones) { -#ifdef USE_BONE_ARRAY - WRITE(p, "mat3x4 u_bone[%i] : register(c%i);\n", numBones, CONST_VS_BONE0); -#else - for (int i = 0; i < numBoneWeights; i++) { - WRITE(p, "mat3x4 u_bone%i : register(c%i);\n", i, CONST_VS_BONE0 + i * 3); - } -#endif - } - if (doTexture) { - WRITE(p, "vec4 u_uvscaleoffset : register(c%i);\n", CONST_VS_UVSCALEOFFSET); - } - for (int i = 0; i < 4; i++) { - if (doLight[i] != LIGHT_OFF) { - // This is needed for shade mapping - WRITE(p, "vec3 u_lightpos%i : register(c%i);\n", i, CONST_VS_LIGHTPOS + 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)); - - if (type != GE_LIGHTTYPE_DIRECTIONAL) - WRITE(p, "vec3 u_lightatt%i : register(c%i);\n", i, CONST_VS_LIGHTATT + i); - - if (type == GE_LIGHTTYPE_SPOT || type == GE_LIGHTTYPE_UNKNOWN) { - WRITE(p, "vec3 u_lightdir%i : register(c%i);\n", i, CONST_VS_LIGHTDIR + i); - WRITE(p, "vec4 u_lightangle_spotCoef%i : register(c%i);\n", i, CONST_VS_LIGHTANGLE_SPOTCOEF + i); - } - WRITE(p, "vec3 u_lightambient%i : register(c%i);\n", i, CONST_VS_LIGHTAMBIENT + i); - WRITE(p, "vec3 u_lightdiffuse%i : register(c%i);\n", i, CONST_VS_LIGHTDIFFUSE + i); - - if (comp == GE_LIGHTCOMP_BOTH) { - WRITE(p, "vec3 u_lightspecular%i : register(c%i);\n", i, CONST_VS_LIGHTSPECULAR + i); - } - } - } - if (enableLighting) { - WRITE(p, "vec4 u_ambient : register(c%i);\n", CONST_VS_AMBIENT); - if ((matUpdate & 2) == 0 || !hasColor) - WRITE(p, "vec3 u_matdiffuse : register(c%i);\n", CONST_VS_MATDIFFUSE); - // if ((matUpdate & 4) == 0) - WRITE(p, "vec4 u_matspecular : register(c%i);\n", CONST_VS_MATSPECULAR); // Specular coef is contained in alpha - WRITE(p, "vec3 u_matemissive : register(c%i);\n", CONST_VS_MATEMISSIVE); - } - } - - if (!isModeThrough && gstate_c.Supports(GPU_ROUND_DEPTH_TO_16BIT)) { - WRITE(p, "vec4 u_depthRange : register(c%i);\n", CONST_VS_DEPTHRANGE); - } - if (!isModeThrough) { - WRITE(p, "vec4 u_cullRangeMin : register(c%i);\n", CONST_VS_CULLRANGEMIN); - WRITE(p, "vec4 u_cullRangeMax : register(c%i);\n", CONST_VS_CULLRANGEMAX); - } - } else { - WRITE(p, "cbuffer base : register(b0) {\n%s};\n", cb_baseStr); - WRITE(p, "cbuffer lights: register(b1) {\n%s};\n", cb_vs_lightsStr); - WRITE(p, "cbuffer bones : register(b2) {\n%s};\n", cb_vs_bonesStr); - } - - bool scaleUV = !isModeThrough && (uvGenMode == GE_TEXMAP_TEXTURE_COORDS || uvGenMode == GE_TEXMAP_UNKNOWN); - - // And the "varyings". - bool texCoordInVec3 = false; - if (useHWTransform) { - WRITE(p, "struct VS_IN { \n"); - if ((doSpline || doBezier) && lang == HLSL_D3D11) { - WRITE(p, " uint instanceId : SV_InstanceID;\n"); - } - if (enableBones) { - WRITE(p, " %s", boneWeightAttrDecl[numBoneWeights]); - } - if (doTexture && hasTexcoord) { - WRITE(p, " vec2 texcoord : TEXCOORD0;\n"); - } - if (hasColor) { - WRITE(p, " vec4 color0 : COLOR0;\n"); - } - if (hasNormal) { - WRITE(p, " vec3 normal : NORMAL;\n"); - } - WRITE(p, " vec3 position : POSITION;\n"); - WRITE(p, "};\n"); - - } else { - WRITE(p, "struct VS_IN {\n"); - WRITE(p, " vec4 position : POSITION;\n"); - if (doTexture && hasTexcoord) { - if (doTextureTransform && !isModeThrough) { - texCoordInVec3 = true; - WRITE(p, " vec3 texcoord : TEXCOORD0;\n"); - } - else - WRITE(p, " vec2 texcoord : TEXCOORD0;\n"); - } - if (hasColor) { - WRITE(p, " vec4 color0 : COLOR0;\n"); - } - // only software transform supplies color1 as vertex data - if (lmode) { - WRITE(p, " vec4 color1 : COLOR1;\n"); - } - WRITE(p, "};\n"); - } - - WRITE(p, "struct VS_OUT {\n"); - if (doTexture) { - WRITE(p, " vec3 v_texcoord : TEXCOORD0;\n"); - } - const char *colorInterpolation = doFlatShading && lang == HLSL_D3D11 ? "nointerpolation " : ""; - WRITE(p, " %svec4 v_color0 : COLOR0;\n", colorInterpolation); - if (lmode) - WRITE(p, " vec3 v_color1 : COLOR1;\n"); - - if (enableFog) { - WRITE(p, " float v_fogdepth: TEXCOORD1;\n"); - } - if (lang == HLSL_D3D9) { - WRITE(p, " vec4 gl_Position : POSITION;\n"); - } else { - WRITE(p, " vec4 gl_Position : SV_Position;\n"); - } - WRITE(p, "};\n"); - - // Confirmed: Through mode gets through exactly the same in GL and D3D in Phantasy Star: Text is 38023.0 in the test scene. - - if (!isModeThrough && gstate_c.Supports(GPU_ROUND_DEPTH_TO_16BIT)) { - // Apply the projection and viewport to get the Z buffer value, floor to integer, undo the viewport and projection. - // The Z range in D3D is different but we compensate for that using parameters. - WRITE(p, "\nvec4 depthRoundZVP(vec4 v) {\n"); - WRITE(p, " float z = v.z / v.w;\n"); - WRITE(p, " z = (z * u_depthRange.x + u_depthRange.y);\n"); - WRITE(p, " z = floor(z);\n"); - WRITE(p, " z = (z - u_depthRange.z) * u_depthRange.w;\n"); - WRITE(p, " return vec4(v.x, v.y, z * v.w, v.w);\n"); - WRITE(p, "}\n\n"); - } - - // Hardware tessellation - if (doSpline || doBezier) { - if (lang == HLSL_D3D11) { - WRITE(p, "struct TessData {\n"); - WRITE(p, " vec3 pos; float pad1;\n"); - WRITE(p, " vec2 tex; vec2 pad2;\n"); - WRITE(p, " vec4 col;\n"); - WRITE(p, "};\n"); - WRITE(p, "StructuredBuffer tess_data : register(t0);\n"); - - WRITE(p, "struct TessWeight {\n"); - WRITE(p, " vec4 basis;\n"); - WRITE(p, " vec4 deriv;\n"); - WRITE(p, "};\n"); - WRITE(p, "StructuredBuffer tess_weights_u : register(t1);\n"); - WRITE(p, "StructuredBuffer tess_weights_v : register(t2);\n"); - } - - const char *init[3] = { "0.0, 0.0", "0.0, 0.0, 0.0", "0.0, 0.0, 0.0, 0.0" }; - for (int i = 2; i <= 4; i++) { - // Define 3 types vec2, vec3, vec4 - WRITE(p, "float%d tess_sample(in float%d points[16], float4x4 weights) {\n", i, i); - WRITE(p, " float%d pos = float%d(%s);\n", i, i, init[i - 2]); - for (int v = 0; v < 4; ++v) { - for (int u = 0; u < 4; ++u) { - WRITE(p, " pos += weights[%i][%i] * points[%i];\n", v, u, v * 4 + u); - } - } - WRITE(p, " return pos;\n"); - WRITE(p, "}\n"); - } - - WRITE(p, "float4x4 outerProduct(vec4 u, vec4 v) {\n"); - WRITE(p, " return mul((float4x1)v, (float1x4)u);\n"); - WRITE(p, "}\n"); - - WRITE(p, "struct Tess {\n"); - WRITE(p, " vec3 pos;\n"); - if (doTexture) - WRITE(p, " vec2 tex;\n"); - WRITE(p, " vec4 col;\n"); - if (hasNormalTess) - WRITE(p, " vec3 nrm;\n"); - WRITE(p, "};\n"); - - WRITE(p, "void tessellate(in VS_IN In, out Tess tess) {\n"); - WRITE(p, " int2 point_pos = int2(In.position.z, In.normal.z)%s;\n", doBezier ? " * 3" : ""); - WRITE(p, " int2 weight_idx = int2(In.position.xy);\n"); - // Load 4x4 control points - WRITE(p, " vec3 _pos[16];\n"); - WRITE(p, " vec2 _tex[16];\n"); - WRITE(p, " vec4 _col[16];\n"); - WRITE(p, " int index;\n"); - for (int i = 0; i < 4; i++) { - for (int j = 0; j < 4; j++) { - WRITE(p, " index = (%i + point_pos.y) * u_spline_counts + (%i + point_pos.x);\n", i, j); - WRITE(p, " _pos[%i] = tess_data[index].pos;\n", i * 4 + j); - if (doTexture && hasTexcoordTess) - WRITE(p, " _tex[%i] = tess_data[index].tex;\n", i * 4 + j); - if (hasColorTess) - WRITE(p, " _col[%i] = tess_data[index].col;\n", i * 4 + j); - } - } - - // Basis polynomials as weight coefficients - WRITE(p, " vec4 basis_u = tess_weights_u[weight_idx.x].basis;\n"); - WRITE(p, " vec4 basis_v = tess_weights_v[weight_idx.y].basis;\n"); - WRITE(p, " float4x4 basis = outerProduct(basis_u, basis_v);\n"); - - // Tessellate - WRITE(p, " tess.pos = tess_sample(_pos, basis);\n"); - if (doTexture) { - if (hasTexcoordTess) - WRITE(p, " tess.tex = tess_sample(_tex, basis);\n"); - else - WRITE(p, " tess.tex = In.normal.xy;\n"); - } - if (hasColorTess) - WRITE(p, " tess.col = tess_sample(_col, basis);\n"); - else - WRITE(p, " tess.col = u_matambientalpha;\n"); - if (hasNormalTess) { - // Derivatives as weight coefficients - WRITE(p, " vec4 deriv_u = tess_weights_u[weight_idx.x].deriv;\n"); - WRITE(p, " vec4 deriv_v = tess_weights_v[weight_idx.y].deriv;\n"); - - WRITE(p, " vec3 du = tess_sample(_pos, outerProduct(deriv_u, basis_v));\n"); - WRITE(p, " vec3 dv = tess_sample(_pos, outerProduct(basis_u, deriv_v));\n"); - WRITE(p, " tess.nrm = normalize(cross(du, dv));\n"); - } - WRITE(p, "}\n"); - } - - WRITE(p, "VS_OUT main(VS_IN In) {\n"); - WRITE(p, " VS_OUT Out;\n"); - if (!useHWTransform) { - // Simple pass-through of vertex data to fragment shader - if (doTexture) { - if (texCoordInVec3) { - WRITE(p, " Out.v_texcoord = In.texcoord;\n"); - } else { - WRITE(p, " Out.v_texcoord = vec3(In.texcoord, 1.0);\n"); - } - } - if (hasColor) { - WRITE(p, " Out.v_color0 = In.color0;\n"); - if (lmode) - WRITE(p, " Out.v_color1 = In.color1.rgb;\n"); - } else { - WRITE(p, " Out.v_color0 = In.u_matambientalpha;\n"); - if (lmode) - WRITE(p, " Out.v_color1 = vec3(0.0);\n"); - } - if (enableFog) { - WRITE(p, " Out.v_fogdepth = In.position.w;\n"); - } - if (isModeThrough) { - WRITE(p, " float4 outPos = mul(u_proj_through, vec4(In.position.xyz, 1.0));\n"); - } else { - if (gstate_c.Supports(GPU_ROUND_DEPTH_TO_16BIT)) { - WRITE(p, " vec4 outPos = depthRoundZVP(mul(u_proj, vec4(In.position.xyz, 1.0)));\n"); - } else { - WRITE(p, " vec4 outPos = mul(u_proj, vec4(In.position.xyz, 1.0));\n"); - } - } - } else { - // Step 1: World Transform / Skinning - if (!enableBones) { - if (doSpline || doBezier) { - // Hardware tessellation - WRITE(p, " Tess tess;\n"); - WRITE(p, " tessellate(In, tess);\n"); - - WRITE(p, " vec3 worldpos = mul(vec4(tess.pos.xyz, 1.0), u_world);\n"); - if (hasNormalTess) - WRITE(p, " vec3 worldnormal = normalize(mul(vec4(%stess.nrm, 0.0), u_world));\n", flipNormalTess ? "-" : ""); - else - WRITE(p, " vec3 worldnormal = vec3(0.0, 0.0, 1.0);\n"); - } else { - // No skinning, just standard T&L. - WRITE(p, " vec3 worldpos = mul(vec4(In.position.xyz, 1.0), u_world);\n"); - if (hasNormal) - WRITE(p, " vec3 worldnormal = normalize(mul(vec4(%sIn.normal, 0.0), u_world));\n", flipNormal ? "-" : ""); - else - WRITE(p, " vec3 worldnormal = vec3(0.0, 0.0, 1.0);\n"); - } - } else { - static const char * const boneWeightAttr[8] = { - "a_w1.x", "a_w1.y", "a_w1.z", "a_w1.w", - "a_w2.x", "a_w2.y", "a_w2.z", "a_w2.w", - }; - - if (lang == HLSL_D3D11) { - if (numBoneWeights == 1) - WRITE(p, " mat3x4 skinMatrix = mul(In.a_w1, u_bone[0])"); - else - WRITE(p, " float4x3 skinMatrix = mul(In.a_w1.x, u_bone[0])"); - for (int i = 1; i < numBoneWeights; i++) { - const char *weightAttr = boneWeightAttr[i]; - // workaround for "cant do .x of scalar" issue - if (numBoneWeights == 1 && i == 0) weightAttr = "a_w1"; - if (numBoneWeights == 5 && i == 4) weightAttr = "a_w2"; - WRITE(p, " + mul(In.%s, u_bone[%i])", weightAttr, i); - } - } else { - if (numBoneWeights == 1) - WRITE(p, " float4x3 skinMatrix = mul(In.a_w1, u_bone0)"); - else - WRITE(p, " float4x3 skinMatrix = mul(In.a_w1.x, u_bone0)"); - for (int i = 1; i < numBoneWeights; i++) { - const char *weightAttr = boneWeightAttr[i]; - // workaround for "cant do .x of scalar" issue - if (numBoneWeights == 1 && i == 0) weightAttr = "a_w1"; - if (numBoneWeights == 5 && i == 4) weightAttr = "a_w2"; - WRITE(p, " + mul(In.%s, u_bone%i)", weightAttr, i); - } - } - - WRITE(p, ";\n"); - - // Trying to simplify this results in bugs in LBP... - WRITE(p, " vec3 skinnedpos = mul(float4(In.position.xyz, 1.0), skinMatrix);\n"); - WRITE(p, " vec3 worldpos = mul(float4(skinnedpos, 1.0), u_world);\n"); - - if (hasNormal) { - WRITE(p, " vec3 skinnednormal = mul(float4(%sIn.normal, 0.0), skinMatrix);\n", flipNormal ? "-" : ""); - } else { - WRITE(p, " vec3 skinnednormal = mul(float4(0.0, 0.0, %s1.0, 0.0), skinMatrix);\n", flipNormal ? "-" : ""); - } - WRITE(p, " vec3 worldnormal = normalize(mul(float4(skinnednormal, 0.0), u_world));\n"); - } - - WRITE(p, " vec4 viewPos = vec4(mul(vec4(worldpos, 1.0), u_view), 1.0);\n"); - - // Final view and projection transforms. - if (gstate_c.Supports(GPU_ROUND_DEPTH_TO_16BIT)) { - WRITE(p, " vec4 outPos = depthRoundZVP(mul(u_proj, viewPos));\n"); - } else { - WRITE(p, " vec4 outPos = mul(u_proj, viewPos);\n"); - } - - // TODO: Declare variables for dots for shade mapping if needed. - - const char *ambientStr = (matUpdate & 1) && hasColor ? "In.color0" : "u_matambientalpha"; - const char *diffuseStr = (matUpdate & 2) && hasColor ? "In.color0.rgb" : "u_matdiffuse"; - const char *specularStr = (matUpdate & 4) && hasColor ? "In.color0.rgb" : "u_matspecular.rgb"; - if (doBezier || doSpline) { - // TODO: Probably, should use hasColorTess but FF4 has a problem with drawing the background. - ambientStr = (matUpdate & 1) && hasColor ? "tess.col" : "u_matambientalpha"; - diffuseStr = (matUpdate & 2) && hasColor ? "tess.col.rgb" : "u_matdiffuse"; - specularStr = (matUpdate & 4) && hasColor ? "tess.col.rgb" : "u_matspecular.rgb"; - } - - bool diffuseIsZero = true; - bool specularIsZero = true; - bool distanceNeeded = false; - bool anySpots = false; - if (enableLighting) { - WRITE(p, " vec4 lightSum0 = u_ambient * %s + float4(u_matemissive, 0.0);\n", ambientStr); - - for (int i = 0; i < 4; 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 (doLight[i] != LIGHT_FULL) - continue; - diffuseIsZero = false; - if (comp == GE_LIGHTCOMP_BOTH) - specularIsZero = false; - if (type != GE_LIGHTTYPE_DIRECTIONAL) - distanceNeeded = true; - if (type == GE_LIGHTTYPE_SPOT || type == GE_LIGHTTYPE_UNKNOWN) - anySpots = true; - } - - if (!specularIsZero) { - WRITE(p, " vec3 lightSum1 = 0;\n"); - } - if (!diffuseIsZero) { - WRITE(p, " vec3 toLight;\n"); - WRITE(p, " vec3 diffuse;\n"); - } - if (distanceNeeded) { - WRITE(p, " float distance;\n"); - WRITE(p, " float lightScale;\n"); - } - WRITE(p, " float ldot;\n"); - if (anySpots) { - WRITE(p, " float angle;\n"); - } - } - - // Calculate lights if needed. If shade mapping is enabled, lights may need to be - // at least partially calculated. - for (int i = 0; i < 4; i++) { - if (doLight[i] != LIGHT_FULL) - continue; - - 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) { - // We prenormalize light positions for directional lights. - WRITE(p, " toLight = u_lightpos%i;\n", i); - } else { - WRITE(p, " toLight = u_lightpos%i - worldpos;\n", i); - WRITE(p, " distance = length(toLight);\n"); - WRITE(p, " toLight /= distance;\n"); - } - - bool doSpecular = comp == GE_LIGHTCOMP_BOTH; - bool poweredDiffuse = comp == GE_LIGHTCOMP_ONLYPOWDIFFUSE; - - WRITE(p, " ldot = dot(toLight, worldnormal);\n"); - if (poweredDiffuse) { - // pow(0.0, 0.0) may be undefined, but the PSP seems to treat it as 1.0. - // Seen in Tales of the World: Radiant Mythology (#2424.) - WRITE(p, " if (u_matspecular.a <= 0.0) {\n"); - WRITE(p, " ldot = 1.0;\n"); - WRITE(p, " } else {\n"); - WRITE(p, " ldot = pow(max(ldot, 0.0), u_matspecular.a);\n"); - WRITE(p, " }\n"); - } - - const char *timesLightScale = " * lightScale"; - - // Attenuation - switch (type) { - case GE_LIGHTTYPE_DIRECTIONAL: - timesLightScale = ""; - break; - case GE_LIGHTTYPE_POINT: - WRITE(p, " lightScale = clamp(1.0 / dot(u_lightatt%i, vec3(1.0, distance, distance*distance)), 0.0, 1.0);\n", i); - break; - case GE_LIGHTTYPE_SPOT: - case GE_LIGHTTYPE_UNKNOWN: - WRITE(p, " angle = length(u_lightdir%i) == 0.0 ? 0.0 : dot(normalize(u_lightdir%i), toLight);\n", i, i); - WRITE(p, " if (angle >= u_lightangle_spotCoef%i.x) {\n", i); - WRITE(p, " lightScale = clamp(1.0 / dot(u_lightatt%i, vec3(1.0, distance, distance*distance)), 0.0, 1.0) * (u_lightangle_spotCoef%i.y <= 0.0 ? 1.0 : pow(angle, u_lightangle_spotCoef%i.y));\n", i, i, i); - WRITE(p, " } else {\n"); - WRITE(p, " lightScale = 0.0;\n"); - WRITE(p, " }\n"); - break; - default: - // ILLEGAL - break; - } - - WRITE(p, " diffuse = (u_lightdiffuse%i * %s) * max(ldot, 0.0);\n", i, diffuseStr); - if (doSpecular) { - WRITE(p, " if (ldot >= 0.0) {\n"); - WRITE(p, " ldot = dot(normalize(toLight + vec3(0.0, 0.0, 1.0)), worldnormal);\n"); - WRITE(p, " if (u_matspecular.a <= 0.0) {\n"); - WRITE(p, " ldot = 1.0;\n"); - WRITE(p, " } else {\n"); - WRITE(p, " ldot = pow(max(ldot, 0.0), u_matspecular.a);\n"); - WRITE(p, " }\n"); - WRITE(p, " if (ldot > 0.0)\n"); - WRITE(p, " lightSum1 += u_lightspecular%i * %s * ldot %s;\n", i, specularStr, timesLightScale); - WRITE(p, " }\n"); - } - WRITE(p, " lightSum0.rgb += (u_lightambient%i * %s.rgb + diffuse)%s;\n", i, ambientStr, timesLightScale); - } - - if (enableLighting) { - // Sum up ambient, emissive here. - if (lmode) { - WRITE(p, " Out.v_color0 = clamp(lightSum0, 0.0, 1.0);\n"); - // v_color1 only exists when lmode = 1. - if (specularIsZero) { - WRITE(p, " Out.v_color1 = splat3(0.0);\n"); - } else { - WRITE(p, " Out.v_color1 = clamp(lightSum1, 0.0, 1.0);\n"); - } - } else { - if (specularIsZero) { - WRITE(p, " Out.v_color0 = clamp(lightSum0, 0.0, 1.0);\n"); - } else { - WRITE(p, " Out.v_color0 = clamp(clamp(lightSum0, 0.0, 1.0) + float4(lightSum1, 0.0), 0.0, 1.0);\n"); - } - } - } else { - // Lighting doesn't affect color. - if (hasColor) { - if (doBezier || doSpline) - WRITE(p, " Out.v_color0 = tess.col;\n"); - else - WRITE(p, " Out.v_color0 = In.color0;\n"); - } else { - WRITE(p, " Out.v_color0 = u_matambientalpha;\n"); - } - if (lmode) - WRITE(p, " Out.v_color1 = splat3(0.0);\n"); - } - - // Step 3: UV generation - if (doTexture) { - switch (uvGenMode) { - case GE_TEXMAP_TEXTURE_COORDS: // Scale-offset. Easy. - case GE_TEXMAP_UNKNOWN: // Not sure what this is, but Riviera uses it. Treating as coords works. - if (scaleUV) { - if (hasTexcoord) { - if (doBezier || doSpline) - WRITE(p, " Out.v_texcoord = vec3(tess.tex.xy * u_uvscaleoffset.xy + u_uvscaleoffset.zw, 0.0);\n"); - else - WRITE(p, " Out.v_texcoord = vec3(In.texcoord.xy * u_uvscaleoffset.xy, 0.0);\n"); - } else { - WRITE(p, " Out.v_texcoord = splat3(0.0);\n"); - } - } else { - if (hasTexcoord) { - WRITE(p, " Out.v_texcoord = vec3(In.texcoord.xy * u_uvscaleoffset.xy + u_uvscaleoffset.zw, 0.0);\n"); - } else { - WRITE(p, " Out.v_texcoord = vec3(u_uvscaleoffset.zw, 0.0);\n"); - } - } - break; - - case GE_TEXMAP_TEXTURE_MATRIX: // Projection mapping. - { - std::string temp_tc; - switch (uvProjMode) { - case GE_PROJMAP_POSITION: // Use model space XYZ as source - temp_tc = "vec4(In.position.xyz, 1.0)"; - break; - case GE_PROJMAP_UV: // Use unscaled UV as source - { - if (hasTexcoord) { - temp_tc = StringFromFormat("vec4(In.texcoord.xy, 0.0, 1.0)"); - } else { - temp_tc = "vec4(0.0, 0.0, 0.0, 1.0)"; - } - } - break; - case GE_PROJMAP_NORMALIZED_NORMAL: // Use normalized transformed normal as source - if (hasNormal) - temp_tc = flipNormal ? "vec4(normalize(-In.normal), 1.0)" : "vec4(normalize(In.normal), 1.0)"; - else - temp_tc = "vec4(0.0, 0.0, 1.0, 1.0)"; - break; - case GE_PROJMAP_NORMAL: // Use non-normalized transformed normal as source - if (hasNormal) - temp_tc = flipNormal ? "vec4(-In.normal, 1.0)" : "vec4(In.normal, 1.0)"; - else - temp_tc = "vec4(0.0, 0.0, 1.0, 1.0)"; - break; - } - // Transform by texture matrix. XYZ as we are doing projection mapping. - WRITE(p, " Out.v_texcoord.xyz = mul(%s, u_texmtx) * vec3(u_uvscaleoffset.xy, 1.0);\n", temp_tc.c_str()); - } - break; - - case GE_TEXMAP_ENVIRONMENT_MAP: // Shade mapping - use dots from light sources. - { - std::string lightFactor0 = StringFromFormat("(length(u_lightpos%i) == 0.0 ? worldnormal.z : dot(normalize(u_lightpos%i), worldnormal))", ls0, ls0); - std::string lightFactor1 = StringFromFormat("(length(u_lightpos%i) == 0.0 ? worldnormal.z : dot(normalize(u_lightpos%i), worldnormal))", ls1, ls1); - WRITE(p, " Out.v_texcoord = vec3(u_uvscaleoffset.xy * vec2(1.0 + %s, 1.0 + %s) * 0.5, 1.0);\n", lightFactor0.c_str(), lightFactor1.c_str()); - } - break; - - default: - // Should be unreachable. - _assert_(false); - return false; - } - } - - // Compute fogdepth - if (enableFog) { - WRITE(p, " Out.v_fogdepth = (viewPos.z + u_fogcoef.x) * u_fogcoef.y;\n"); - } - } - - if (!isModeThrough && gstate_c.Supports(GPU_SUPPORTS_VS_RANGE_CULLING)) { - WRITE(p, " vec3 projPos = outPos.xyz / outPos.w;\n"); - // Vertex range culling doesn't happen when depth is clamped, so only do this if in range. - WRITE(p, " if (u_cullRangeMin.w <= 0.0 || (projPos.z >= u_cullRangeMin.z && projPos.z <= u_cullRangeMax.z)) {\n"); - const char *outMin = "projPos.x < u_cullRangeMin.x || projPos.y < u_cullRangeMin.y || projPos.z < u_cullRangeMin.z"; - const char *outMax = "projPos.x > u_cullRangeMax.x || projPos.y > u_cullRangeMax.y || projPos.z > u_cullRangeMax.z"; - WRITE(p, " if (%s || %s) {\n", outMin, outMax); - WRITE(p, " outPos.w = u_cullRangeMax.w;\n"); - WRITE(p, " }\n"); - WRITE(p, " }\n"); - } - WRITE(p, " Out.gl_Position = outPos;\n"); - - WRITE(p, " return Out;\n"); - WRITE(p, "}\n"); - return true; -} diff --git a/GPU/GLES/ShaderManagerGLES.cpp b/GPU/GLES/ShaderManagerGLES.cpp index 44ce55e9dc..7e77133a85 100644 --- a/GPU/GLES/ShaderManagerGLES.cpp +++ b/GPU/GLES/ShaderManagerGLES.cpp @@ -721,7 +721,7 @@ Shader *ShaderManagerGLES::CompileVertexShader(VShaderID VSID) { uint32_t attrMask; uint64_t uniformMask; std::string errorString; - if (!GenerateVertexShaderGLSL(VSID, codeBuffer_, compat_, &attrMask, &uniformMask, &errorString)) { + if (!GenerateVertexShader(VSID, codeBuffer_, compat_, &attrMask, &uniformMask, &errorString)) { ERROR_LOG(G3D, "Shader gen error: %s", errorString.c_str()); return nullptr; } diff --git a/GPU/GLES/ShaderManagerGLES.h b/GPU/GLES/ShaderManagerGLES.h index a075530ecd..2343f7d508 100644 --- a/GPU/GLES/ShaderManagerGLES.h +++ b/GPU/GLES/ShaderManagerGLES.h @@ -23,24 +23,11 @@ #include "Common/GPU/OpenGL/GLRenderManager.h" #include "GPU/Common/ShaderCommon.h" #include "GPU/Common/ShaderId.h" -#include "GPU/GLES/VertexShaderGeneratorGLES.h" +#include "GPU/Common/VertexShaderGenerator.h" #include "GPU/Common/FragmentShaderGenerator.h" class Shader; -// Pre-fetched attrs and uniforms -enum { - ATTR_POSITION = 0, - ATTR_TEXCOORD = 1, - ATTR_NORMAL = 2, - ATTR_W1 = 3, - ATTR_W2 = 4, - ATTR_COLOR0 = 5, - ATTR_COLOR1 = 6, - - ATTR_COUNT, -}; - class LinkedShader { public: LinkedShader(GLRenderManager *render, VShaderID VSID, Shader *vs, FShaderID FSID, Shader *fs, bool useHWTransform, bool preloading = false); diff --git a/GPU/GLES/VertexShaderGeneratorGLES.h b/GPU/GLES/VertexShaderGeneratorGLES.h deleted file mode 100644 index df42bd4d11..0000000000 --- a/GPU/GLES/VertexShaderGeneratorGLES.h +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) 2012- PPSSPP Project. - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, version 2.0 or later versions. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License 2.0 for more details. - -// A copy of the GPL 2.0 should have been included with the program. -// If not, see http://www.gnu.org/licenses/ - -// Official git repository and contact information can be found at -// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. - -#pragma once - -#include "Common/CommonTypes.h" - -struct VShaderID; - -bool GenerateVertexShaderGLSL(const VShaderID &id, char *buffer, const ShaderLanguageDesc &compat, uint32_t *attrMask, uint64_t *uniformMask, std::string *errorString); diff --git a/GPU/GPU.vcxproj b/GPU/GPU.vcxproj index 09288fdf97..675602d6d5 100644 --- a/GPU/GPU.vcxproj +++ b/GPU/GPU.vcxproj @@ -368,6 +368,7 @@ + @@ -390,7 +391,6 @@ - @@ -447,12 +447,6 @@ true true - - true - true - true - true - @@ -537,6 +531,7 @@ true true + @@ -561,7 +556,6 @@ - @@ -630,12 +624,6 @@ true true - - true - true - true - true - diff --git a/GPU/GPU.vcxproj.filters b/GPU/GPU.vcxproj.filters index ee96efb190..509e962588 100644 --- a/GPU/GPU.vcxproj.filters +++ b/GPU/GPU.vcxproj.filters @@ -189,9 +189,6 @@ GLES - - GLES - GLES @@ -267,12 +264,12 @@ Common - - DirectX9 - Common + + Common + @@ -446,9 +443,6 @@ GLES - - GLES - GLES @@ -539,11 +533,11 @@ Common - - DirectX9 - Common + + Common + \ No newline at end of file diff --git a/GPU/Vulkan/ShaderManagerVulkan.cpp b/GPU/Vulkan/ShaderManagerVulkan.cpp index e1c6cbee59..4b54a77d83 100644 --- a/GPU/Vulkan/ShaderManagerVulkan.cpp +++ b/GPU/Vulkan/ShaderManagerVulkan.cpp @@ -36,11 +36,11 @@ #include "GPU/Math3D.h" #include "GPU/GPUState.h" #include "GPU/ge_constants.h" +#include "GPU/Common/FragmentShaderGenerator.h" +#include "GPU/Common/VertexShaderGenerator.h" #include "GPU/Vulkan/ShaderManagerVulkan.h" #include "GPU/Vulkan/DrawEngineVulkan.h" #include "GPU/Vulkan/FramebufferManagerVulkan.h" -#include "GPU/Common/FragmentShaderGenerator.h" -#include "GPU/GLES/VertexShaderGeneratorGLES.h" VulkanFragmentShader::VulkanFragmentShader(VulkanContext *vulkan, FShaderID id, const char *code) : vulkan_(vulkan), id_(id), failed_(false), module_(0) { @@ -265,7 +265,7 @@ void ShaderManagerVulkan::GetShaders(int prim, u32 vertType, VulkanVertexShader std::string genErrorString; uint64_t uniformMask = 0; // Not used uint32_t attributeMask = 0; // Not used - bool success = GenerateVertexShaderGLSL(VSID, codeBuffer_, compat_, &attributeMask, &uniformMask, &genErrorString); + bool success = GenerateVertexShader(VSID, codeBuffer_, compat_, &attributeMask, &uniformMask, &genErrorString); _assert_(success); vs = new VulkanVertexShader(vulkan_, VSID, codeBuffer_, useHWTransform); vsCache_.Insert(VSID, vs); @@ -398,7 +398,7 @@ bool ShaderManagerVulkan::LoadCache(FILE *f) { std::string genErrorString; uint32_t attributeMask = 0; uint64_t uniformMask = 0; - if (!GenerateVertexShaderGLSL(id, codeBuffer_, compat_, &attributeMask, &uniformMask, &genErrorString)) { + if (!GenerateVertexShader(id, codeBuffer_, compat_, &attributeMask, &uniformMask, &genErrorString)) { return false; } VulkanVertexShader *vs = new VulkanVertexShader(vulkan_, id, codeBuffer_, useHWTransform); diff --git a/GPU/Vulkan/ShaderManagerVulkan.h b/GPU/Vulkan/ShaderManagerVulkan.h index d48c7dcd99..e35a07a103 100644 --- a/GPU/Vulkan/ShaderManagerVulkan.h +++ b/GPU/Vulkan/ShaderManagerVulkan.h @@ -24,7 +24,7 @@ #include "Common/GPU/Vulkan/VulkanMemory.h" #include "GPU/Common/ShaderCommon.h" #include "GPU/Common/ShaderId.h" -#include "GPU/GLES/VertexShaderGeneratorGLES.h" +#include "GPU/Common/VertexShaderGenerator.h" #include "GPU/Common/FragmentShaderGenerator.h" #include "GPU/Vulkan/VulkanUtil.h" #include "Common/Math/lin/matrix4x4.h" diff --git a/UWP/GPU_UWP/GPU_UWP.vcxproj b/UWP/GPU_UWP/GPU_UWP.vcxproj index 5fd5c99998..d5b5d3167e 100644 --- a/UWP/GPU_UWP/GPU_UWP.vcxproj +++ b/UWP/GPU_UWP/GPU_UWP.vcxproj @@ -402,6 +402,7 @@ + @@ -417,7 +418,6 @@ - @@ -462,6 +462,7 @@ + @@ -477,7 +478,6 @@ - diff --git a/UWP/GPU_UWP/GPU_UWP.vcxproj.filters b/UWP/GPU_UWP/GPU_UWP.vcxproj.filters index 331a8c82e6..12f33ec3af 100644 --- a/UWP/GPU_UWP/GPU_UWP.vcxproj.filters +++ b/UWP/GPU_UWP/GPU_UWP.vcxproj.filters @@ -41,7 +41,6 @@ - @@ -57,6 +56,7 @@ + @@ -96,7 +96,6 @@ - @@ -114,5 +113,6 @@ + \ No newline at end of file diff --git a/android/jni/Android.mk b/android/jni/Android.mk index d9f3a34883..71e6915927 100644 --- a/android/jni/Android.mk +++ b/android/jni/Android.mk @@ -326,6 +326,7 @@ EXEC_AND_LIB_FILES := \ $(SRC)/GPU/Common/TextureDecoder.cpp \ $(SRC)/GPU/Common/PostShader.cpp \ $(SRC)/GPU/Common/ShaderUniforms.cpp \ + $(SRC)/GPU/Common/VertexShaderGenerator.cpp \ $(SRC)/GPU/Debugger/Breakpoints.cpp \ $(SRC)/GPU/Debugger/Debugger.cpp \ $(SRC)/GPU/Debugger/Playback.cpp \ @@ -333,14 +334,13 @@ EXEC_AND_LIB_FILES := \ $(SRC)/GPU/Debugger/Stepping.cpp \ $(SRC)/GPU/GLES/FramebufferManagerGLES.cpp \ $(SRC)/GPU/GLES/DepalettizeShaderGLES.cpp \ - $(SRC)/GPU/GLES/DepthBufferGLES.cpp.arm \ + $(SRC)/GPU/GLES/DepthBufferGLES.cpp \ $(SRC)/GPU/GLES/GPU_GLES.cpp.arm \ - $(SRC)/GPU/GLES/StencilBufferGLES.cpp.arm \ + $(SRC)/GPU/GLES/StencilBufferGLES.cpp \ $(SRC)/GPU/GLES/TextureCacheGLES.cpp.arm \ $(SRC)/GPU/GLES/DrawEngineGLES.cpp.arm \ $(SRC)/GPU/GLES/StateMappingGLES.cpp.arm \ $(SRC)/GPU/GLES/ShaderManagerGLES.cpp.arm \ - $(SRC)/GPU/GLES/VertexShaderGeneratorGLES.cpp.arm \ $(SRC)/GPU/GLES/FragmentTestCacheGLES.cpp.arm \ $(SRC)/GPU/GLES/TextureScalerGLES.cpp \ $(SRC)/GPU/Null/NullGpu.cpp \ diff --git a/libretro/Makefile.common b/libretro/Makefile.common index ec02dcfe44..3120c903c8 100644 --- a/libretro/Makefile.common +++ b/libretro/Makefile.common @@ -249,6 +249,7 @@ SOURCES_CXX += \ $(GPUDIR)/Debugger/Record.cpp \ $(GPUDIR)/Debugger/Stepping.cpp \ $(GPUDIR)/Common/FragmentShaderGenerator.cpp \ + $(GPUDIR)/Common/VertexShaderGenerator.cpp \ $(GPUDIR)/Common/TextureCacheCommon.cpp \ $(GPUDIR)/Common/TextureScalerCommon.cpp \ $(GPUDIR)/Common/SoftwareTransformCommon.cpp \ @@ -269,7 +270,6 @@ SOURCES_CXX += \ $(GPUDIR)/Software/RasterizerRectangle.cpp \ $(GPUDIR)/GLES/DepalettizeShaderGLES.cpp \ $(GPUDIR)/GLES/DepthBufferGLES.cpp \ - $(GPUDIR)/GLES/VertexShaderGeneratorGLES.cpp \ $(GPUDIR)/GLES/DrawEngineGLES.cpp \ $(GPUDIR)/GLES/GPU_GLES.cpp \ $(GPUDIR)/GLES/FragmentTestCacheGLES.cpp \ diff --git a/unittest/TestShaderGenerators.cpp b/unittest/TestShaderGenerators.cpp index bd91558ca1..645069172e 100644 --- a/unittest/TestShaderGenerators.cpp +++ b/unittest/TestShaderGenerators.cpp @@ -10,9 +10,7 @@ #include "GPU/Vulkan/VulkanContext.h" #include "GPU/Common/FragmentShaderGenerator.h" - -#include "GPU/Directx9/VertexShaderGeneratorHLSL.h" -#include "GPU/GLES/VertexShaderGeneratorGLES.h" +#include "GPU/Common/VertexShaderGenerator.h" #include "GPU/D3D11/D3D11Util.h" #include "GPU/D3D11/D3D11Loader.h" @@ -60,27 +58,27 @@ bool GenerateVShader(VShaderID id, char *buffer, ShaderLanguage lang, std::strin case ShaderLanguage::GLSL_VULKAN: { ShaderLanguageDesc compat(ShaderLanguage::GLSL_VULKAN); - return GenerateVertexShaderGLSL(id, buffer, compat, &attrMask, &uniformMask, errorString); + return GenerateVertexShader(id, buffer, compat, &attrMask, &uniformMask, errorString); } case ShaderLanguage::GLSL_140: { ShaderLanguageDesc compat(ShaderLanguage::GLSL_140); - return GenerateVertexShaderGLSL(id, buffer, compat, &attrMask, &uniformMask, errorString); + return GenerateVertexShader(id, buffer, compat, &attrMask, &uniformMask, errorString); } case ShaderLanguage::GLSL_300: { ShaderLanguageDesc compat(ShaderLanguage::GLSL_140); - return GenerateVertexShaderGLSL(id, buffer, compat, &attrMask, &uniformMask, errorString); + return GenerateVertexShader(id, buffer, compat, &attrMask, &uniformMask, errorString); } case ShaderLanguage::HLSL_D3D9: { - //ShaderLanguageDesc compat(ShaderLanguage::HLSL_D3D9); - return GenerateVertexShaderHLSL(id, buffer, ShaderLanguage::HLSL_D3D9, errorString); + ShaderLanguageDesc compat(ShaderLanguage::HLSL_D3D9); + return GenerateVertexShader(id, buffer, compat, &attrMask, &uniformMask, errorString); } case ShaderLanguage::HLSL_D3D11: { - //ShaderLanguageDesc compat(ShaderLanguage::HLSL_D3D11); - return GenerateVertexShaderHLSL(id, buffer, ShaderLanguage::HLSL_D3D11, errorString); + ShaderLanguageDesc compat(ShaderLanguage::HLSL_D3D11); + return GenerateVertexShader(id, buffer, compat, &attrMask, &uniformMask, errorString); } default: return false; @@ -167,6 +165,56 @@ 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; + + // The generated bits need some adjustment: + + // We don't use these bits in the HLSL shader generator. + id.SetBits(VS_BIT_WEIGHT_FMTSCALE, 2, 0); + // If mode is through, we won't do hardware transform. + if (id.Bit(VS_BIT_IS_THROUGH)) { + id.SetBit(VS_BIT_USE_HW_TRANSFORM, 0); + } + if (!id.Bit(VS_BIT_USE_HW_TRANSFORM)) { + id.SetBit(VS_BIT_ENABLE_BONES, 0); + } + + 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()); + } + } + + // 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]) { + std::string errorMessage; + if (!TestCompileShader(buffer[j], languages[j], true, &errorMessage)) { + printf("Error compiling vertex shader %d:\n\n%s\n\n%s\n", (int)j, LineNumberString(buffer[j]).c_str(), errorMessage.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++) { @@ -195,24 +243,6 @@ bool TestShaderGenerators() { // We ignore the contents of the error string here, not even gonna try to compile if it errors. } - // KEEPING FOR REUSE LATER: Defunct temporary test. - /* - 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! a=glsl b=hlsl\n"); - PrintDiff(buffer[0], buffer[1]); - return 1; - } - if (generateSuccess[2] && strcmp(buffer[2], buffer[3])) { - printf("mismatching shaders! a=glsl b=hlsl\n"); - PrintDiff(buffer[2], buffer[3]); - 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++) { @@ -232,58 +262,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; - - 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()); - } - } - - // KEEPING FOR REUSE LATER: Defunct temporary test: Compare GLSL-in-Vulkan-mode vs Vulkan - 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 false; - } - if (generateSuccess[0] && strcmp(buffer[0], buffer[1])) { - printf("mismatching shaders!\n"); - PrintDiff(buffer[0], buffer[1]); - return false; - } - - // 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]) { - std::string errorMessage; - if (!TestCompileShader(buffer[j], languages[j], true, &errorMessage)) { - printf("Error compiling vertex shader:\n\n%s\n\n%s\n", LineNumberString(buffer[j]).c_str(), errorMessage.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; - */ _CrtCheckMemory();