Kode / Kinc

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

Weird drawing behavior with G4onG5 with Vulkan backend #830

Open tcdude opened 9 months ago

tcdude commented 9 months ago

Describe the bug Drawing the same indexed vertices multiple times with some uniforms set only once per pipeline/frame and other uniforms set differently for each draw call, the resulting render looks strange (out of sync, appears to be drawn over the span of multiple frames).

using G4onG5 with Vulkan backend

https://github.com/Kode/Kinc/assets/13047632/ba2fccbc-82ab-4d93-8ddc-9b33d3c3b657

using OpenGL backend

https://github.com/Kode/Kinc/assets/13047632/06046b0a-2ede-499a-bfd4-3a61335a065c

To Reproduce A repro repo can be found here: weird-vulkan-behavior

Expected behavior See the video where the OpenGL backend is being used.

Execution Environment:

Using Kinc (be46b461) from /home/tc/code/weird-vulkan-behavior/Kinc kfile found. Creating Linux project files. Compiling shader 1 of 4 (shader.frag). Compiling shader 2 of 4 (shader.vert). Compiling shader 3 of 4 (g1.frag). Compiling shader 4 of 4 (g1.vert). Compiling... [1/22] gcc -g -I../../Kinc/Sources -I../../Kinc/Backends/System/Linux/Sources -I../../Kinc/Backends/System/POSIX/Sources -I../wayland -I../../Kinc/Backends/Graphics5/Vulkan/Sources -I../../Kinc/Backends/Graphics4/G4onG5/Sources -DKINC_NO_WAYLAND -DKORE_G1 -DKORE_G2 -DKORE_G3 -DKORE_A1 -DKORE_A2 -DKORE_LZ4X -DKORE_VULKAN -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -DKORE_G4 -DKORE_G5 -DKORE_G4ONG5 -MD -MF posixunit.o.d -c ../../Kinc/Backends/System/POSIX/Sources/kinc/backend/posixunit.c -o posixunit.o [2/22] gcc -g -I../../Kinc/Sources -I../../Kinc/Backends/System/Linux/Sources -I../../Kinc/Backends/System/POSIX/Sources -I../wayland -I../../Kinc/Backends/Graphics5/Vulkan/Sources -I../../Kinc/Backends/Graphics4/G4onG5/Sources -DKINC_NO_WAYLAND -DKORE_G1 -DKORE_G2 -DKORE_G3 -DKORE_A1 -DKORE_A2 -DKORE_LZ4X -DKORE_VULKAN -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -DKORE_G4 -DKORE_G5 -DKORE_G4ONG5 -MD -MF shader.o.d -c ../../Sources/shader.c -o shader.o [3/22] g++ -g -I../../Kinc/Sources -I../../Kinc/Backends/System/Linux/Sources -I../../Kinc/Backends/System/POSIX/Sources -I../wayland -I../../Kinc/Backends/Graphics5/Vulkan/Sources -I../../Kinc/Backends/Graphics4/G4onG5/Sources -DKINC_NO_WAYLAND -DKORE_G1 -DKORE_G2 -DKORE_G3 -DKORE_A1 -DKORE_A2 -DKORE_LZ4X -DKORE_VULKAN -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -DKORE_G4 -DKORE_G5 -DKOREG4ONG5 -MD -MF rootunit.o.d -c ../../Kinc/Sources/kinc/rootunit.cpp -o rootunit_.o [4/22] gcc -g -I../../Kinc/Sources -I../../Kinc/Backends/System/Linux/Sources -I../../Kinc/Backends/System/POSIX/Sources -I../wayland -I../../Kinc/Backends/Graphics5/Vulkan/Sources -I../../Kinc/Backends/Graphics4/G4onG5/Sources -DKINC_NO_WAYLAND -DKORE_G1 -DKORE_G2 -DKORE_G3 -DKORE_A1 -DKORE_A2 -DKORE_LZ4X -DKORE_VULKAN -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -DKORE_G4 -DKORE_G5 -DKORE_G4ONG5 -MD -MF compute.o.d -c ../../Kinc/Backends/Graphics5/Vulkan/Sources/kinc/backend/compute.c -o compute.o [5/22] gcc -g -I../../Kinc/Sources -I../../Kinc/Backends/System/Linux/Sources -I../../Kinc/Backends/System/POSIX/Sources -I../wayland -I../../Kinc/Backends/Graphics5/Vulkan/Sources -I../../Kinc/Backends/Graphics4/G4onG5/Sources -DKINC_NO_WAYLAND -DKORE_G1 -DKORE_G2 -DKORE_G3 -DKORE_A1 -DKORE_A2 -DKORE_LZ4X -DKORE_VULKAN -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -DKORE_G4 -DKORE_G5 -DKORE_G4ONG5 -MD -MF audio.o.d -c ../../Kinc/Sources/kinc/audio2/audio.c -o audio.o [6/22] gcc -g -I../../Kinc/Sources -I../../Kinc/Backends/System/Linux/Sources -I../../Kinc/Backends/System/POSIX/Sources -I../wayland -I../../Kinc/Backends/Graphics5/Vulkan/Sources -I../../Kinc/Backends/Graphics4/G4onG5/Sources -DKINC_NO_WAYLAND -DKORE_G1 -DKORE_G2 -DKORE_G3 -DKORE_A1 -DKORE_A2 -DKORE_LZ4X -DKORE_VULKAN -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -DKORE_G4 -DKORE_G5 -DKORE_G4ONG5 -MD -MF g4ong5unit.o.d -c ../../Kinc/Backends/Graphics4/G4onG5/Sources/kinc/backend/graphics4/g4ong5unit.c -o g4ong5unit.o [7/22] g++ -g -I../../Kinc/Sources -I../../Kinc/Backends/System/Linux/Sources -I../../Kinc/Backends/System/POSIX/Sources -I../wayland -I../../Kinc/Backends/Graphics5/Vulkan/Sources -I../../Kinc/Backends/Graphics4/G4onG5/Sources -DKINC_NO_WAYLAND -DKORE_G1 -DKORE_G2 -DKORE_G3 -DKORE_A1 -DKORE_A2 -DKORE_LZ4X -DKORE_VULKAN -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -DKORE_G4 -DKORE_G5 -DKORE_G4ONG5 -MD -MF graphics_kore.o.d -c ../../Kinc/Sources/kinc/graphics2/graphics_kore.cpp -o graphics_kore.o [8/22] gcc -g -I../../Kinc/Sources -I../../Kinc/Backends/System/Linux/Sources -I../../Kinc/Backends/System/POSIX/Sources -I../wayland -I../../Kinc/Backends/Graphics5/Vulkan/Sources -I../../Kinc/Backends/Graphics4/G4onG5/Sources -DKINC_NO_WAYLAND -DKORE_G1 -DKORE_G2 -DKORE_G3 -DKORE_A1 -DKORE_A2 -DKORE_LZ4X -DKORE_VULKAN -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -DKORE_G4 -DKORE_G5 -DKORE_G4ONG5 -MD -MF graphics.o.d -c ../../Kinc/Sources/kinc/graphics1/graphics.c -o graphics.o [9/22] gcc -g -I../../Kinc/Sources -I../../Kinc/Backends/System/Linux/Sources -I../../Kinc/Backends/System/POSIX/Sources -I../wayland -I../../Kinc/Backends/Graphics5/Vulkan/Sources -I../../Kinc/Backends/Graphics4/G4onG5/Sources -DKINC_NO_WAYLAND -DKORE_G1 -DKORE_G2 -DKORE_G3 -DKORE_A1 -DKORE_A2 -DKORE_LZ4X -DKORE_VULKAN -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -DKORE_G4 -DKORE_G5 -DKORE_G4ONG5 -MD -MF a1unit.o.d -c ../../Kinc/Sources/kinc/audio1/a1unit.c -o a1unit.o [10/22] gcc -g -I../../Kinc/Sources -I../../Kinc/Backends/System/Linux/Sources -I../../Kinc/Backends/System/POSIX/Sources -I../wayland -I../../Kinc/Backends/Graphics5/Vulkan/Sources -I../../Kinc/Backends/Graphics4/G4onG5/Sources -DKINC_NO_WAYLAND -DKORE_G1 -DKORE_G2 -DKORE_G3 -DKORE_A1 -DKORE_A2 -DKORE_LZ4X -DKORE_VULKAN -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -DKORE_G4 -DKORE_G5 -DKORE_G4ONG5 -MD -MF graphics_soft.o.d -c ../../Kinc/Sources/kinc/graphics2/graphics_soft.c -o graphics_soft.o [11/22] gcc -g -I../../Kinc/Sources -I../../Kinc/Backends/System/Linux/Sources -I../../Kinc/Backends/System/POSIX/Sources -I../wayland -I../../Kinc/Backends/Graphics5/Vulkan/Sources -I../../Kinc/Backends/Graphics4/G4onG5/Sources -DKINC_NO_WAYLAND -DKORE_G1 -DKORE_G2 -DKORE_G3 -DKORE_A1 -DKORE_A2 -DKORE_LZ4X -DKORE_VULKAN -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -DKORE_G4 -DKORE_G5 -DKORE_G4ONG5 -MD -MF g4unit.o.d -c ../../Kinc/Sources/kinc/graphics4/g4unit.c -o g4unit.o [12/22] gcc -g -I../../Kinc/Sources -I../../Kinc/Backends/System/Linux/Sources -I../../Kinc/Backends/System/POSIX/Sources -I../wayland -I../../Kinc/Backends/Graphics5/Vulkan/Sources -I../../Kinc/Backends/Graphics4/G4onG5/Sources -DKINC_NO_WAYLAND -DKORE_G1 -DKORE_G2 -DKORE_G3 -DKORE_A1 -DKORE_A2 -DKORE_LZ4X -DKORE_VULKAN -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -DKORE_G4 -DKORE_G5 -DKORE_G4ONG5 -MD -MF g5unit.o.d -c ../../Kinc/Sources/kinc/graphics5/g5unit.c -o g5unit.o [13/22] gcc -g -I../../Kinc/Sources -I../../Kinc/Backends/System/Linux/Sources -I../../Kinc/Backends/System/POSIX/Sources -I../wayland -I../../Kinc/Backends/Graphics5/Vulkan/Sources -I../../Kinc/Backends/Graphics4/G4onG5/Sources -DKINC_NO_WAYLAND -DKORE_G1 -DKORE_G2 -DKORE_G3 -DKORE_A1 -DKORE_A2 -DKORE_LZ4X -DKORE_VULKAN -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -DKORE_G4 -DKORE_G5 -DKORE_G4ONG5 -MD -MF linuxunit.o.d -c ../../Kinc/Backends/System/Linux/Sources/kinc/backend/linuxunit.c -o linuxunit.o [14/22] gcc -g -I../../Kinc/Sources -I../../Kinc/Backends/System/Linux/Sources -I../../Kinc/Backends/System/POSIX/Sources -I../wayland -I../../Kinc/Backends/Graphics5/Vulkan/Sources -I../../Kinc/Backends/Graphics4/G4onG5/Sources -DKINC_NO_WAYLAND -DKORE_G1 -DKORE_G2 -DKORE_G3 -DKORE_A1 -DKORE_A2 -DKORE_LZ4X -DKORE_VULKAN -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -DKORE_G4 -DKORE_G5 -DKORE_G4ONG5 -MD -MF inputunit.o.d -c ../../Kinc/Sources/kinc/input/inputunit.c -o inputunit.o [15/22] gcc -g -I../../Kinc/Sources -I../../Kinc/Backends/System/Linux/Sources -I../../Kinc/Backends/System/POSIX/Sources -I../wayland -I../../Kinc/Backends/Graphics5/Vulkan/Sources -I../../Kinc/Backends/Graphics4/G4onG5/Sources -DKINC_NO_WAYLAND -DKORE_G1 -DKORE_G2 -DKORE_G3 -DKORE_A1 -DKORE_A2 -DKORE_LZ4X -DKORE_VULKAN -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -DKORE_G4 -DKORE_G5 -DKORE_G4ONG5 -MD -MF iounit.o.d -c ../../Kinc/Sources/kinc/io/iounit.c -o iounit.o [16/22] gcc -g -I../../Kinc/Sources -I../../Kinc/Backends/System/Linux/Sources -I../../Kinc/Backends/System/POSIX/Sources -I../wayland -I../../Kinc/Backends/Graphics5/Vulkan/Sources -I../../Kinc/Backends/Graphics4/G4onG5/Sources -DKINC_NO_WAYLAND -DKORE_G1 -DKORE_G2 -DKORE_G3 -DKORE_A1 -DKORE_A2 -DKORE_LZ4X -DKORE_VULKAN -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -DKORE_G4 -DKORE_G5 -DKORE_G4ONG5 -MD -MF mathunit.o.d -c ../../Kinc/Sources/kinc/math/mathunit.c -o mathunit.o [17/22] gcc -g -I../../Kinc/Sources -I../../Kinc/Backends/System/Linux/Sources -I../../Kinc/Backends/System/POSIX/Sources -I../wayland -I../../Kinc/Backends/Graphics5/Vulkan/Sources -I../../Kinc/Backends/Graphics4/G4onG5/Sources -DKINC_NO_WAYLAND -DKORE_G1 -DKORE_G2 -DKORE_G3 -DKORE_A1 -DKORE_A2 -DKORE_LZ4X -DKORE_VULKAN -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -DKORE_G4 -DKORE_G5 -DKORE_G4ONG5 -MD -MF rootunit.o.d -c ../../Kinc/Sources/kinc/rootunit.c -o rootunit.o [18/22] gcc -g -I../../Kinc/Sources -I../../Kinc/Backends/System/Linux/Sources -I../../Kinc/Backends/System/POSIX/Sources -I../wayland -I../../Kinc/Backends/Graphics5/Vulkan/Sources -I../../Kinc/Backends/Graphics4/G4onG5/Sources -DKINC_NO_WAYLAND -DKORE_G1 -DKORE_G2 -DKORE_G3 -DKORE_A1 -DKORE_A2 -DKORE_LZ4X -DKORE_VULKAN -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -DKORE_G4 -DKORE_G5 -DKORE_G4ONG5 -MD -MF networkunit.o.d -c ../../Kinc/Sources/kinc/network/networkunit.c -o networkunit.o [19/22] gcc -g -I../../Kinc/Sources -I../../Kinc/Backends/System/Linux/Sources -I../../Kinc/Backends/System/POSIX/Sources -I../wayland -I../../Kinc/Backends/Graphics5/Vulkan/Sources -I../../Kinc/Backends/Graphics4/G4onG5/Sources -DKINC_NO_WAYLAND -DKORE_G1 -DKORE_G2 -DKORE_G3 -DKORE_A1 -DKORE_A2 -DKORE_LZ4X -DKORE_VULKAN -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -DKORE_G4 -DKORE_G5 -DKORE_G4ONG5 -MD -MF waylandunit.o.d -c ../wayland/wayland-generated/waylandunit.c -o waylandunit.o [20/22] gcc -g -I../../Kinc/Sources -I../../Kinc/Backends/System/Linux/Sources -I../../Kinc/Backends/System/POSIX/Sources -I../wayland -I../../Kinc/Backends/Graphics5/Vulkan/Sources -I../../Kinc/Backends/Graphics4/G4onG5/Sources -DKINC_NO_WAYLAND -DKORE_G1 -DKORE_G2 -DKORE_G3 -DKORE_A1 -DKORE_A2 -DKORE_LZ4X -DKORE_VULKAN -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -DKORE_G4 -DKORE_G5 -DKORE_G4ONG5 -MD -MF vulkanunit.o.d -c ../../Kinc/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/vulkanunit.c -o vulkanunit.o [21/22] gcc -g -I../../Kinc/Sources -I../../Kinc/Backends/System/Linux/Sources -I../../Kinc/Backends/System/POSIX/Sources -I../wayland -I../../Kinc/Backends/Graphics5/Vulkan/Sources -I../../Kinc/Backends/Graphics4/G4onG5/Sources -DKINC_NO_WAYLAND -DKORE_G1 -DKORE_G2 -DKORE_G3 -DKORE_A1 -DKORE_A2 -DKORE_LZ4X -DKORE_VULKAN -D_POSIX_C_SOURCE=200112L -D_XOPEN_SOURCE=600 -DKORE_G4 -DKORE_G5 -DKORE_G4ONG5 -MD -MF stb_vorbis.o.d -c ../../Kinc/Sources/kinc/libs/stb_vorbis.c -o stbvorbis.o [22/22] g++ -o ShaderTest -g shader.o g4ong5unit.o compute.o vulkanunit.o linuxunit.o posixunit.o rootunit.o rootunit.o a1unit.o audio.o graphics.o graphics_kore.o graphics_soft.o g4unit.o g5unit.o inputunit.o iounit.o stb_vorbis.o mathunit.o networkunit.o waylandunit.o -static-libgcc -static-libstdc++ -pthread -lasound -ldl -ludev -lvulkan Build time: 0m 2s Done.


