Skip to content

Commit

Permalink
nv2a/psh: Normalize coords at sample time
Browse files Browse the repository at this point in the history
  • Loading branch information
mborgerson committed Jul 30, 2024
1 parent 08f3f98 commit 44a0c52
Showing 1 changed file with 43 additions and 24 deletions.
67 changes: 43 additions & 24 deletions hw/xbox/nv2a/pgraph/glsl/psh.c
Original file line number Diff line number Diff line change
Expand Up @@ -645,13 +645,17 @@ static void psh_append_shadowmap(const struct PixelShader *ps, int i, bool compa
return;
}

g_autofree gchar *normalize_tex_coords = g_strdup_printf("norm%d", i);
const char *tex_remap = ps->state.rect_tex[i] ? normalize_tex_coords : "";

const char *comparison = shadow_comparison_map[ps->state.shadow_depth_func];

bool extract_msb_24b = ps->state.tex_x8y24[i] && ps->state.vulkan;

mstring_append_fmt(vars,
"%svec4 t%d_depth%s = textureProj(texSamp%d, pT%d.xyw);\n",
extract_msb_24b ? "u" : "", i, extract_msb_24b ? "_raw" : "", i, i);
mstring_append_fmt(
vars, "%svec4 t%d_depth%s = textureProj(texSamp%d, %s(pT%d.xyw));\n",
extract_msb_24b ? "u" : "", i, extract_msb_24b ? "_raw" : "", i,
tex_remap, i);

if (extract_msb_24b) {
mstring_append_fmt(vars,
Expand Down Expand Up @@ -707,12 +711,17 @@ static void apply_border_adjustment(const struct PixelShader *ps, MString *vars,
static void apply_convolution_filter(const struct PixelShader *ps, MString *vars, int tex)
{
// FIXME: Quincunx

g_autofree gchar *normalize_tex_coords = g_strdup_printf("norm%d", tex);
const char *tex_remap = ps->state.rect_tex[tex] ? normalize_tex_coords : "";

mstring_append_fmt(vars,
"vec4 t%d = vec4(0.0);\n"
"for (int i = 0; i < 9; i++) {\n"
" vec3 texCoord = pT%d.xyw + vec3(convolution3x3[i] / (textureSize(texSamp%d, 0) * texScale%d), 0);\n"
" t%d += textureProj(texSamp%d, texCoord) * gaussian3x3[i];\n"
"}\n", tex, tex, tex, tex, tex, tex, tex);
" vec3 texCoordDelta = vec3(convolution3x3[i], 0);\n"
" texCoordDelta.xy /= textureSize(texSamp%d, 0);\n"
" t%d += textureProj(texSamp%d, %s(pT%d.xyw) + texCoordDelta) * gaussian3x3[i];\n"
"}\n", tex, tex, tex, tex, tex_remap, tex);
}

static MString* psh_convert(struct PixelShader *ps)
Expand Down Expand Up @@ -884,16 +893,13 @@ static MString* psh_convert(struct PixelShader *ps)

ps->code = mstring_new();

for (i = 0; i < 4; i++) {
if (ps->state.rect_tex[i]) {
mstring_append_fmt(vars, "pT%d.xy /= textureSize(texSamp%d, 0) / texScale%d;\n", i, i, i);
}
}

for (i = 0; i < 4; i++) {

const char *sampler_type = get_sampler_type(ps->tex_modes[i], &ps->state, i);

g_autofree gchar *normalize_tex_coords = g_strdup_printf("norm%d", i);
const char *tex_remap = ps->state.rect_tex[i] ? normalize_tex_coords : "";

assert(ps->dot_map[i] < 8);
const char *dotmap_func = dotmap_funcs[ps->dot_map[i]];
if (ps->dot_map[i] > 3) {
Expand All @@ -914,8 +920,8 @@ static MString* psh_convert(struct PixelShader *ps)
(ps->state.conv_tex[i] == CONVOLUTION_FILTER_QUINCUNX))) {
apply_convolution_filter(ps, vars, i);
} else {
mstring_append_fmt(vars, "vec4 t%d = textureProj(texSamp%d, pT%d.xyw);\n",
i, i, i);
mstring_append_fmt(vars, "vec4 t%d = textureProj(texSamp%d, %s(pT%d.xyw));\n",
i, i, tex_remap, i);
}
}
break;
Expand Down Expand Up @@ -964,8 +970,8 @@ static MString* psh_convert(struct PixelShader *ps)

mstring_append_fmt(vars, "dsdt%d = bumpMat%d * dsdt%d;\n",
i, i, i, i);
mstring_append_fmt(vars, "vec4 t%d = texture(texSamp%d, (pT%d.xy + dsdt%d));\n",
i, i, i, i);
mstring_append_fmt(vars, "vec4 t%d = texture(texSamp%d, %s(pT%d.xy + dsdt%d));\n",
i, i, tex_remap, i, i);
break;
case PS_TEXTUREMODES_BUMPENVMAP_LUM:
assert(i >= 1);
Expand All @@ -982,8 +988,8 @@ static MString* psh_convert(struct PixelShader *ps)

mstring_append_fmt(vars, "dsdtl%d.st = bumpMat%d * dsdtl%d.st;\n",
i, i, i, i);
mstring_append_fmt(vars, "vec4 t%d = texture(texSamp%d, (pT%d.xy + dsdtl%d.st));\n",
i, i, i, i);
mstring_append_fmt(vars, "vec4 t%d = texture(texSamp%d, %s(pT%d.xy + dsdtl%d.st));\n",
i, i, tex_remap, i, i);
mstring_append_fmt(vars, "t%d = t%d * (bumpScale%d * dsdtl%d.p + bumpOffset%d);\n",
i, i, i, i, i);
break;
Expand All @@ -1002,8 +1008,8 @@ static MString* psh_convert(struct PixelShader *ps)
i, i, dotmap_func, ps->input_tex[i], i, i-1, i);

