Skip to content

Commit

Permalink
nv2a: Offset vertices to match HW rounding
Browse files Browse the repository at this point in the history
Co-authored-by: Erik Abair <[email protected]>
  • Loading branch information
antangelo and abaire committed Jul 27, 2024
1 parent a8d8ef5 commit 5ce8573
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
26 changes: 23 additions & 3 deletions hw/xbox/nv2a/shaders.c
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,9 @@ GLSL_DEFINE(materialEmissionColor, GLSL_LTCTXA(NV_IGRAPH_XF_LTCTXA_CM_COL) ".xyz
}

mstring_append(body,
" oPos = invViewport * (tPosition * compositeMat);\n"
" oPos = tPosition * compositeMat;\n"
" oPos.xy = adjust_pixel_center(oPos.xy, oPos.w);\n"
" oPos = invViewport * oPos;\n"
" oPos.z = oPos.z * 2.0 - oPos.w;\n");

/* FIXME: Testing */
Expand Down Expand Up @@ -857,6 +859,26 @@ GLSL_DEFINE(texMat3, GLSL_C_MAT4(NV_IGRAPH_XF_XFCTX_T3MAT))
}
mstring_append(header, "\n");

mstring_append_fmt(header, "\n"
"vec2 adjust_pixel_center(vec2 screen_pos, float w) {\n"
" if (w == 0.0 || isinf(w)) {\n"
" w = 1.0;\n"
" }\n"

" screen_pos /= w;\n"
" vec2 pixel = floor(screen_pos);\n"
" vec2 subpixel = screen_pos - pixel;\n"
" vec2 round_down = vec2(lessThan(subpixel, vec2(0.5625)));\n"

" subpixel -= vec2(0.0625);\n"

" vec2 bias = vec2(0.002);\n"
" subpixel += mix(bias, -bias, round_down);\n"

" return w * (pixel + subpixel);\n"
"}\n"
);

MString *body = mstring_from_str("void main() {\n");

for (i = 0; i < NV2A_VERTEXSHADER_ATTRIBUTES; i++) {
Expand Down Expand Up @@ -985,12 +1007,10 @@ GLSL_DEFINE(texMat3, GLSL_C_MAT4(NV_IGRAPH_XF_XFCTX_T3MAT))
shade_model_mult,
shade_model_mult);


/* Return combined header + source */
mstring_append(header, mstring_get_str(body));
mstring_unref(body);
return header;

}

static GLuint create_gl_shader(GLenum gl_shader_type,
Expand Down
5 changes: 2 additions & 3 deletions hw/xbox/nv2a/vsh.c
Original file line number Diff line number Diff line change
Expand Up @@ -840,10 +840,9 @@ void vsh_translate(uint16_t version,
mstring_append(body,
/* the shaders leave the result in screen space, while
* opengl expects it in clip space.
* TODO: the pixel-center co-ordinate differences should handled
*/
" oPos.x = 2.0 * (oPos.x - surfaceSize.x * 0.5) / surfaceSize.x;\n"
" oPos.y = -2.0 * (oPos.y - surfaceSize.y * 0.5) / surfaceSize.y;\n"
" oPos.xy = 2.0 * adjust_pixel_center(oPos.xy, 1.0) / surfaceSize - vec2(1.0);\n"
" oPos.y *= -1;\n"
);
if (z_perspective) {
mstring_append(body, " oPos.z = oPos.w;\n");
Expand Down

0 comments on commit 5ce8573

Please sign in to comment.