[GPU] cleanup and optimize the TEV shader functions

This commit is contained in:
fleroviux 2023-06-17 01:27:26 +02:00
parent 76d6140797
commit c3acf2b393

View file

@ -54,12 +54,13 @@ const char* fragmentShader = R"(
uniform sampler2D u_tex0; uniform sampler2D u_tex0;
// TODO: figure out what the correct "initial" value used by TEV0 is. // TODO: figure out the color that is returned to the TEVs when reading from a disabled texture slot.
vec4 previous = vec4(1.0); vec4 tev_texture_sources[4] = vec4[](vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0));
// TODO: presumably this needs to be initialized to the value of GPUREG_TEXENV_BUFFER_COLOR // TODO: figure out the correct "initial" value returned to TEV0 when reading the "previous" source.
vec4 buffer; vec4 tev_previous = vec4(0.0);
vec4 next_buffer;
vec4 tev_previous_buffer[2];
bool tev_unimplemented_source = false; bool tev_unimplemented_source = false;
@ -69,23 +70,21 @@ const char* fragmentShader = R"(
uint rgb_source = (u_textureEnvSource[tev_id] >> (src_id * 4)) & 15u; uint rgb_source = (u_textureEnvSource[tev_id] >> (src_id * 4)) & 15u;
uint alpha_source = (u_textureEnvSource[tev_id] >> (16 + src_id * 4)) & 15u; uint alpha_source = (u_textureEnvSource[tev_id] >> (16 + src_id * 4)) & 15u;
// TODO: get rid of redundant texture fetches during TEV evaluation.
switch (rgb_source) { switch (rgb_source) {
case 0u: source.rgb = colour.rgb; break; // Primary color, TODO: confirm that this is correct case 0u: source.rgb = colour.rgb; break; // Primary color, TODO: confirm that this is correct
case 3u: source.rgb = texture(u_tex0, tex0_UVs).rgb; break; // Texture 0 case 3u: source.rgb = tev_texture_sources[0].rgb; break; // Texture 0
case 13u: source.rgb = buffer.rgb; break; // Previous buffer case 13u: source.rgb = tev_previous_buffer[0].rgb; break; // Previous buffer
case 14u: source.rgb = u_textureEnvColor[tev_id].rgb; break; // Constant (GPUREG_TEXENVi_COLOR) case 14u: source.rgb = u_textureEnvColor[tev_id].rgb; break; // Constant (GPUREG_TEXENVi_COLOR)
case 15u: source.rgb = previous.rgb; break; // Previous (output from TEV #n-1) case 15u: source.rgb = tev_previous.rgb; break; // Previous (output from TEV #n-1)
default: tev_unimplemented_source = true; break; // TODO: implement remaining sources default: tev_unimplemented_source = true; break; // TODO: implement remaining sources
} }
switch (alpha_source) { switch (alpha_source) {
case 0u: source.a = colour.a; break; // Primary color, TODO: confirm that this is correct case 0u: source.a = colour.a; break; // Primary color, TODO: confirm that this is correct
case 3u: source.a = texture(u_tex0, tex0_UVs).a; break; // Texture 0 case 3u: source.a = tev_texture_sources[0].a; break; // Texture 0
case 13u: source.a = buffer.a; break; // Previous buffer case 13u: source.a = tev_previous_buffer[0].a; break; // Previous buffer
case 14u: source.a = u_textureEnvColor[tev_id].a; break; // Constant (GPUREG_TEXENVi_COLOR) case 14u: source.a = u_textureEnvColor[tev_id].a; break; // Constant (GPUREG_TEXENVi_COLOR)
case 15u: source.a = previous.a; break; // Previous (output from TEV #n-1) case 15u: source.a = tev_previous.a; break; // Previous (output from TEV #n-1)
default: tev_unimplemented_source = true; break; // TODO: implement remaining sources default: tev_unimplemented_source = true; break; // TODO: implement remaining sources
} }
@ -166,42 +165,40 @@ const char* fragmentShader = R"(
} }
void main() { void main() {
if ((u_textureConfig & 1u) != 0) { // Render texture 0 if enabled if ((u_textureConfig & 1u) != 0u) tev_texture_sources[0] = texture(u_tex0, tex0_UVs);
// TODO: fix all the redundancy
buffer = vec4(0.0); // TODO: make TEV logic less redundant
next_buffer = u_textureEnvBufferColor;
previous = tev_combine(0); tev_previous_buffer[0] = vec4(0.0);
buffer = next_buffer; tev_previous_buffer[1] = u_textureEnvBufferColor;
if ((u_textureEnvUpdateBuffer & 0x100u) != 0u) next_buffer.rgb = previous.rgb;
if ((u_textureEnvUpdateBuffer & 0x1000u) != 0u) next_buffer.a = previous.a;
previous = tev_combine(1); tev_previous = tev_combine(0);
buffer = next_buffer; tev_previous_buffer[0] = tev_previous_buffer[1];
if ((u_textureEnvUpdateBuffer & 0x200u) != 0u) next_buffer.rgb = previous.rgb; if ((u_textureEnvUpdateBuffer & 0x100u) != 0u) tev_previous_buffer[1].rgb = tev_previous.rgb;
if ((u_textureEnvUpdateBuffer & 0x2000u) != 0u) next_buffer.a = previous.a; if ((u_textureEnvUpdateBuffer & 0x1000u) != 0u) tev_previous_buffer[1].a = tev_previous.a;
previous = tev_combine(2); tev_previous = tev_combine(1);
buffer = next_buffer; tev_previous_buffer[0] = tev_previous_buffer[1];
if ((u_textureEnvUpdateBuffer & 0x400u) != 0u) next_buffer.rgb = previous.rgb; if ((u_textureEnvUpdateBuffer & 0x200u) != 0u) tev_previous_buffer[1].rgb = tev_previous.rgb;
if ((u_textureEnvUpdateBuffer & 0x4000u) != 0u) next_buffer.a = previous.a; if ((u_textureEnvUpdateBuffer & 0x2000u) != 0u) tev_previous_buffer[1].a = tev_previous.a;
previous = tev_combine(3); tev_previous = tev_combine(2);
buffer = next_buffer; tev_previous_buffer[0] = tev_previous_buffer[1];
if ((u_textureEnvUpdateBuffer & 0x800u) != 0u) next_buffer.rgb = previous.rgb; if ((u_textureEnvUpdateBuffer & 0x400u) != 0u) tev_previous_buffer[1].rgb = tev_previous.rgb;
if ((u_textureEnvUpdateBuffer & 0x8000u) != 0u) next_buffer.a = previous.a; if ((u_textureEnvUpdateBuffer & 0x4000u) != 0u) tev_previous_buffer[1].a = tev_previous.a;
previous = tev_combine(4); tev_previous = tev_combine(3);
buffer = next_buffer; tev_previous_buffer[0] = tev_previous_buffer[1];
if ((u_textureEnvUpdateBuffer & 0x800u) != 0u) tev_previous_buffer[1].rgb = tev_previous.rgb;
if ((u_textureEnvUpdateBuffer & 0x8000u) != 0u) tev_previous_buffer[1].a = tev_previous.a;
fragColour = tev_combine(5); tev_previous = tev_combine(4);
tev_previous_buffer[0] = tev_previous_buffer[1];
if (tev_unimplemented_source) { fragColour = tev_combine(5);
fragColour = vec4(1.0, 0.0, 1.0, 1.0);
} if (tev_unimplemented_source) {
} else { // fragColour = vec4(1.0, 0.0, 1.0, 1.0);
fragColour = colour;
} }
// Get original depth value by converting from [near, far] = [0, 1] to [-1, 1] // Get original depth value by converting from [near, far] = [0, 1] to [-1, 1]