ps2dev / gsKit

gsKit is a C interface to the PS2 Graphics Synthesizer
Other
103 stars 27 forks source link

`offSetX` and `offSetY` how is this working? #75

Open fjtrujy opened 8 months ago

fjtrujy commented 8 months ago

I have some questions about how these values are working.

Why those values are set by default as:

gsGlobal->OffsetX = (int)(2048.0f * 16.0f);
gsGlobal->OffsetY = (int)(2048.0f * 16.0f);

Then later on we are doing:

*p_data++ = GS_SETREG_XYOFFSET_1( gsGlobal->OffsetX, gsGlobal->OffsetY);
*p_data++ = GS_XYOFFSET_1;

And finally, everytime we perform any primitive we are doing things like:

static inline int gsKit_float_to_int_x(const GSGLOBAL *gsGlobal, float fx)
{
    return __gsKit_float_to_int_xy(fx, gsGlobal->OffsetX);
}

static inline int gsKit_float_to_int_y(const GSGLOBAL *gsGlobal, float fy)
{
    return __gsKit_float_to_int_xy(fy, gsGlobal->OffsetY);
}

static inline gs_xyz2 vertex_to_XYZ2(const GSGLOBAL *gsGlobal, float fx, float fy, int iz)
{
    gs_xyz2 res;

    res.xyz.x = gsKit_float_to_int_x(gsGlobal, fx);
    res.xyz.y = gsKit_float_to_int_y(gsGlobal, fy);
    res.xyz.z = iz;
    res.tag = GS_XYZ2;

    return res;
}

Shouldn't be these values set originally as 0,0 and then never use those values when calculating the final vertex position?

rickgaiser commented 8 months ago

These offsets are very important when drawing to the GS, especially when drawing outside of the draw area.

The GS coordinate system uses 12.4 bits fixed point resolution. So we can draw from 0 to 4095 (12 bit), with subpixel accuracy of 1/16th pixel (4bit).

The render buffer can be placed into this 4k x 4k area using the offset registers. Good practice is to put the renderbuffer in the center of this area (as OPL does).

What you need is an offset into the renderbuffer. Such offset does not exist in the gs hardware, and also not in gsKit as far as I'm aware. You'll have to manage the offset in your application (SDL in this specific case). You probably also want to set the scissor to make sure you don't draw outside of your viewport.