Keep a bounded set of recent material programs and match their owned shader instructions across separately prepared scenes. Keep borrowed lookup pointers inside the frame and evict unused programs by last use.
304 lines
15 KiB
Ziran
304 lines
15 KiB
Ziran
// Actual EGL/GLES execution, never a visible window. Runner scrubs desktop env.
|
|
#import, file "../src/backend/scene_gpu.zi";
|
|
G :: #import, file "../src/backend/scene_gpu_gl.zi";
|
|
#import "kryon/scene"
|
|
#import "kryon/scene_render"
|
|
#import "kryon/shader"
|
|
#import "std/image"
|
|
#import "std/vec"
|
|
#import "std/text_tools"
|
|
libc :: #system_library "c";
|
|
AsyncPause :: (microseconds: u32)->s32 #foreign libc "usleep";
|
|
AsyncFlush :: (stream: *void)->s32 #foreign libc "fflush";
|
|
AsyncHash :: (image: *Image)->u64 {
|
|
h: u64=1469598103934665603
|
|
for at:0..image.pixels.count-1 {h=(h^cast(u64)image.pixels[at])*cast(u64)1099511628211}
|
|
return h
|
|
}
|
|
AsyncImage :: (pixels: []u8,width: s32,height: s32)->Image {
|
|
image:=NewImage(width,height)
|
|
for at:0..image.pixels.count-1 {image.pixels[at]=pixels[at]}
|
|
return image
|
|
}
|
|
AsyncWait :: (gpu: *SceneGpuContext,image: *Image)->SceneGpuAsyncResult {
|
|
for at:0..4999 {
|
|
result:=SceneGpuPoll(gpu,image)
|
|
if !result.ok||result.completed {return result}
|
|
unused AsyncPause(1000)
|
|
}
|
|
return .{diagnostic="test completion timeout"}
|
|
}
|
|
AsyncTests :: ()->bool {
|
|
gpu: SceneGpuContext;reference: SceneGpuContext
|
|
defer SceneGpuRelease(*gpu);defer SceneGpuRelease(*reference)
|
|
caps:=SceneGpuProbe(*gpu)
|
|
if !caps.available||!caps.async_available {print("Async capability failed % %\n",caps.diagnostic,caps.async_diagnostic);return false}
|
|
print("Async GPU % samples %\n",TextView(caps.renderer[:]),caps.samples)
|
|
SceneGpuSetProfiling(*gpu,true)
|
|
scene: Scene;SceneInit(*scene);defer SceneFree(*scene)
|
|
program: ShaderProgram
|
|
code: [3]ShaderInstruction=.[.{op=.ShaderUniform,dest=4,slot=0},.{op=.ShaderMultiply,dest=5,a=4,b=0},.{op=.ShaderOutput,a=5}]
|
|
if !ShaderCompile(*program,code[:]).ok {return false}
|
|
uniforms: [1]ShaderValue=.[.{0.7,0.4,0.1,1}]
|
|
material:=UnlitMaterial(.{255,255,255,255});material.shader=*program;material.shader_uniforms=uniforms[:]
|
|
texels: [16]u8=.[255,100,40,255,80,220,100,255,50,100,230,255,190,140,70,255]
|
|
texture:=AsyncImage(texels[:],2,2);defer VecFree(texture.pixels)
|
|
material=ImageMaterial(*scene,*texture,material)
|
|
a:=RoundedBox(*scene,0.9,0.9,0.4,0.05,material);SetPosition(*scene,a,-0.3,0,0)
|
|
material.transparent=true;material.color.a=110
|
|
unused RoundedBox(*scene,0.8,0.8,0.5,0.05,material)
|
|
camera:=PerspectiveCamera(45,1.6,0.1,20);camera.position=.{0,0,3}
|
|
image:=NewImage(160,100);sync:=NewImage(160,100);expected:=NewImage(160,100)
|
|
defer VecFree(image.pixels);defer VecFree(sync.pixels);defer VecFree(expected.pixels)
|
|
unready: s32
|
|
for batch:0..15 {
|
|
// Run the PowerVR framebuffer path through real EGL on every test host.
|
|
gpu.direct_readback=batch%2==0
|
|
translucent_second:=batch%4!=2
|
|
hashes: [2]u64;cameras: [2]SceneCamera
|
|
for slot:0..1 {
|
|
scene.background=.{20,42,73,ifx slot==0||!translucent_second then cast(u8)255 else cast(u8)0}
|
|
id:=cast(u64)(batch*2+slot+1)
|
|
camera.position.x=cast(float64)(batch*2+slot)*0.006
|
|
camera.shift_x=cast(float64)(batch%3-1)*0.45
|
|
camera.shift_y=ifx slot==0 then -0.3 else 0.3
|
|
cameras[slot]=camera
|
|
uniforms[0].x=0.1+cast(float64)(batch*2+slot)*0.02
|
|
program.code[2].a=ifx slot==0 then 5 else 4
|
|
SetPosition(*scene,a,-0.3+cast(float64)slot*0.1,0,0)
|
|
scene.meshes[0].material.double_sided=slot==0
|
|
if !SceneGpuRender(*reference,*scene,camera,*expected).ok {return false}
|
|
hashes[slot]=AsyncHash(*expected)
|
|
submitted:=SceneGpuSubmit(*gpu,*scene,camera,160,100,id)
|
|
if !submitted.ok||!submitted.queued||submitted.request_id!=id {print("Submit failure %\n",submitted.diagnostic);return false}
|
|
}
|
|
before:=AsyncHash(*image)
|
|
busy:=SceneGpuSubmit(*gpu,*scene,camera,160,100,1000)
|
|
if !busy.ok||!busy.busy||busy.queued||AsyncHash(*image)!=before||SceneGpuMemoryUsage(*gpu).pending_frames!=2 {return false}
|
|
// Mutate the scene and render synchronously while both transfers are queued.
|
|
scene.background.a=0
|
|
uniforms[0].x=0.95;SetPosition(*scene,a,0.4,0,0)
|
|
scene.positions[0].x+=0.0001
|
|
if !SceneGpuRender(*gpu,*scene,camera,*sync).ok {return false}
|
|
// Poll never changes metadata or pixels for unready work.
|
|
first:=SceneGpuPoll(*gpu,*image)
|
|
if !first.ok {return false}
|
|
if !first.completed {
|
|
unready+=1
|
|
if AsyncHash(*image)!=before {return false}
|
|
first=AsyncWait(*gpu,*image)
|
|
}
|
|
if !first.completed||first.request_id!=cast(u64)(batch*2+1)||first.camera.position.x!=cameras[0].position.x||first.camera.shift_x!=cameras[0].shift_x||first.camera.shift_y!=cameras[0].shift_y||AsyncHash(*image)!=hashes[0] {print("First completion mismatch %\n",batch);return false}
|
|
second:=AsyncWait(*gpu,*image)
|
|
if !second.completed||second.request_id!=cast(u64)(batch*2+2)||second.camera.position.x!=cameras[1].position.x||second.camera.shift_x!=cameras[1].shift_x||second.camera.shift_y!=cameras[1].shift_y||AsyncHash(*image)!=hashes[1] {print("Second completion mismatch %\n",batch);return false}
|
|
stable:=AsyncHash(*image)
|
|
empty:=SceneGpuPoll(*gpu,*image)
|
|
if !empty.ok||empty.completed||AsyncHash(*image)!=stable {return false}
|
|
r:=SceneGpuMemoryUsage(*gpu)
|
|
if r.pending_frames!=0||r.fences!=0 {return false}
|
|
if gpu.direct_readback {
|
|
first_target:=gpu.transfers[0].framebuffer;second_target:=gpu.transfers[1].framebuffer
|
|
if first_target==0||second_target==0||first_target==second_target||first_target==gpu.resolve||second_target==gpu.resolve||
|
|
gpu.transfers[0].straight||gpu.transfers[1].straight!=translucent_second||r.buffers!=1||r.framebuffers!=5 {return false}
|
|
if !first.frame.profile.direct_resolve||second.frame.profile.direct_resolve==translucent_second {return false}
|
|
if first.frame.profile.gpu_completion_ms<=0.0||first.frame.profile.pixel_transfer_ms<=0.0||first.frame.profile.image_copy_ms<=0.0 {return false}
|
|
if batch==0 {print("Opaque resolve ownership and transparent conversion use distinct fenced framebuffers\n")}
|
|
} else if r.buffers!=3 {
|
|
return false
|
|
}
|
|
}
|
|
gpu.direct_readback=true
|
|
scene.background=.{20,42,73,255}
|
|
// The real large target exercises an unfinished fence without sync interleave.
|
|
large:=NewImage(1280,800);defer VecFree(large.pixels)
|
|
before_large:=AsyncHash(*large)
|
|
if !SceneGpuSubmit(*gpu,*scene,camera,1280,800,40).queued {return false}
|
|
early:=SceneGpuPoll(*gpu,*large)
|
|
if !early.ok {return false}
|
|
if !early.completed {
|
|
unready+=1
|
|
if AsyncHash(*large)!=before_large {return false}
|
|
early=AsyncWait(*gpu,*large)
|
|
}
|
|
if !early.completed||early.request_id!=40 {return false}
|
|
expected.width=1280;expected.height=800
|
|
if !SceneGpuRender(*reference,*scene,camera,*expected).ok||AsyncHash(*large)!=AsyncHash(*expected) {return false}
|
|
// A failed request is never queued and full-quality sync remains usable.
|
|
uniforms[0].x=1e100
|
|
rejected:=SceneGpuSubmit(*gpu,*scene,camera,160,100,100)
|
|
if rejected.ok||rejected.diagnostic==""||SceneGpuMemoryUsage(*gpu).pending_frames!=0 {return false}
|
|
uniforms[0].x=0.3
|
|
if !SceneGpuSubmit(*gpu,*scene,camera,160,100,101).queued {return false}
|
|
if !SceneGpuSubmit(*gpu,*scene,camera,160,100,102).queued {return false}
|
|
before:=AsyncHash(*image)
|
|
if !SceneGpuResize(*gpu,80,50)||SceneGpuMemoryUsage(*gpu).pending_frames!=0||SceneGpuPoll(*gpu,*image).completed||AsyncHash(*image)!=before {return false}
|
|
image.width=80;image.height=50
|
|
if !SceneGpuSubmit(*gpu,*scene,camera,80,50,103).queued||!AsyncWait(*gpu,*image).completed {return false}
|
|
if !SceneGpuSubmit(*gpu,*scene,camera,80,50,104).queued {return false}
|
|
SceneGpuCancel(*gpu)
|
|
if SceneGpuMemoryUsage(*gpu).fences!=0||SceneGpuPoll(*gpu,*image).completed {return false}
|
|
// Context loss discards all pending IDs, and Release/recreate remains safe.
|
|
if !SceneGpuSubmit(*gpu,*scene,camera,80,50,105).queued {return false}
|
|
unused G.EDestroyContext(gpu.display,gpu.context)
|
|
lost:=SceneGpuPoll(*gpu,*image)
|
|
if lost.ok||lost.completed||SceneGpuMemoryUsage(*gpu).gpu_bytes!=0 {return false}
|
|
for cycle:0..23 {
|
|
if !SceneGpuProbe(*gpu).available {return false}
|
|
gpu.direct_readback=true
|
|
if !SceneGpuSubmit(*gpu,*scene,camera,80,50,1).queued {return false}
|
|
if !SceneGpuSubmit(*gpu,*scene,camera,80,50,2).queued {return false}
|
|
SceneGpuRelease(*gpu);SceneGpuRelease(*gpu)
|
|
r:=SceneGpuMemoryUsage(*gpu)
|
|
if r.cpu_bytes!=0||r.gpu_bytes!=0||r.buffers!=0||r.fences!=0||r.pending_frames!=0 {return false}
|
|
print("async release cycle %\n",cycle);unused AsyncFlush(null);unused AsyncPause(20000)
|
|
}
|
|
gpu.attempted=true;gpu.capabilities.diagnostic="test lacks full 4x coverage"
|
|
if SceneGpuSubmit(*gpu,*scene,camera,80,50,1).ok {return false}
|
|
fallback:=SceneGpuRenderOrCPU(*gpu,*scene,camera,*image)
|
|
if !fallback.ok||fallback.accelerated||fallback.profile.direct_resolve {return false}
|
|
SceneGpuRelease(*gpu)
|
|
if !SceneGpuProbe(*gpu).available {return false}
|
|
gpu.capabilities.async_available=false;gpu.capabilities.async_diagnostic="test lacks PBO mapping"
|
|
if SceneGpuSubmit(*gpu,*scene,camera,80,50,1).ok||!SceneGpuRender(*gpu,*scene,camera,*image).ok {return false}
|
|
print("Async exact camera/material/geometry/owned-texture/program frames, sync interleave, two-slot backpressure, cancellation, loss, fallback and 24 releases passed; unready polls %\n",unready)
|
|
return true
|
|
}
|
|
// Sync GPU conversion must match the original mapped-PBO CPU conversion for
|
|
// every alpha byte, including its integer rounding and zero-alpha RGB rule.
|
|
ReadbackAlphaParity :: ()->bool {
|
|
gpu: SceneGpuContext;defer SceneGpuRelease(*gpu)
|
|
scene: Scene;SceneInit(*scene);defer SceneFree(*scene)
|
|
program: ShaderProgram;defer ShaderFree(*program)
|
|
code: [2]ShaderInstruction=.[.{op=.ShaderSample,dest=4,a=3,slot=0},.{op=.ShaderOutput,a=4}]
|
|
if !ShaderCompile(*program,code[:]).ok {return false}
|
|
pixels: [256*4]u8
|
|
for at:0..255 {
|
|
pixels[at*4]=cast(u8)((at*7)%256);pixels[at*4+1]=cast(u8)((at*13)%256)
|
|
pixels[at*4+2]=cast(u8)((at*17)%256);pixels[at*4+3]=cast(u8)at
|
|
}
|
|
samplers: [1]ShaderSampler=.[.{256,1,pixels[:],false}]
|
|
material:=UnlitMaterial(.{255,255,255,255});material.transparent=true
|
|
material.shader=*program;material.shader_samplers=samplers[:]
|
|
unused Plane(*scene,2.0,2.0,material)
|
|
camera:=OrthographicCamera(-1.0,1.0,1.0,-1.0,0.1,10.0);camera.position=.{0.0,0.0,3.0}
|
|
sync:=NewImage(256,3);mapped:=NewImage(256,3)
|
|
defer VecFree(sync.pixels);defer VecFree(mapped.pixels)
|
|
if !SceneGpuProbe(*gpu).available {return false}
|
|
for request:1..2 {
|
|
scene.background=.{48,96,144,ifx request==1 then cast(u8)255 else cast(u8)0}
|
|
gpu.direct_readback=false
|
|
if !SceneGpuRender(*gpu,*scene,camera,*sync).ok {return false}
|
|
gpu.direct_readback=true
|
|
if !SceneGpuRender(*gpu,*scene,camera,*mapped).ok||AsyncHash(*sync)!=AsyncHash(*mapped) {return false}
|
|
gpu.direct_readback=request==1
|
|
if !SceneGpuSubmit(*gpu,*scene,camera,256,3,cast(u64)request).queued||!AsyncWait(*gpu,*mapped).completed {return false}
|
|
for at:0..sync.pixels.count-1 {
|
|
if sync.pixels[at]!=mapped.pixels[at] {print("readback alpha mismatch byte % sync % mapped %\n",at,sync.pixels[at],mapped.pixels[at]);return false}
|
|
if request==1&&at%4==3&&sync.pixels[at]!=cast(u8)255 {return false}
|
|
}
|
|
}
|
|
print("GPU readbacks match for opaque clears and all 256 alpha values\n")
|
|
return true
|
|
}
|
|
|
|
// Use the Balcony output size with textured geometry and an opaque clear.
|
|
// Alternate the two sync paths on the same warmed context and verify pixels
|
|
// outside the sampled stages. The async path is covered by AsyncTests.
|
|
ReadbackProfile :: ()->bool {
|
|
gpu: SceneGpuContext;defer SceneGpuRelease(*gpu)
|
|
caps:=SceneGpuProbe(*gpu)
|
|
if !caps.available {print("GPU unavailable %\n",caps.diagnostic);return false}
|
|
print("Readback GPU %\n",TextView(caps.renderer[:]))
|
|
SceneGpuSetProfiling(*gpu,true)
|
|
scene: Scene;SceneInit(*scene);defer SceneFree(*scene)
|
|
scene.background=.{32,48,64,255}
|
|
texture:=NewImage(2048,1024);defer VecFree(texture.pixels)
|
|
if !ImageValid(*texture) {return false}
|
|
for at:0..texture.pixels.count/4-1 {
|
|
texture.pixels[at*4]=cast(u8)(at%251)
|
|
texture.pixels[at*4+1]=cast(u8)((at/2048)%241)
|
|
texture.pixels[at*4+2]=100;texture.pixels[at*4+3]=255
|
|
}
|
|
material:=ImageMaterial(*scene,*texture,UnlitMaterial(.{255,255,255,255}))
|
|
panel:=Plane(*scene,3.0,1.5,material);SetPosition(*scene,panel,0.3,-0.1,0)
|
|
camera:=PerspectiveCamera(45,2527.0/1423.0,0.1,20);camera.position=.{0,0,3}
|
|
image:=NewImage(2527,1423);defer VecFree(image.pixels)
|
|
expected: u64
|
|
for sample:0..21 {
|
|
direct:=sample%2==0
|
|
gpu.direct_readback=direct
|
|
frame:=SceneGpuRender(*gpu,*scene,camera,*image)
|
|
if !frame.ok {print("Readback failed %\n",frame.diagnostic);return false}
|
|
if sample==0 {
|
|
expected=AsyncHash(*image)
|
|
if gpu.straight_framebuffer!=0||frame.profile.texture_upload_ms<=0.0 {return false}
|
|
} else if AsyncHash(*image)!=expected {return false}
|
|
p:=frame.profile
|
|
if p.direct_resolve!=direct {return false}
|
|
print("readback % % % % % % % %\n",sample,ifx direct then "resolve" else "straight",
|
|
p.shader_setup_ms,p.texture_upload_ms,p.gpu_completion_ms,p.pixel_transfer_ms,p.image_copy_ms,
|
|
frame.submit_ms+frame.readback_ms+p.image_copy_ms)
|
|
}
|
|
print("Readback frame hashes match %\n",expected)
|
|
return true
|
|
}
|
|
|
|
ProgramSwitches :: (check_cache: bool)->bool {
|
|
gpu: SceneGpuContext;defer SceneGpuRelease(*gpu)
|
|
if !SceneGpuProbe(*gpu).available {return false}
|
|
gpu.direct_readback=false
|
|
SceneGpuSetProfiling(*gpu,true)
|
|
scene: Scene;SceneInit(*scene);defer SceneFree(*scene)
|
|
scene.background=.{32,48,64,255}
|
|
programs: [3]ShaderProgram
|
|
code: [2]ShaderInstruction=.[.{op=.ShaderLiteral,dest=4,value=.{0.25,0.2,0.4,1.0}},.{op=.ShaderOutput,a=4}]
|
|
if !ShaderCompile(*programs[0],code[:]).ok {return false}
|
|
code[0].value.x=0.75
|
|
if !ShaderCompile(*programs[1],code[:]).ok {return false}
|
|
programs[2]=programs[0]
|
|
material:=UnlitMaterial(.{255,255,255,255});material.shader=*programs[0]
|
|
unused Plane(*scene,2.0,1.25,material)
|
|
camera:=PerspectiveCamera(45,1.6,0.1,20);camera.position=.{0,0,3}
|
|
image:=NewImage(96,60);defer VecFree(image.pixels)
|
|
hashes: [2]u64
|
|
for sample:0..23 {
|
|
variant:=sample%2
|
|
scene.meshes[0].material.shader=*programs[ifx sample%4==2 then 2 else variant]
|
|
frame:=SceneGpuRender(*gpu,*scene,camera,*image)
|
|
if !frame.ok {return false}
|
|
hash:=AsyncHash(*image)
|
|
if sample<2 {hashes[variant]=hash}
|
|
else if hash!=hashes[variant] {return false}
|
|
count:=SceneGpuMemoryUsage(*gpu).programs
|
|
if !check_cache {print("program switch % % % %\n",sample,frame.profile.shader_setup_ms,count,hash)}
|
|
if check_cache {
|
|
if sample>0&&count!=4 {return false}
|
|
for at:0..gpu.programs.count-1 {if gpu.programs[at].key!=null {return false}}
|
|
}
|
|
}
|
|
if hashes[0]==hashes[1] {return false}
|
|
if !check_cache {return true}
|
|
// Overwrite the same shader address with new code and render it. The cache
|
|
// must preserve the newest variants, and retain only a bounded number.
|
|
for revision:0..11 {
|
|
code[0].value.x=0.1+cast(float64)revision*0.06
|
|
if !ShaderCompile(*programs[0],code[:]).ok {return false}
|
|
scene.meshes[0].material.shader=*programs[0]
|
|
if !SceneGpuRender(*gpu,*scene,camera,*image).ok {return false}
|
|
hash:=AsyncHash(*image)
|
|
if hash==hashes[0]||SceneGpuMemoryUsage(*gpu).programs>GPU_RECENT_PROGRAMS+3 {return false}
|
|
hashes[0]=hash
|
|
}
|
|
print("Room program switches preserve pixels and bound the linked cache\n")
|
|
return true
|
|
}
|
|
|
|
#program_export
|
|
main :: (argc: s32, argv: **u8)->s32 {
|
|
unused argv
|
|
if argc==2 {return ifx ReadbackProfile() then 0 else 1}
|
|
if argc==3 {return ifx ProgramSwitches(false) then 0 else 1}
|
|
if !AsyncTests()||!ReadbackAlphaParity()||!ProgramSwitches(true) {return 1}
|
|
return 0
|
|
}
|