apply_border_adjustment(ps, vars, i, "dotST%d");
mstring_append_fmt(vars, "vec4 t%d = texture(texSamp%d, dotST%d);\n",
i, i, i);
mstring_append_fmt(vars, "vec4 t%d = texture(texSamp%d, %s(dotST%d));\n",
i, i, tex_remap, i);
break;
case PS_TEXTUREMODES_DOT_ZW:
assert(i >= 2);
Expand Down Expand Up @@ -1071,16 +1077,16 @@ static MString* psh_convert(struct PixelShader *ps)
assert(!ps->state.rect_tex[i]);
mstring_append_fmt(vars, "vec2 t%dAR = t%d.ar;\n", i, ps->input_tex[i]);
apply_border_adjustment(ps, vars, i, "t%dAR");
mstring_append_fmt(vars, "vec4 t%d = texture(texSamp%d, t%dAR);\n",
i, i, i);
mstring_append_fmt(vars, "vec4 t%d = texture(texSamp%d, %s(t%dAR));\n",
i, i, tex_remap, i);
break;
case PS_TEXTUREMODES_DPNDNT_GB:
assert(i >= 1);
assert(!ps->state.rect_tex[i]);
mstring_append_fmt(vars, "vec2 t%dGB = t%d.gb;\n", i, ps->input_tex[i]);
apply_border_adjustment(ps, vars, i, "t%dGB");
mstring_append_fmt(vars, "vec4 t%d = texture(texSamp%d, t%dGB);\n",
i, i, i);
mstring_append_fmt(vars, "vec4 t%d = texture(texSamp%d, %s(t%dGB));\n",
i, i, tex_remap, i);
break;
case PS_TEXTUREMODES_DOTPRODUCT:
assert(i == 1 || i == 2);
Expand Down Expand Up @@ -1112,6 +1118,19 @@ static MString* psh_convert(struct PixelShader *ps)
mstring_append_fmt(vars, "if (t%d.a == 0.0) { discard; };\n",
i);
}

if (ps->state.rect_tex[i]) {
mstring_append_fmt(preflight,
"vec2 norm%d(vec2 coord) {\n"
" return coord / (textureSize(texSamp%d, 0) / texScale%d);\n"
"}\n",
i, i, i);
mstring_append_fmt(preflight,
"vec3 norm%d(vec3 coord) {\n"
" return vec3(norm%d(coord.xy), coord.z);\n"
"}\n",
i, i);
}
}
}

Expand Down

0 comments on commit 44a0c52

Please sign in to comment.