Files
kryon/tests/practice_material_gpu_test.zi
T
waozi 4b79d97615 Retain smooth indexed scene geometry on GLES
Upload only changed vertex streams during deformation and keep material texture uploads on their assigned sampler units.
2026-11-18 01:33:24 -03:00

260 lines
10 KiB
Ziran

// Real CPU/GLES execution of the indexed skin and portable watercolor material.
#import "kryon/scene"
#import "kryon/scene_skin"
#import "kryon/scene_render"
#import "kryon/scene_gpu"
#import "kryon/watercolor"
#import "kryon/shader"
#import "std/image"
#import "std/vec"
SKIN_GRID :: 128
PixelHash :: (pixels: []u8) -> u64 {
hash: u64 = 1469598103934665603
for byte: pixels { hash = (hash ^ cast(u64)byte) * cast(u64)1099511628211 }
return hash
}
CheckAnimatedGrid :: () -> bool {
scene: Scene
SceneInit(*scene)
skin: SceneSkin
gpu: SceneGpuContext
image := NewImage(128, 128)
positions: Vec(Vector3)
normals: Vec(Vector3)
uvs: Vec(ShapePoint)
weights: Vec(SkinInfluence)
indices: Vec(s32)
defer {
SceneGpuRelease(*gpu)
SceneSkinRelease(*skin)
SceneFree(*scene)
VecFree(image.pixels)
VecFree(positions); VecFree(normals); VecFree(uvs); VecFree(weights); VecFree(indices)
}
stride := SKIN_GRID + 1
vertices := stride * stride
if !VecResize(positions, vertices) || !VecResize(normals, vertices) ||
!VecResize(uvs, vertices) || !VecResize(weights, vertices) ||
!VecResize(indices, SKIN_GRID * SKIN_GRID * 6) { return false }
for y: 0..SKIN_GRID {
for x: 0..SKIN_GRID {
at := y * stride + x
u := cast(float64)x / cast(float64)SKIN_GRID
v := cast(float64)y / cast(float64)SKIN_GRID
positions[at] = .{u - 0.5, v - 0.5, 0.0}
normals[at] = .{0.0, 0.0, 1.0}
uvs[at] = .{u, v}
weights[at] = SkinInfluence.{}
weights[at].joints[1] = 1
weights[at].weights[0] = 1.0 - v
weights[at].weights[1] = v
}
}
for y: 0..SKIN_GRID - 1 {
for x: 0..SKIN_GRID - 1 {
vertex := cast(s32)(y * stride + x)
at := (y * SKIN_GRID + x) * 6
indices[at] = vertex; indices[at + 1] = vertex + 1; indices[at + 2] = vertex + 1 + cast(s32)stride
indices[at + 3] = vertex; indices[at + 4] = vertex + 1 + cast(s32)stride; indices[at + 5] = vertex + cast(s32)stride
}
}
if !SceneSkinCreate(*scene, *skin, 2, positions[:], normals[:], uvs[:], indices[:], weights[:],
UnlitMaterial(.{180, 120, 80, 255})) { return false }
camera := OrthographicCamera(-1.0, 1.0, 1.0, -1.0, 0.1, 10.0)
camera.position = .{0.0, 0.0, 3.0}
submit: float64
first_hash: u64
last_hash: u64
uploaded: s64
for frame: 0..47 {
bones: [2]SkinAffine = .[SkinIdentity(), SkinIdentity()]
bones[1].translation.x = cast(float64)frame / 200.0
if !SceneSkinDeform(*scene, *skin, bones[:]) { return false }
result := SceneGpuRender(*gpu, *scene, camera, *image)
if !result.ok || !result.accelerated { print("Animated grid failed: %\n", result.diagnostic); return false }
if frame == 7 { uploaded = SceneGpuMemoryUsage(*gpu).geometry_vertices }
if frame >= 8 { submit += result.submit_ms }
if frame == 8 || frame == 47 {
hash := PixelHash(image.pixels[:])
if frame == 8 { first_hash = hash } else { last_hash = hash }
}
}
uploaded = (SceneGpuMemoryUsage(*gpu).geometry_vertices - uploaded) / 40
print("Animated indexed grid: submit % ms, vertices per frame %, hashes % %\n",
submit / 40.0, uploaded, first_hash, last_hash)
return first_hash != last_hash && uploaded == vertices
}
CheckIndexedMeshes :: () -> bool {
scene: Scene
SceneInit(*scene)
gpu: SceneGpuContext
cpu := NewImage(64, 64)
image := NewImage(64, 64)
defer {
SceneGpuRelease(*gpu)
SceneFree(*scene)
VecFree(cpu.pixels)
VecFree(image.pixels)
RenderSceneRelease()
}
positions: [4]Vector3 = .[.{-0.3, -0.3, 0.0}, .{0.3, -0.3, 0.0},
.{0.3, 0.3, 0.0}, .{-0.3, 0.3, 0.0}]
normals: [4]Vector3 = .[.{0.0, 0.0, 1.0}, .{0.0, 0.0, 1.0},
.{0.0, 0.0, 1.0}, .{0.0, 0.0, 1.0}]
uvs: [4]ShapePoint
indices: [6]s32 = .[0, 1, 2, 0, 2, 3]
left := SceneIndexedMesh(*scene, positions[:], normals[:], uvs[:], indices[:],
UnlitMaterial(.{255, 0, 0, 255}))
right := SceneIndexedMesh(*scene, positions[:], normals[:], uvs[:], indices[:],
UnlitMaterial(.{0, 255, 0, 255}))
if left < 0 || right < 0 { return false }
unused SetPosition(*scene, left, -0.5, 0.0, 0.0)
unused SetPosition(*scene, right, 0.5, 0.0, 0.0)
camera := OrthographicCamera(-1.0, 1.0, 1.0, -1.0, 0.1, 10.0)
camera.position = .{0.0, 0.0, 3.0}
result := SceneGpuRender(*gpu, *scene, camera, *image)
if !result.ok || !result.accelerated {
print("Two indexed meshes failed on GLES: %\n", result.diagnostic)
return false
}
if !RenderScene(*scene, camera, *cpu) { return false }
left_pixel := (32 * 64 + 16) * 4
right_pixel := (32 * 64 + 48) * 4
if !(cpu.pixels[left_pixel] > 200 && cpu.pixels[left_pixel + 1] < 10 &&
cpu.pixels[right_pixel] < 10 && cpu.pixels[right_pixel + 1] > 200 &&
image.pixels[left_pixel] > 200 && image.pixels[left_pixel + 1] < 10 &&
image.pixels[right_pixel] < 10 && image.pixels[right_pixel + 1] > 200) { return false }
uploaded := SceneGpuMemoryUsage(*gpu).geometry_vertices
unused SetPosition(*scene, right, 0.0, 0.0, 0.0)
if !SceneGpuRender(*gpu, *scene, camera, *image).ok { return false }
return SceneGpuMemoryUsage(*gpu).geometry_vertices - uploaded == positions.count &&
image.pixels[left_pixel] > 200 && image.pixels[right_pixel + 3] == 0 &&
image.pixels[(32 * 64 + 32) * 4 + 1] > 200
}
main :: () -> s32 {
if !CheckAnimatedGrid() { return 8 }
if !CheckIndexedMeshes() {
print("Two indexed meshes must both render on CPU and GLES\n")
return 7
}
scene: Scene
SceneInit(*scene)
skin: SceneSkin
program: ShaderProgram
gpu: SceneGpuContext
cpu := NewImage(96, 96)
image := NewImage(96, 96)
defer {
SceneGpuRelease(*gpu)
SceneSkinRelease(*skin)
SceneFree(*scene)
ShaderFree(*program)
VecFree(cpu.pixels)
VecFree(image.pixels)
RenderSceneRelease()
}
if !WatercolorMaterialCompile(*program).ok {
return 1
}
paper: [16]u8 = .[255, 255, 255, 255, 220, 230, 235, 255,
245, 238, 230, 255, 230, 225, 220, 255]
pigment: [16]u8 = .[100, 140, 180, 255, 180, 125, 110, 255,
140, 190, 110, 255, 160, 120, 200, 255]
samplers: [2]ShaderSampler = .[.{2, 2, paper[:], true}, .{2, 2, pigment[:], true}]
uniforms: [4]ShaderValue = .[.{2.0, 2.0, 0.0, 0.0}, .{-0.4, 0.7, 0.8, 0.0},
.{0.0, 0.0, 1.0, 0.0}, .{0.04, 0.06, 0.08, 0.0}]
material := UnlitMaterial(.{180, 120, 80, 255})
material.shader = *program
material.shader_uniforms = uniforms[:]
material.shader_samplers = samplers[:]
positions: [4]Vector3 = .[.{-0.5, -0.5, 0.0}, .{0.5, -0.5, 0.0},
.{0.5, 0.5, 0.0}, .{-0.5, 0.5, 0.0}]
normals: [4]Vector3 = .[.{0.0, 0.0, 1.0}, .{0.0, 0.0, 1.0},
.{0.0, 0.0, 1.0}, .{0.0, 0.0, 1.0}]
uvs: [4]ShapePoint = .[.{0.0, 0.0}, .{1.0, 0.0}, .{1.0, 1.0}, .{0.0, 1.0}]
indices: [6]s32 = .[0, 1, 2, 0, 2, 3]
influences: [4]SkinInfluence
for index: cast(s32)0..3 {
influences[index].joints[0] = index / 2
influences[index].weights[0] = 1.0
}
if !SceneSkinCreate(*scene, *skin, 2, positions[:], normals[:], uvs[:], indices[:],
influences[:], material) {
return 2
}
camera := OrthographicCamera(-1.0, 1.0, 1.0, -1.0, 0.1, 10.0)
camera.position = .{0.0, 0.0, 3.0}
previous: u64
for state: cast(s32)0..2 {
transforms: [2]SkinAffine = .[SkinIdentity(), SkinIdentity()]
transforms[1].translation.x = cast(float64)state * 0.16
if state == 2 {
material.opacity = 0.58
material.transparent = true
SetMaterial(*scene, skin.node, material)
uniforms[3] = .{0.20, 0.50, 0.72, 0.0}
}
if !SceneSkinDeform(*scene, *skin, transforms[:]) || !RenderScene(*scene, camera, *cpu) {
return 3
}
result := SceneGpuRender(*gpu, *scene, camera, *image)
if !result.ok || !result.accelerated {
print("Practice material GPU failure: %\n", result.diagnostic)
return 4
}
error: s64
large: s64
painted: s64
hash: u64 = 1469598103934665603
for at: 0..image.pixels.count - 1 {
difference := cast(s32)image.pixels[at] - cast(s32)cpu.pixels[at]
if difference < 0 {
difference = -difference
}
error += difference
if difference > 16 {
large += 1
}
hash = (hash ^ cast(u64)image.pixels[at]) * cast(u64)1099511628211
}
for pixel: cast(s32)0..96 * 96 - 1 {
if image.pixels[pixel * 4 + 3] > 100 {
painted += 1
}
}
if error > image.pixels.count || large > image.pixels.count / 200 ||
painted < 1500 || (state > 0 && hash == previous) {
print("Practice GPU parity failure: state=% error=% large=% painted=%\n",
state, error, large, painted)
return 5
}
previous = hash
if state == 0 {
// A first upload must leave both paper and pigment bound to their
// own units, just as the next draw using cached textures does.
warm := SceneGpuRender(*gpu, *scene, camera, *image)
if !warm.ok || !warm.accelerated || PixelHash(image.pixels[:]) != hash {
print("First GLES frame with two samplers differs from the cached frame\n")
return 9
}
}
print("Practice skin/watercolor CPU-GLES state %: mean error %, painted %\n",
state, cast(float64)error / cast(float64)image.pixels.count, painted)
}
SceneGpuRelease(*gpu)
usage := SceneGpuMemoryUsage(*gpu)
if usage.cpu_bytes != 0 || usage.gpu_bytes != 0 || usage.textures != 0 || usage.programs != 0 {
return 6
}
print("Practice skin/material deformation, translucency and GPU cleanup passed\n")
return 0
}