- Application output (if it runs): `Running with Vulkan validation layers enabled.`
KosLooney commented 8 months ago

It happens here too (amd + windows).

It looks like you have to put these uniform constants in the for loop to make it work as intended:

kinc_g4_set_matrix4(mvp_loc, &mvp_mat);
kinc_g4_set_matrix4(v_loc, &v_mat);
kinc_g4_set_floats(lightPos_loc, (float*)&light_pos, 3);

The constants are written to a vertexConstantBuffer and fragmentConstantBuffer. https://github.com/Kode/Kinc/blob/6c012b7878c5ff8c8bc9145df06edaff3ed23877/Backends/Graphics4/G4onG5/Sources/kinc/backend/graphics4/G4.c.h#L35-L36

Each call to kinc_g4_draw_indexed_vertices on G4onG5 uses a offset (constantBufferIndex*constantBufferSize) from these buffers and they are then linked with a g5 command. https://github.com/Kode/Kinc/blob/6c012b7878c5ff8c8bc9145df06edaff3ed23877/Backends/Graphics4/G4onG5/Sources/kinc/backend/graphics4/G4.c.h#L229-L237 https://github.com/Kode/Kinc/blob/6c012b7878c5ff8c8bc9145df06edaff3ed23877/Backends/Graphics4/G4onG5/Sources/kinc/backend/graphics4/G4.c.h#L171-L172 https://github.com/Kode/Kinc/blob/6c012b7878c5ff8c8bc9145df06edaff3ed23877/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/commandlist.c.h#L799-L805

The command list is then executed every 100 draw calls, on a vertex_buffer change, kinc_g4_start() or on kinc_g4_end(). https://github.com/Kode/Kinc/blob/6c012b7878c5ff8c8bc9145df06edaff3ed23877/Backends/Graphics4/G4onG5/Sources/kinc/backend/graphics4/G4.c.h#L177-L182

So at the moment your 1st instance uses all constants and the rest are only using off_loc and rot_loc. 1st cube:

1st_call

2nd cube:

2nd_call

Edit: The cubes eventually appear because each frame the mvp and v uniform constants are set on a different offset location 1st cube now has offset 344064:

1st_call_2