Kode / Kinc

Modern low level game library and hardware abstraction.
http://kinc.tech
zlib License
513 stars 122 forks source link

Assume render targets are inverted for scissor #878

Closed tcdude closed 1 month ago

tcdude commented 1 month ago

For a correct scissor result when using render targets, y must be inverted

RobDangerous commented 1 month ago

Wait a minute, I think the if-else has to be switched upside down here, not eliminated.

tcdude commented 1 month ago

I only tested on one Linux machine with OpenGL, where it appears that both framebuffer and render targets are inverted. If I flip the if/else, the scissor rect is applied incorrectly when drawing to the framebuffer instead.

I'm no expert but the internet told me that OpenGL places the origin bottom left, as opposed to pretty much everything else.. So I would assume that "backbuffer" and render targets both are treated the same.

RobDangerous commented 1 month ago

Tested it now and without your change and without using a render-target I get the same results with OpenGL and D3D12. Is this some strange Linux situation maybe?

I changed update in Shader-Kinc to this:

static void update(void *data) {
    kinc_g4_begin(0);
    kinc_g4_clear(KINC_G4_CLEAR_COLOR, 0, 0.0f, 0);

    kinc_g4_scissor(10, 100, 100, 768);

    kinc_g4_set_pipeline(&pipeline);
    kinc_g4_set_vertex_buffer(&vertices);
    kinc_g4_set_index_buffer(&indices);
    kinc_g4_draw_indexed_vertices();

    kinc_g4_end(0);
    kinc_g4_swap_buffers();
}

and I get this:

grafik

tcdude commented 1 month ago

It does work correctly for rendering to the framebuffer and doing the same as you specified works the same on Linux. My PR only removes the if/else, because the render target appears to behave the same as the framebuffer with regards to origin (which in OpenGL is bottom left according to the docs).

If I modify the Tests/Shader/Sources/shader.c update to do this:

        kinc_g4_begin(0);

        kinc_g4_render_target_t *render_targets = {&render_target};
        kinc_g4_set_render_targets(&render_targets, 1);

        kinc_g4_clear(KINC_G4_CLEAR_COLOR, 0xFF000000, 0.0f, 0);

        kinc_g4_scissor(10, 100, 100, 2048);

        kinc_g4_set_pipeline(&pipeline);
        kinc_g4_set_vertex_buffer(&vertices);
        kinc_g4_set_index_buffer(&indices);
        kinc_g4_draw_indexed_vertices();

        kinc_g4_disable_scissor();

        kinc_g4_end(0);
        kinc_g4_swap_buffers();
        ...

then I get this result in the written out test.png: image

RobDangerous commented 1 month ago

You are correct.