Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nv2a: Offset vertices to match HW rounding #1362

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming you're able to fix the mismatch between fixed & programmable: I think it's worth bringing over some of the comments explaining why this is necessary, particularly the magic numbers involved. I don't think any of this would make sense to a future maintainer without understanding the context of the nv2a-specific rounding behavior, which differs from the generally well known/documented 0.5 delta between old DirectX and OpenGL.

I also don't know if it's actually possible to address higher render scales with a simple offsetting approach. 2x and higher will turn garbage that would've been truncated to 0 into a full pixel or more. I had started to look at using a viewport to handle the 9/16th rounding instead but I think I stopped on realizing that glViewportIndexedf requires OGL 4.1.

"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