ludic migrate state packages <every example program> packages/ludic.lab/example/plate.ludic 1804 vars into 126 states, 64 into lets; 23498 edits in 460 files Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
112 lines
6.9 KiB
Text
112 lines
6.9 KiB
Text
# vk_resources.ludic — render3d's Vulkan resources on their own: textures uploaded the way the
|
|
# renderer uploads them and read back, a mip chain, the samplers the renderer asks for, and a
|
|
# buffer filled and grown. Run it under the validation layer on each machine.
|
|
#
|
|
# gpu_vk_res.ludic speaks OpenGL's names for formats and filters, as gpu.ludic does, and those
|
|
# names only exist where Gl.* is named - hence the one Gl.pixel_scale() below. Nothing here
|
|
# opens an OpenGL context or draws with one.
|
|
# bin/ludicc examples/rendering/vk_resources.ludic --headless -o build/vk_resources
|
|
import "../../packages/ludic.render3d/gpu_vk.ludic"
|
|
import "../../packages/ludic.render3d/gpu_vk_res.ludic"
|
|
|
|
program VkResources {
|
|
property Marker { on: int = 1 }
|
|
model Anchor { Marker }
|
|
|
|
state VkResourcesState {
|
|
failures: int = 0
|
|
}
|
|
unsafe function check(vk_resources_st: mut VkResourcesState, what: string, ok: bool) -> void {
|
|
if ok { print(`vulkan: {what} ok`) } else { print(`vulkan: {what} FAILED`); vk_resources_st.failures += 1 }
|
|
}
|
|
|
|
handler Boot(render3d_st: mut Render3dState, vk_resources_st: mut VkResourcesState) phase Start {
|
|
spawn Anchor {}
|
|
print(`vulkan: OpenGL names spliced (pixel scale {Gl.pixel_scale()})`)
|
|
if not gvk_init(render3d_st) { print(`VKRES NONE: {render3d_st.gvk_why}`); quit() }
|
|
|
|
# RGBA8 up and back, byte for byte
|
|
let w = 64
|
|
let h = 32
|
|
let px = bytes(w * h * 4)
|
|
for i in 0 .. w * h { px[i * 4] = i & 255; px[i * 4 + 1] = (i >> 8) & 255; px[i * 4 + 2] = (i * 7) & 255; px[i * 4 + 3] = 200 }
|
|
let t1 = gvk_tex_new(render3d_st)
|
|
check(vk_resources_st, "RGBA8 storage with mips", gvk_tex_storage(render3d_st, t1, false, GL_RGBA8, w, h, 1, true))
|
|
check(vk_resources_st, "RGBA8 upload", gvk_tex_upload(render3d_st, t1, GL_RGBA8, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, px))
|
|
check(vk_resources_st, "RGBA8 mip chain (7 levels)", render3d_st.gvk_tex_levels[t1] == 7 and gvk_tex_mips(render3d_st, t1, w, h))
|
|
let back = bytes(w * h * 4)
|
|
check(vk_resources_st, "RGBA8 read-back call", gvk_tex_read(render3d_st, t1, GL_RGBA8, w, h, GL_RGBA, GL_UNSIGNED_BYTE, back))
|
|
var same = true
|
|
for i in 0 .. w * h * 4 { if back[i] != px[i] { same = false } }
|
|
check(vk_resources_st, "RGBA8 read-back matches", same)
|
|
|
|
# R32F, the terrain's height field path
|
|
let hf = bytes(w * h * 4)
|
|
for i in 0 .. w * h { Vk.put_i32(hf, i * 4, 0x40000000 + i) }
|
|
let t2 = gvk_tex_new(render3d_st)
|
|
check(vk_resources_st, "R32F target", gvk_tex_storage(render3d_st, t2, false, GL_R32F, w, h, 1, false))
|
|
check(vk_resources_st, "R32F upload", gvk_tex_upload(render3d_st, t2, GL_R32F, w, h, 1, GL_RED, GL_FLOAT, hf))
|
|
let hb = bytes(w * h * 4)
|
|
check(vk_resources_st, "R32F read-back call", gvk_tex_read(render3d_st, t2, GL_R32F, w, h, GL_RED, GL_FLOAT, hb))
|
|
var hsame = true
|
|
for i in 0 .. w * h * 4 { if hb[i] != hf[i] { hsame = false } }
|
|
check(vk_resources_st, "R32F read-back matches", hsame)
|
|
|
|
# RGB8 stored as RGBA, and an HDR image's float RGB into half floats
|
|
let rgb = bytes(w * h * 3)
|
|
for i in 0 .. w * h * 3 { rgb[i] = (i * 3) & 255 }
|
|
let t3 = gvk_tex_new(render3d_st)
|
|
check(vk_resources_st, "SRGB8 expanded to RGBA", gvk_tex_storage(render3d_st, t3, false, GL_SRGB8, w, h, 1, true) and gvk_tex_upload(render3d_st, t3, GL_SRGB8, w, h, 1, GL_RGB, GL_UNSIGNED_BYTE, rgb))
|
|
let fl = bytes(w * h * 12)
|
|
for i in 0 .. w * h * 3 { Vk.put_i32(fl, i * 4, 0x3FC00000) }
|
|
let t4 = gvk_tex_new(render3d_st)
|
|
check(vk_resources_st, "RGB16F from floats (halved)", gvk_tex_storage(render3d_st, t4, false, GL_RGB16F, w, h, 1, true) and gvk_tex_upload(render3d_st, t4, GL_RGB16F, w, h, 1, GL_RGB, GL_FLOAT, fl))
|
|
check(vk_resources_st, "half of 1.5 is 0x3E00", gvk_half(0x3FC00000) == 0x3E00)
|
|
|
|
# a 16-bit RGB PNG (a normal map): big-endian samples, swapped on upload, alpha filled opaque
|
|
let n16 = bytes(w * h * 6)
|
|
for i in 0 .. w * h * 3 { n16[i * 2] = (i >> 8) & 255; n16[i * 2 + 1] = i & 255 }
|
|
let t7 = gvk_tex_new(render3d_st)
|
|
render3d_st.gvk_unpack_swap = true
|
|
check(vk_resources_st, "RGB16 swapped upload", gvk_tex_storage(render3d_st, t7, false, GL_RGB16, w, h, 1, true) and gvk_tex_upload(render3d_st, t7, GL_RGB16, w, h, 1, GL_RGB, GL_UNSIGNED_SHORT, n16))
|
|
render3d_st.gvk_unpack_swap = false
|
|
let b16 = bytes(w * h * 6)
|
|
check(vk_resources_st, "RGB16 read-back call", gvk_tex_read(render3d_st, t7, GL_RGB16, w, h, GL_RGB, GL_UNSIGNED_SHORT, b16))
|
|
var s16 = true
|
|
for i in 0 .. w * h * 3 { if b16[i * 2] != (i & 255) or b16[i * 2 + 1] != ((i >> 8) & 255) { s16 = false } }
|
|
check(vk_resources_st, "RGB16 samples arrive little-endian", s16)
|
|
|
|
# the shadow cascades: a depth array, and a render-sized half-float target with no pixels
|
|
let t5 = gvk_tex_new(render3d_st)
|
|
check(vk_resources_st, "D32 array of 4", gvk_tex_storage(render3d_st, t5, true, GL_DEPTH_COMPONENT32F, 256, 256, 4, false))
|
|
let t6 = gvk_tex_new(render3d_st)
|
|
check(vk_resources_st, "RGBA16F target", gvk_tex_storage(render3d_st, t6, false, GL_RGBA16F, 320, 180, 1, false))
|
|
|
|
# the samplers the renderer asks for, and that asking twice returns the same one
|
|
let s1 = gvk_sampler(render3d_st, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT, 0, 0x41800000)
|
|
let s2 = gvk_sampler(render3d_st, GL_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0, 0)
|
|
let s3 = gvk_sampler(render3d_st, GL_LINEAR, GL_LINEAR, GL_CLAMP_TO_BORDER, GL_CLAMP_TO_BORDER, GL_LEQUAL, 0)
|
|
let s4 = gvk_sampler(render3d_st, GL_NEAREST, GL_NEAREST, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0, 0)
|
|
let again = gvk_sampler(render3d_st, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT, 0, 0x41800000)
|
|
check(vk_resources_st, "four samplers made", s1 != 0 and s2 != 0 and s3 != 0 and s4 != 0 and len(render3d_st.gvk_smp) == 4)
|
|
check(vk_resources_st, "a sampler asked for twice is one sampler", again == s1)
|
|
|
|
# a buffer filled, filled again smaller (kept), then larger (grown)
|
|
let b = gvk_buf_new(render3d_st)
|
|
let data = bytes(4096)
|
|
for i in 0 .. 4096 { data[i] = i & 255 }
|
|
check(vk_resources_st, "buffer upload", gvk_buf_upload(render3d_st, b, 1024, data))
|
|
let first = render3d_st.gvk_buf[b]
|
|
check(vk_resources_st, "a smaller upload keeps the buffer", gvk_buf_upload(render3d_st, b, 512, data) and render3d_st.gvk_buf[b] == first)
|
|
check(vk_resources_st, "a larger upload grows it", gvk_buf_upload(render3d_st, b, 4096, data) and render3d_st.gvk_buf_size[b] >= 4096)
|
|
|
|
for t in 1 .. 8 { gvk_tex_release(render3d_st, t) }
|
|
gvk_buf_release(render3d_st, b)
|
|
for i in 0 .. len(render3d_st.gvk_smp) { Vk.destroy_sampler(render3d_st.gvk_dev, render3d_st.gvk_smp[i], null) }
|
|
print(`vulkan: {render3d_st.gvk_n_allocs} allocations left after freeing`)
|
|
check(vk_resources_st, "every allocation freed", render3d_st.gvk_n_allocs == 0)
|
|
gvk_shutdown(render3d_st)
|
|
if vk_resources_st.failures == 0 { print("VKRES OK") } else { print(`VKRES FAILED ({vk_resources_st.failures})`) }
|
|
quit()
|
|
}
|
|
}
|