dymokomi/luce-gpu
Source of release 0.5.5
0.5.5 / tests / gpu / drawing.lucb
from luce_gpu import gpu
from luce_window import window
import native
import allocation
import memory
import shaders
func full(color: gpu.Vertex) -> gpu.Vertex[6]:
var a = color
var b = color
var c = color
var d = color
a.x = -1.0
a.y = -1.0
b.x = 1.0
b.y = -1.0
c.x = 1.0
c.y = 1.0
d.x = -1.0
d.y = 1.0
return [a, b, c, a, c, d]
pub func contracts() -> !:
var tracker = allocation.Tracker(upstream = memory.heap)
let previous = memory.heap
memory.heap = &tracker
defer allocation.restore(previous)
var canvas: gpu.Canvas
defer canvas.destroy()
let clip = gpu.Clip(width = 32, height = 32)
# Refusal at either growth stage leaves zero recorded vertices and cleanup
# releases all storage, including a successfully grown vertex allocation.
var stage: usize = 0
while stage < 2:
tracker.remaining = stage
var refused = false
canvas.triangles(full(gpu.Vertex(red = 1.0)), clip) catch failure:
assert(failure.code == memory.exhausted)
refused = true
tracker.remaining = none
assert(refused and canvas.count() == 0)
canvas.destroy()
assert(tracker.live == 0)
stage += 1
try canvas.triangles(full(gpu.Vertex(red = 1.0)), clip)
var invalid = full(gpu.Vertex(red = 1.0))
invalid[0].alpha = -0.1
var rejected = false
canvas.triangles(invalid, clip) catch failure:
assert(failure.code == gpu.invalid_geometry)
rejected = true
assert(rejected and canvas.count() == 6)
rejected = false
canvas.triangles(invalid[..<2], clip) catch failure:
assert(failure.code == gpu.invalid_geometry)
rejected = true
assert(rejected and canvas.count() == 6)
canvas.clear()
assert(canvas.count() == 0)
for bias in [-1.0,257.0,f64.bits(9221120237041090560),f64.bits(9218868437227405312)]:
rejected = false
canvas.triangles(full(gpu.Vertex(red = 1.0)),clip,true,slope_bias = bias) catch failure:
assert(failure.code == gpu.invalid_geometry)
rejected = true
assert(rejected and canvas.count() == 0)
rejected = false
canvas.triangles(full(gpu.Vertex(red = 1.0)),clip,false,slope_bias = 1.0) catch failure:
assert(failure.code == gpu.invalid_geometry)
rejected = true
assert(rejected and canvas.count() == 0)
canvas.destroy()
assert(tracker.live == 0)
# Dense 3D is explicit: the default cap still rejects the same workload.
let dense = try new gpu.Vertex[1048578] --- in memory.heap
for index in 0..<dense.length:
dense[index] = gpu.Vertex(red = 1.0)
rejected = false
canvas.triangles(dense, clip) catch failure:
assert(failure.code == gpu.command_limit)
rejected = true
assert(rejected and canvas.count() == 0)
try canvas.allow_vertices(2097152)
try canvas.triangles(dense, clip)
assert(canvas.count() == dense.length)
rejected = false
canvas.allow_vertices(8388609) catch failure:
assert(failure.code == gpu.command_limit)
rejected = true
assert(rejected and canvas.count() == dense.length)
free(dense) in memory.heap
canvas.destroy()
assert(tracker.live == 0)
# The draw limit: 4096 by default, raised by allow_draws up to 65536.
let one = full(gpu.Vertex(green = 1.0))
assert(canvas.draw_limit() == 4096)
for index in 0..<(usize)4096:
try canvas.triangles(one, clip)
rejected = false
canvas.triangles(one, clip) catch failure:
assert(failure.code == gpu.command_limit)
rejected = true
assert(rejected and canvas.count() == 4096 * 6)
try canvas.allow_draws(5000)
try canvas.allow_draws(100)
assert(canvas.draw_limit() == 5000)
for index in 4096..<(usize)5000:
try canvas.triangles(one, clip)
rejected = false
canvas.triangles(one, clip) catch failure:
assert(failure.code == gpu.command_limit)
rejected = true
assert(rejected and canvas.count() == 5000 * 6)
rejected = false
canvas.allow_draws(65537) catch failure:
assert(failure.code == gpu.command_limit)
rejected = true
assert(rejected and canvas.draw_limit() == 5000)
canvas.destroy()
assert(tracker.live == 0)
pub func pixels() -> !:
var device = try gpu.Device.open()
defer device.destroy()
var target = try window.Window.open(window.Options(title = "GPU triangles", width = 96, height = 64))
defer target.destroy()
var surface = try gpu.Surface.open(device, target)
defer surface.destroy()
try target.show()
while (try target.poll()) != none:
_ = 0
let size = try surface.size()
let width = (u32)size.pixel_width
let height = (u32)size.pixel_height
let clip = gpu.Clip(width = width, height = height)
var canvas: gpu.Canvas
defer canvas.destroy()
try canvas.triangles(full(gpu.Vertex(red = 1.0, z = 0.2)), clip, true)
# A later, farther primitive must fail depth testing.
try canvas.triangles(full(gpu.Vertex(blue = 1.0, z = 0.8)), clip, true)
native.begin("GPU triangles")
defer native.end()
assert(try surface.render(canvas) == gpu.PresentResult.submitted)
canvas.clear()
try surface.wait_idle()
native.pixels((u64)width, (u64)height, 255, 0, 0)
native.end()
# Scissoring restricts the draw to the left half. Straight alpha blends in
# linear space, producing sRGB 188 for a 50% channel, not an encoded 128.
try canvas.triangles(full(gpu.Vertex(red = 1.0, alpha = 0.5)), gpu.Clip(width = width // 2, height = height))
native.begin("GPU triangles")
assert(try surface.render(canvas, gpu.Color(blue = 1.0)) == gpu.PresentResult.submitted)
try surface.wait_idle()
native.pixel(0, 0, 188, 0, 188)
native.pixel((u64)width - 1, 0, 0, 0, 255)
native.end()
# Recreate depth storage after resize and verify commands survive CPU reuse.
try target.resize(80, 48)
while (try target.poll()) != none:
_ = 0
canvas.clear()
try canvas.triangles(full(gpu.Vertex(green = 1.0)), gpu.Clip(width = 10000, height = 10000))
native.begin("GPU triangles")
assert(try surface.render(canvas) == gpu.PresentResult.submitted)
canvas.destroy()
try surface.wait_idle()
let resized = try surface.size()
native.pixels((u64)resized.pixel_width, (u64)resized.pixel_height, 0, 255, 0)
native.end()
# A sub-viewport maps local clip coordinates into the widget region. A
# following draw without a viewport must restore full-surface mapping.
let half_width = resized.pixel_width * 0.5
try canvas.triangles(full(gpu.Vertex(red = 1.0)), gpu.Clip(width = 10000, height = 10000), false, gpu.Viewport(x = half_width, width = half_width, height = resized.pixel_height))
native.begin("GPU triangles")
assert(try surface.render(canvas) == gpu.PresentResult.submitted)
try surface.wait_idle()
native.pixel(0, 0, 0, 0, 0)
native.pixel((u64)resized.pixel_width - 1, 0, 255, 0, 0)
native.end()
# Retained-only frames upload no dynamic vertices. Destroying input owners
# after recording is safe; depth-tested line expansion runs on the GPU.
var retained = try gpu.Geometry.create(device, full(gpu.Vertex(red = 1.0, z = 0.8)))
var wire = try gpu.Geometry.create(device, [gpu.Vertex(x = -1.0, y = 0.0, z = 0.2, red = 1.0, green = 0.0, blue = 0.2)], true)
let frame = try surface.frame()
defer frame.release()
let view = try frame.value().target()
defer view.release()
let matrix: f32[16] = [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0]
try gpu.draw_geometry(try view.get(), retained, matrix)
try gpu.draw_geometry(try view.get(), wire, matrix, gpu.Color(green = 1.0), 4.0)
retained.destroy()
wire.destroy()
native.begin("GPU triangles")
_ = try frame.value().present()
try surface.wait_idle()
native.pixel((u64)resized.pixel_width // 2, (u64)resized.pixel_height // 2, 0, 255, 0)
native.pixel((u64)resized.pixel_width // 2, 0, 255, 0, 0)
## An encoded surface blends on encoded values: half-alpha red over blue is 128
## where a linear surface shows 188, while a clear and opaque draws look the
## same; client pipelines made for surfaces draw there too.
pub func encoded() -> !:
var device = try gpu.Device.open()
defer device.destroy()
var target = try window.Window.open(window.Options(title = "GPU encoded", width = 96, height = 64))
defer target.destroy()
var surface = try gpu.Surface.open(device, target, gpu.Blending.encoded)
defer surface.destroy()
assert((try surface.blending()) == gpu.Blending.encoded)
try target.show()
while (try target.poll()) != none:
_ = 0
let size = try surface.size()
let width = (u32)size.pixel_width
let height = (u32)size.pixel_height
# A linear-light clear of 0.5 is encoded to 188, as on a linear surface.
native.begin("GPU encoded")
assert((try surface.clear_present(gpu.Color(0.5, 0.5, 0.5))) == gpu.PresentResult.submitted)
try surface.wait_idle()
native.pixels((u64)width, (u64)height, 188, 188, 188)
native.end()
# Half-alpha red over the left half of a blue clear: 128, 0, 128.
let frame = try surface.frame()
let root = try frame.value().target()
assert((try root.value().blending()) == gpu.Blending.encoded and (try root.value().pixel_format()) == none)
let left = try root.value().region(gpu.Rect(width = size.width / 2.0, height = size.height))
try left.value().triangles(full(gpu.Vertex(red = 1.0, alpha = 0.5)))
left.release()
root.release()
native.begin("GPU encoded")
assert((try frame.value().present(gpu.Color(blue = 1.0))) == gpu.PresentResult.submitted)
frame.release()
try surface.wait_idle()
native.pixel(0, 0, 128, 0, 128)
native.pixel((u64)width - 1, 0, 0, 0, 255)
native.end()
# A client pipeline made for surfaces writes its output as is: (1, 0.5, 0.5)
# premultiplied stays 255, 128, 128.
var shader = try gpu.Shader.create(device, shaders.tint_frag_words, shaders.tint_frag_msl)
defer shader.destroy()
var pipeline = try gpu.Pipeline.create(device, shader, gpu.Blend.over)
defer pipeline.destroy()
var white = try gpu.Texture.create(device, 1, 1, gpu.Format.rgba8_linear)
defer white.destroy()
try white.upload([(u8)255, (u8)255, (u8)255, (u8)255])
var uniforms: f32[9] = [1.0, 0.5, 0.5, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0]
let shaded = try surface.frame()
let whole = try shaded.value().target()
try gpu.shade(whole.value(), pipeline, gpu.Rect(width = size.width, height = size.height), u8[]((u8*)&uniforms[0], 36), [white, white])
whole.release()
native.begin("GPU encoded")
assert((try shaded.value().present()) == gpu.PresentResult.submitted)
shaded.release()
try surface.wait_idle()
native.pixel(1, 1, 255, 128, 128)
native.end()