dymokomi/luce-gpu
Source of release 0.5.2
0.5.2 / tests / gpu / instances.lucb
from luce_gpu import gpu
import memory
import shaders
## shade_instances: many rectangles in one draw, each with its own values at
## the fragment's locations 1..3, clipped like other draws, and the same
## fragment program under plain `shade` (which hands it zeros).
pub func pixels(device: gpu.Device) -> !:
var shader = try gpu.Shader.create(device, shaders.instance_frag_words, shaders.instance_frag_msl)
defer shader.destroy()
var pipeline = try gpu.Pipeline.create(device, shader, gpu.Blend.over, gpu.Format.rgba8_linear)
defer pipeline.destroy()
var texture = try gpu.Texture.create(device, 64, 64, gpu.Format.rgba8_linear)
defer texture.destroy()
# 4,096 one-pixel instances, one per pixel, in one draw: the default draw
# limit would refuse them as separate draws. Red is x * 4, green y * 4.
let instances = try new gpu.ShadeInstance[4096] --- in memory.heap
defer free(instances) in memory.heap
for index in 0..<(usize)4096:
let x = index % 64
let y = index // 64
var instance = gpu.ShadeInstance(rect = [(f32)x, (f32)y, 1.0, 1.0])
instance.data[0] = (f32)(x * 4) / 255.0
instance.data[1] = (f32)(y * 4) / 255.0
instance.data[3] = 1.0
instance.data[4] = 1.0
instances[index] = instance
var uniforms: f32[2] = [1.0, 0.0]
let frame = try texture.frame()
let root = try frame.value().target()
try gpu.shade_instances(root.value(), pipeline, instances, u8[]((u8*)&uniforms[0], 8))
# A second, clipped call: an opaque blue square over everything, through
# data2, kept to a region from (8, 8) to (16, 16).
let region = try root.value().clipped(gpu.Rect(x = 8.0, y = 8.0, width = 8.0, height = 8.0))
var cover = gpu.ShadeInstance(rect = [0.0, 0.0, 64.0, 64.0])
cover.data[10] = 1.0
cover.data[11] = 1.0
try gpu.shade_instances(region.value(), pipeline, [cover], u8[]((u8*)&uniforms[0], 8))
region.release()
# Plain shade with the same program: data is zero, the base term draws.
uniforms[1] = 1.0
try gpu.shade(root.value(), pipeline, gpu.Rect(x = 60.0, y = 60.0, width = 4.0, height = 4.0), u8[]((u8*)&uniforms[0], 8))
# Zero instances record nothing.
try gpu.shade_instances(root.value(), pipeline, instances[..<0])
root.release()
assert((try frame.value().present(gpu.Color(0.0, 0.0, 0.0, 1.0))) == gpu.PresentResult.submitted)
frame.release()
var texels: u8[16384]
try texture.read(texels)
for y in 0..<(usize)64:
for x in 0..<(usize)64:
let at = (y * 64 + x) * 4
if x >= 60 and y >= 60:
assert(texels[at] == 0 and texels[at + 1] == 0 and texels[at + 2] == 255)
elif x >= 8 and x < 16 and y >= 8 and y < 16:
assert(texels[at + 2] == 255 and texels[at] == 0)
else:
assert(texels[at] == (u8)(x * 4) and texels[at + 1] == (u8)(y * 4) and texels[at + 2] == 0 and texels[at + 3] == 255)
# Refusals: a rectangle that is not finite, data that is not finite.
let again = try texture.frame()
defer again.release()
let target = try again.value().target()
defer target.release()
var bad = gpu.ShadeInstance(rect = [0.0, 0.0, -1.0, 1.0])
var rejected = false
gpu.shade_instances(target.value(), pipeline, [bad]) catch failure:
assert(failure.code == gpu.invalid_geometry)
rejected = true
assert(rejected)
bad = gpu.ShadeInstance(rect = [0.0, 0.0, 1.0, 1.0])
bad.data[5] = f32.bits(0x7fc00000)
rejected = false
gpu.shade_instances(target.value(), pipeline, [bad]) catch failure:
assert(failure.code == gpu.invalid_geometry)
rejected = true
assert(rejected)