From 901942f24a20f4696aed72a4e721553c00f569b9 Mon Sep 17 00:00:00 2001 From: kd-11 Date: Mon, 14 Oct 2019 00:30:34 +0300 Subject: [PATCH] rsx: Replace pointless f32[4] restriction on texture parameters. - Use a struct instead to improve readability and remove pointless OpBitCast --- rpcs3/Emu/RSX/Common/GLSLCommon.h | 61 ++++++++++++++++---------- rpcs3/Emu/RSX/GL/GLFragmentProgram.cpp | 4 +- rpcs3/Emu/RSX/RSXThread.cpp | 2 +- rpcs3/Emu/RSX/VK/VKFragmentProgram.cpp | 4 +- 4 files changed, 44 insertions(+), 27 deletions(-) diff --git a/rpcs3/Emu/RSX/Common/GLSLCommon.h b/rpcs3/Emu/RSX/Common/GLSLCommon.h index 700f66ff47..0e50f097d3 100644 --- a/rpcs3/Emu/RSX/Common/GLSLCommon.h +++ b/rpcs3/Emu/RSX/Common/GLSLCommon.h @@ -608,12 +608,11 @@ namespace glsl " }\n" "}\n\n" - "vec4 texture2DReconstruct(sampler2D tex, usampler2D stencil_tex, const in vec2 coord, const in float remap)\n" + "vec4 texture2DReconstruct(sampler2D tex, usampler2D stencil_tex, const in vec2 coord, const in uint remap)\n" "{\n" - " uint control_bits = floatBitsToUint(remap);\n" - " vec4 result = decode_depth24(texture(tex, coord.xy).r, control_bits >> 16);\n" + " vec4 result = decode_depth24(texture(tex, coord.xy).r, remap >> 16);\n" " result.z = float(texture(stencil_tex, coord.xy).x) / 255.f;\n" - " uint remap_vector = control_bits & 0xFF;\n" + " uint remap_vector = remap & 0xFF;\n" " if (remap_vector == 0xE4) return result;\n\n" " vec4 tmp;\n" " uint remap_a = remap_vector & 0x3;\n" @@ -706,39 +705,39 @@ namespace glsl "#define TEX_NAME(index) tex##index\n" "#define TEX_NAME_STENCIL(index) tex##index##_stencil\n\n" - "#define TEX1D(index, coord1) process_texel(texture(TEX_NAME(index), coord1 * texture_parameters[index].x), floatBitsToUint(texture_parameters[index].w))\n" - "#define TEX1D_BIAS(index, coord1, bias) process_texel(texture(TEX_NAME(index), coord1 * texture_parameters[index].x, bias), floatBitsToUint(texture_parameters[index].w))\n" - "#define TEX1D_LOD(index, coord1, lod) process_texel(textureLod(TEX_NAME(index), coord1 * texture_parameters[index].x, lod), floatBitsToUint(texture_parameters[index].w))\n" - "#define TEX1D_GRAD(index, coord1, dpdx, dpdy) process_texel(textureGrad(TEX_NAME(index), coord1 * texture_parameters[index].x, dpdx, dpdy), floatBitsToUint(texture_parameters[index].w))\n" - "#define TEX1D_PROJ(index, coord2) process_texel(textureProj(TEX_NAME(index), coord2 * vec2(texture_parameters[index].x, 1.)), floatBitsToUint(texture_parameters[index].w))\n" + "#define TEX1D(index, coord1) process_texel(texture(TEX_NAME(index), coord1 * texture_parameters[index].scale.x), texture_parameters[index].flags)\n" + "#define TEX1D_BIAS(index, coord1, bias) process_texel(texture(TEX_NAME(index), coord1 * texture_parameters[index].scale.x, bias), texture_parameters[index].flags)\n" + "#define TEX1D_LOD(index, coord1, lod) process_texel(textureLod(TEX_NAME(index), coord1 * texture_parameters[index].scale.x, lod), texture_parameters[index].flags)\n" + "#define TEX1D_GRAD(index, coord1, dpdx, dpdy) process_texel(textureGrad(TEX_NAME(index), coord1 * texture_parameters[index].scale.x, dpdx, dpdy), texture_parameters[index].flags)\n" + "#define TEX1D_PROJ(index, coord2) process_texel(textureProj(TEX_NAME(index), coord2 * vec2(texture_parameters[index].scale.x, 1.)), texture_parameters[index].flags)\n" - "#define TEX2D(index, coord2) process_texel(texture(TEX_NAME(index), coord2 * texture_parameters[index].xy), floatBitsToUint(texture_parameters[index].w))\n" - "#define TEX2D_BIAS(index, coord2, bias) process_texel(texture(TEX_NAME(index), coord2 * texture_parameters[index].xy, bias), floatBitsToUint(texture_parameters[index].w))\n" - "#define TEX2D_LOD(index, coord2, lod) process_texel(textureLod(TEX_NAME(index), coord2 * texture_parameters[index].xy, lod), floatBitsToUint(texture_parameters[index].w))\n" - "#define TEX2D_GRAD(index, coord2, dpdx, dpdy) process_texel(textureGrad(TEX_NAME(index), coord2 * texture_parameters[index].xy, dpdx, dpdy), floatBitsToUint(texture_parameters[index].w))\n" - "#define TEX2D_PROJ(index, coord4) process_texel(textureProj(TEX_NAME(index), coord4 * vec4(texture_parameters[index].xy, 1., 1.)), floatBitsToUint(texture_parameters[index].w))\n" + "#define TEX2D(index, coord2) process_texel(texture(TEX_NAME(index), coord2 * texture_parameters[index].scale), texture_parameters[index].flags)\n" + "#define TEX2D_BIAS(index, coord2, bias) process_texel(texture(TEX_NAME(index), coord2 * texture_parameters[index].scale, bias), texture_parameters[index].flags)\n" + "#define TEX2D_LOD(index, coord2, lod) process_texel(textureLod(TEX_NAME(index), coord2 * texture_parameters[index].scale, lod), texture_parameters[index].flags)\n" + "#define TEX2D_GRAD(index, coord2, dpdx, dpdy) process_texel(textureGrad(TEX_NAME(index), coord2 * texture_parameters[index].scale, dpdx, dpdy), texture_parameters[index].flags)\n" + "#define TEX2D_PROJ(index, coord4) process_texel(textureProj(TEX_NAME(index), coord4 * vec4(texture_parameters[index].scale, 1., 1.)), texture_parameters[index].flags)\n" - "#define TEX2D_DEPTH_RGBA8(index, coord2) process_texel(texture2DReconstruct(TEX_NAME(index), TEX_NAME_STENCIL(index), coord2 * texture_parameters[index].xy, texture_parameters[index].z), floatBitsToUint(texture_parameters[index].w))\n"; + "#define TEX2D_DEPTH_RGBA8(index, coord2) process_texel(texture2DReconstruct(TEX_NAME(index), TEX_NAME_STENCIL(index), coord2 * texture_parameters[index].scale, texture_parameters[index].remap), texture_parameters[index].flags)\n"; if (props.emulate_shadow_compare) { OS << - "#define TEX2D_SHADOW(index, coord3) shadowCompare(TEX_NAME(index), coord3 * vec3(texture_parameters[index].xy, 1.), floatBitsToUint(texture_parameters[index].w) >> 8)\n" - "#define TEX2D_SHADOWPROJ(index, coord4) shadowCompareProj(TEX_NAME(index), coord4 * vec4(texture_parameters[index].xy, 1., 1.), floatBitsToUint(texture_parameters[index].w) >> 8)\n"; + "#define TEX2D_SHADOW(index, coord3) shadowCompare(TEX_NAME(index), coord3 * vec3(texture_parameters[index].scale, 1.), texture_parameters[index].flags >> 8)\n" + "#define TEX2D_SHADOWPROJ(index, coord4) shadowCompareProj(TEX_NAME(index), coord4 * vec4(texture_parameters[index].scale, 1., 1.), texture_parameters[index].flags >> 8)\n"; } else { OS << - "#define TEX2D_SHADOW(index, coord3) texture(TEX_NAME(index), coord3 * vec3(texture_parameters[index].xy, 1.))\n" - "#define TEX2D_SHADOWPROJ(index, coord4) textureProj(TEX_NAME(index), coord4 * vec4(texture_parameters[index].xy, 1., 1.))\n"; + "#define TEX2D_SHADOW(index, coord3) texture(TEX_NAME(index), coord3 * vec3(texture_parameters[index].scale, 1.))\n" + "#define TEX2D_SHADOWPROJ(index, coord4) textureProj(TEX_NAME(index), coord4 * vec4(texture_parameters[index].scale, 1., 1.))\n"; } OS << - "#define TEX3D(index, coord3) process_texel(texture(TEX_NAME(index), coord3), floatBitsToUint(texture_parameters[index].w))\n" - "#define TEX3D_BIAS(index, coord3, bias) process_texel(texture(TEX_NAME(index), coord3, bias), floatBitsToUint(texture_parameters[index].w))\n" - "#define TEX3D_LOD(index, coord3, lod) process_texel(textureLod(TEX_NAME(index), coord3, lod), floatBitsToUint(texture_parameters[index].w))\n" - "#define TEX3D_GRAD(index, coord3, dpdx, dpdy) process_texel(textureGrad(TEX_NAME(index), coord3, dpdx, dpdy), floatBitsToUint(texture_parameters[index].w))\n" - "#define TEX3D_PROJ(index, coord4) process_texel(textureProj(TEX_NAME(index), coord4), floatBitsToUint(texture_parameters[index].w))\n\n"; + "#define TEX3D(index, coord3) process_texel(texture(TEX_NAME(index), coord3), texture_parameters[index].flags)\n" + "#define TEX3D_BIAS(index, coord3, bias) process_texel(texture(TEX_NAME(index), coord3, bias), texture_parameters[index].flags)\n" + "#define TEX3D_LOD(index, coord3, lod) process_texel(textureLod(TEX_NAME(index), coord3, lod), texture_parameters[index].flags)\n" + "#define TEX3D_GRAD(index, coord3, dpdx, dpdy) process_texel(textureGrad(TEX_NAME(index), coord3, dpdx, dpdy), texture_parameters[index].flags)\n" + "#define TEX3D_PROJ(index, coord4) process_texel(textureProj(TEX_NAME(index), coord4), texture_parameters[index].flags)\n\n"; } if (props.require_wpos) @@ -840,4 +839,18 @@ namespace glsl return "TEX2D_DEPTH_RGBA8($_i, $0.xy)"; } } + + static void insert_subheader_block(std::ostream& OS) + { + // Global types and stuff + // Must be compatible with std140 packing rules + OS << + "struct sampler_info\n" + "{\n" + " vec2 scale;\n" + " uint remap;\n" + " uint flags;\n" + "};\n" + "\n"; + } } diff --git a/rpcs3/Emu/RSX/GL/GLFragmentProgram.cpp b/rpcs3/Emu/RSX/GL/GLFragmentProgram.cpp index ccc8ef72a2..f1eafcb795 100644 --- a/rpcs3/Emu/RSX/GL/GLFragmentProgram.cpp +++ b/rpcs3/Emu/RSX/GL/GLFragmentProgram.cpp @@ -44,6 +44,8 @@ void GLFragmentDecompilerThread::insertHeader(std::stringstream & OS) OS << "#extension GL_AMD_gpu_shader_half_float: require\n"; } } + + glsl::insert_subheader_block(OS); } void GLFragmentDecompilerThread::insertInputs(std::stringstream & OS) @@ -189,7 +191,7 @@ void GLFragmentDecompilerThread::insertConstants(std::stringstream & OS) OS << "layout(std140, binding = 5) uniform TextureParametersBuffer\n"; OS << "{\n"; - OS << " vec4 texture_parameters[16];\n"; //sampling: x,y scaling and (unused) offsets data + OS << " sampler_info texture_parameters[16];\n"; OS << "};\n\n"; } diff --git a/rpcs3/Emu/RSX/RSXThread.cpp b/rpcs3/Emu/RSX/RSXThread.cpp index 78f6ef0cec..6ad71e8103 100644 --- a/rpcs3/Emu/RSX/RSXThread.cpp +++ b/rpcs3/Emu/RSX/RSXThread.cpp @@ -1680,7 +1680,7 @@ namespace rsx auto &tex = rsx::method_registers.fragment_textures[i]; result.texture_scale[i][0] = sampler_descriptors[i]->scale_x; result.texture_scale[i][1] = sampler_descriptors[i]->scale_y; - result.texture_scale[i][2] = (f32)tex.remap(); //Debug value + result.texture_scale[i][2] = std::bit_cast(tex.remap()); if (tex.enabled() && (current_fp_metadata.referenced_textures_mask & (1 << i))) { diff --git a/rpcs3/Emu/RSX/VK/VKFragmentProgram.cpp b/rpcs3/Emu/RSX/VK/VKFragmentProgram.cpp index 565969d20b..a3413ce61f 100644 --- a/rpcs3/Emu/RSX/VK/VKFragmentProgram.cpp +++ b/rpcs3/Emu/RSX/VK/VKFragmentProgram.cpp @@ -40,6 +40,8 @@ void VKFragmentDecompilerThread::insertHeader(std::stringstream & OS) } OS << "#extension GL_ARB_separate_shader_objects: enable\n\n"; + + glsl::insert_subheader_block(OS); } void VKFragmentDecompilerThread::insertInputs(std::stringstream & OS) @@ -205,7 +207,7 @@ void VKFragmentDecompilerThread::insertConstants(std::stringstream & OS) OS << "layout(std140, set = 0, binding = 4) uniform TextureParametersBuffer\n"; OS << "{\n"; - OS << " vec4 texture_parameters[16];\n"; + OS << " sampler_info texture_parameters[16];\n"; OS << "};\n\n"; vk::glsl::program_input in;