dymokomi/luce-gpu
Source of release 0.5.1
0.5.1 / tests / programs / gpu / textures.lucb
from luce_gpu import gpu
import memory
import shaders
## Texture contracts that need a device but no window: uploads, offscreen
## frames, image draws with both filters, readback, and refusals.
pub func pixels(device: gpu.Device) -> !:
# The device reports a budget, and what it holds grows with a large texture.
let before = try device.memory()
assert(before.budget > 0)
var large = try gpu.Texture.create(device, 2048, 2048, gpu.Format.rgba16_float)
let held = try device.memory()
assert(held.used >= before.used)
large.destroy()
# A 2x2 sRGB texture: red, green / blue, opaque white with half alpha.
var source = try gpu.Texture.create(device, 2, 2, gpu.Format.rgba8)
defer source.destroy()
assert((try source.width()) == 2 and (try source.height()) == 2 and (try source.pixel_format()) == gpu.Format.rgba8)
var texels: u8[16] = [255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255, 255, 255, 255, 128]
try source.upload(texels)
var back: u8[16]
try source.read(back)
for index in 0..<(usize)16:
assert(back[index] == texels[index])
# A partial upload and a partial readback address the same texels.
var corner: u8[4] = [10, 20, 30, 255]
try source.upload(corner, gpu.Region(1, 1, 1, 1))
var one: u8[4]
try source.read(one, gpu.Region(1, 1, 1, 1))
assert(one[0] == 10 and one[1] == 20 and one[2] == 30 and one[3] == 255)
try source.upload(texels)
# Nearest sampling scales each texel onto a 2x2 block of a linear target.
var target = try gpu.Texture.create(device, 4, 4, gpu.Format.rgba8_linear)
defer target.destroy()
let frame = try target.frame()
defer frame.release()
let root = try frame.value().target()
defer root.release()
try gpu.draw_image(root.value(), source, gpu.Rect(width = 4.0, height = 4.0), filter = gpu.Filter.nearest)
assert((try frame.value().present(gpu.Color(0.0, 0.0, 0.0, 0.0))) == gpu.PresentResult.submitted)
var result: u8[64]
try target.read(result)
# sRGB 255 decodes to linear 1.0; the linear target stores it unchanged.
assert(result[0] == 255 and result[1] == 0 and result[2] == 0 and result[3] == 255)
assert(result[8] == 0 and result[9] == 255 and result[10] == 0)
assert(result[32] == 0 and result[33] == 0 and result[34] == 255)
# Half-alpha white over a transparent clear stays premultiplied: 128, 128, 128, 128.
assert(result[60] >= 126 and result[60] <= 129 and result[63] >= 126 and result[63] <= 129)
# A copy writes texels as stored: the half-alpha white stays straight...
var copied = try gpu.Texture.create(device, 4, 4, gpu.Format.rgba8_linear)
defer copied.destroy()
let copy_frame = try copied.frame()
defer copy_frame.release()
let copy_root = try copy_frame.value().target()
defer copy_root.release()
try gpu.copy_image(copy_root.value(), source, gpu.Rect(width = 4.0, height = 4.0))
assert((try copy_frame.value().present(gpu.Color(0.0, 0.0, 0.0, 0.0))) == gpu.PresentResult.submitted)
try copied.read(result)
assert(result[0] == 255 and result[1] == 0 and result[3] == 255)
assert(result[60] == 255 and result[62] == 255 and result[63] >= 126 and result[63] <= 129)
# ...and the premultiplied drawing above, straightened, is white at half alpha.
var straight = try gpu.Texture.create(device, 4, 4, gpu.Format.rgba8_linear)
defer straight.destroy()
let straight_frame = try straight.frame()
defer straight_frame.release()
let straight_root = try straight_frame.value().target()
defer straight_root.release()
try gpu.copy_image(straight_root.value(), target, gpu.Rect(width = 4.0, height = 4.0), straighten = true)
assert((try straight_frame.value().present(gpu.Color(0.0, 0.0, 0.0, 0.0))) == gpu.PresentResult.submitted)
try straight.read(result)
assert(result[0] == 255 and result[3] == 255)
assert(result[60] >= 253 and result[62] >= 253 and result[63] >= 126 and result[63] <= 129)
# A solid quad, an opacity-scaled image and clipping compose on one frame.
var canvas = try gpu.Texture.create(device, 8, 8, gpu.Format.rgba8)
defer canvas.destroy()
let second = try canvas.frame()
defer second.release()
let whole = try second.value().target()
defer whole.release()
let vertices = [gpu.Vertex(x = -1.0, y = -1.0, blue = 1.0), gpu.Vertex(x = 1.0, y = -1.0, blue = 1.0), gpu.Vertex(x = 1.0, y = 1.0, blue = 1.0),
gpu.Vertex(x = -1.0, y = -1.0, blue = 1.0), gpu.Vertex(x = 1.0, y = 1.0, blue = 1.0), gpu.Vertex(x = -1.0, y = 1.0, blue = 1.0)]
try whole.value().triangles(vertices)
let left = try whole.value().region(gpu.Rect(width = 4.0, height = 8.0))
defer left.release()
try gpu.draw_image(left.value(), source, gpu.Rect(x = -4.0, width = 8.0, height = 8.0), gpu.Region(0, 0, 1, 1), 1.0, gpu.Filter.nearest)
assert((try second.value().present()) == gpu.PresentResult.submitted)
var composed: u8[256]
try canvas.read(composed)
# Left half: the red texel drawn over blue; right half: untouched blue.
assert(composed[0] == 255 and composed[1] == 0 and composed[2] == 0)
assert(composed[7 * 4] == 0 and composed[7 * 4 + 2] == 255)
# Refusals: bad regions, wrong data length, self-sampling, another frame while open.
var rejected = false
source.upload(texels, gpu.Region(1, 1, 2, 2)) catch failure:
assert(failure.code == gpu.invalid_geometry)
rejected = true
assert(rejected)
rejected = false
source.upload(corner) catch failure:
assert(failure.code == gpu.invalid_pixels)
rejected = true
assert(rejected)
rejected = false
let loop = try source.frame()
defer loop.release()
let inside = try loop.value().target()
defer inside.release()
try gpu.draw_image(inside.value(), source, gpu.Rect(width = 2.0, height = 2.0))
_ = loop.value().present() catch failure:
assert(failure.code == gpu.invalid_geometry)
rejected = true
recover gpu.PresentResult.skipped
assert(rejected)
rejected = false
let again = source.frame() catch failure:
assert(failure.code == gpu.frame_in_use)
rejected = true
recover try target.frame()
again.release()
# The failed present closed the loop frame, so the texture is free again.
assert(not rejected)
rejected = false
_ = gpu.Texture.create(device, 0, 4, gpu.Format.r8) catch failure:
assert(failure.code == gpu.invalid_geometry)
rejected = true
recover gpu.Texture()
assert(rejected)
# A destroyed handle refuses, while a draw recorded from it keeps drawing.
var doomed = try gpu.Texture.create(device, 1, 1, gpu.Format.r8)
var gray: u8[1] = [200]
try doomed.upload(gray)
let third = try target.frame()
defer third.release()
let full = try third.value().target()
defer full.release()
try gpu.draw_image(full.value(), doomed, gpu.Rect(width = 4.0, height = 4.0))
doomed.destroy()
rejected = false
_ = doomed.width() catch failure:
assert(failure.code == gpu.closed)
rejected = true
recover 0
assert(rejected)
assert((try third.value().present()) == gpu.PresentResult.submitted)
try target.read(result)
# r8 samples as (r, 0, 0, 1): stored red 200 straight into the linear target.
assert(result[0] >= 199 and result[0] <= 201 and result[1] == 0 and result[3] == 255)
## Client fragment shaders: uniforms, two sampled images, the three blend
## modes, and the refusals for wrong targets and devices.
pub func programs(device: gpu.Device) -> !:
var shader = try gpu.Shader.create(device, shaders.tint_frag_words, shaders.tint_frag_msl)
defer shader.destroy()
var over = try gpu.Pipeline.create(device, shader, gpu.Blend.over, gpu.Format.rgba8_linear)
defer over.destroy()
var replace = try gpu.Pipeline.create(device, shader, gpu.Blend.replace, gpu.Format.rgba8_linear)
defer replace.destroy()
var add = try gpu.Pipeline.create(device, shader, gpu.Blend.add, gpu.Format.rgba8_linear)
defer add.destroy()
assert((try over.blend()) == gpu.Blend.over)
var first = try gpu.Texture.create(device, 1, 1, gpu.Format.rgba8_linear)
defer first.destroy()
try first.upload([(u8)255, (u8)128, (u8)255, (u8)255])
var second = try gpu.Texture.create(device, 1, 1, gpu.Format.r8)
defer second.destroy()
try second.upload([(u8)64])
var target = try gpu.Texture.create(device, 4, 4, gpu.Format.rgba8_linear)
defer target.destroy()
# tint (1, 1, 0.5, 1), the rectangle in pixels, offset scale 1: fragment =
# white * tint * first + second.r = (1 + 0.25, 0.5, 0.5, 1), red clamped.
var uniforms: f32[9] = [1.0, 1.0, 0.5, 1.0, 0.0, 0.0, 4.0, 4.0, 1.0]
let bytes = u8[]((u8*)&uniforms[0], 36)
let frame = try target.frame()
defer frame.release()
let root = try frame.value().target()
defer root.release()
try gpu.shade(root.value(), over, gpu.Rect(width = 4.0, height = 4.0), bytes, [first, second])
assert((try frame.value().present(gpu.Color(0.0, 0.0, 1.0, 1.0))) == gpu.PresentResult.submitted)
var pixel: u8[4]
try target.read(pixel, gpu.Region(1, 1, 1, 1))
# 0.5 is 127.5 in eight bits; drivers may round either way.
assert(pixel[0] == 255 and pixel[1] >= 127 and pixel[1] <= 128 and pixel[2] >= 127 and pixel[2] <= 128 and pixel[3] == 255)
# replace ignores the blue clear beneath a half-alpha draw; add sums with it.
uniforms[3] = 0.5
let half = try target.frame()
defer half.release()
let whole = try half.value().target()
defer whole.release()
try gpu.shade(whole.value(), replace, gpu.Rect(width = 2.0, height = 4.0), bytes, [first, second], gpu.Filter.nearest)
try gpu.shade(whole.value(), add, gpu.Rect(x = 2.0, width = 2.0, height = 4.0), bytes, [first, second], gpu.Filter.nearest)
assert((try half.value().present(gpu.Color(0.0, 0.0, 1.0, 1.0))) == gpu.PresentResult.submitted)
try target.read(pixel, gpu.Region(0, 0, 1, 1))
# replace: premultiplied (0.625, 0.25, 0.125, 0.5) as stored.
assert(pixel[0] >= 158 and pixel[0] <= 161 and pixel[1] >= 63 and pixel[1] <= 65 and pixel[3] == 128)
try target.read(pixel, gpu.Region(3, 0, 1, 1))
# add: the same plus the opaque blue clear: blue saturates, alpha saturates.
assert(pixel[0] >= 158 and pixel[0] <= 161 and pixel[2] == 255 and pixel[3] == 255)
# A pipeline made for rgba8_linear cannot draw into an rgba8 texture.
var other = try gpu.Texture.create(device, 2, 2, gpu.Format.rgba8)
defer other.destroy()
let wrong = try other.frame()
defer wrong.release()
let area = try wrong.value().target()
defer area.release()
try gpu.shade(area.value(), over, gpu.Rect(width = 2.0, height = 2.0))
var rejected = false
_ = wrong.value().present() catch failure:
assert(failure.code == gpu.wrong_target)
rejected = true
recover gpu.PresentResult.skipped
assert(rejected)
# Bad SPIR-V and a closed shader are refused.
rejected = false
_ = gpu.Shader.create(device, [(u32)1, (u32)2, (u32)3, (u32)4, (u32)5], shaders.tint_frag_msl) catch failure:
assert(failure.code == gpu.invalid_shader)
rejected = true
recover gpu.Shader()
assert(rejected)
var closed = try gpu.Shader.create(device, shaders.tint_frag_words, shaders.tint_frag_msl)
closed.destroy()
rejected = false
_ = gpu.Pipeline.create(device, closed, gpu.Blend.over) catch failure:
assert(failure.code == gpu.closed)
rejected = true
recover gpu.Pipeline()
assert(rejected)
## A read batch: copies of many regions at chosen offsets and row pitches, one
## submission and one wait, views of the bytes in place; a full batch refuses
## more, a texture destroyed after its copy is recorded still reads, the ring's
## slots run out only while held, and a read larger than a slot is split.
pub func batches(device: gpu.Device) -> !:
var left = try gpu.Texture.create(device, 256, 256, gpu.Format.rgba8_linear)
var right = try gpu.Texture.create(device, 256, 256, gpu.Format.rgba8_linear)
defer right.destroy()
let texels = try new u8[256 * 256 * 4] --- in memory.heap
defer free(texels) in memory.heap
for side in 0..<(usize)2:
for y in 0..<(usize)256:
for x in 0..<(usize)256:
let at = (y * 256 + x) * 4
texels[at] = (u8)x
texels[at + 1] = (u8)y
texels[at + 2] = (u8)(side * 100)
texels[at + 3] = 255
try (left if side == 0 else right).upload(texels)
var batch = try gpu.ReadBatch.begin(device)
defer batch.finish()
assert(batch.capacity() >= 1 << 20)
# Both tiles side by side: one strip 512 texels wide.
let strip = (try batch.reserve(512 * 256 * 4)) else trap("a strip fits")
try batch.copy(left, gpu.Region(0, 0, 256, 256), strip, row_texels = 512)
try batch.copy(right, gpu.Region(0, 0, 256, 256), strip + 256 * 4, row_texels = 512)
# A packed corner of the right tile after it.
let corner = (try batch.add(right, gpu.Region(10, 20, 3, 2))) else trap("a corner fits")
assert(corner >= strip + 512 * 256 * 4 and batch.copies() == 3)
assert((try batch.reserve(batch.capacity() + 1)) == none)
# The batch keeps what it reads alive until it is submitted.
left.destroy()
assert(refuses_bytes(&batch))
try batch.submit()
try batch.wait()
let bytes = try batch.bytes(strip, 512 * 256 * 4)
for y in [(usize)0, 7, 255]:
for x in [(usize)0, 3, 255, 256, 300, 511]:
let at = (y * 512 + x) * 4
assert(bytes[at] == (u8)(x % 256) and bytes[at + 1] == (u8)y and bytes[at + 2] == (u8)(0 if x < 256 else 100) and bytes[at + 3] == 255)
let small = try batch.bytes(corner, 3 * 2 * 4)
assert(small[0] == 10 and small[1] == 20 and small[2] == 100 and small[12] == 10 and small[13] == 21)
# Recording again into the same slot.
try batch.reset()
let again = (try batch.add(right, gpu.Region(255, 255, 1, 1))) else trap("a texel fits")
try batch.submit()
try batch.wait()
assert((try batch.bytes(again, 4))[0] == 255)
# Polled rather than waited for: done at some point, then the same bytes.
try batch.reset()
let polled = (try batch.add(right, gpu.Region(3, 4, 1, 1))) else trap("a texel fits")
try batch.submit()
var polls = 0
while not (try batch.ready()):
polls += 1
assert(polls < 100000000)
assert(try batch.ready())
let texel = try batch.bytes(polled, 4)
assert(texel[0] == 3 and texel[1] == 4 and texel[2] == 100)
# Three slots at most are held at once.
var second = try gpu.ReadBatch.begin(device)
var third = try gpu.ReadBatch.begin(device)
var refused = false
var fourth = gpu.ReadBatch.begin(device) catch failure:
_ = failure
refused = true
recover gpu.ReadBatch()
assert(refused)
fourth.finish()
second.finish()
third.finish()
# A read larger than a slot comes back whole, a slot's worth of rows at a time.
var large = try gpu.Texture.create(device, 4096, 4096, gpu.Format.rgba8_linear)
defer large.destroy()
let whole = try new u8[4096 * 4096 * 4] --- in memory.heap
defer free(whole) in memory.heap
for row in 0..<(usize)4096:
for column in 0..<(usize)4096:
let at = (row * 4096 + column) * 4
whole[at] = (u8)(row % 251)
whole[at + 1] = (u8)(column % 241)
whole[at + 2] = 7
whole[at + 3] = 255
try large.upload(whole)
let back = try new u8[4096 * 4096 * 4] --- in memory.heap
defer free(back) in memory.heap
try large.read(back)
for row in [(usize)0, 1000, 2048, 4095]:
for column in [(usize)0, 17, 4095]:
let at = (row * 4096 + column) * 4
assert(back[at] == (u8)(row % 251) and back[at + 1] == (u8)(column % 241) and back[at + 2] == 7)
## Whether the batch refuses to show its bytes (before it has been waited for).
func refuses_bytes(batch: gpu.ReadBatch*) -> bool:
let view = batch.bytes(0, 4) catch failure:
_ = failure
return true
_ = view
return false
## Upload batches: many regions of many textures in one submission, in the
## order added; a batch larger than the upload ring's slot still arrives whole.
pub func upload_batches(device: gpu.Device) -> !:
var first = try gpu.Texture.create(device, 256, 256, gpu.Format.rgba8_linear)
defer first.destroy()
var second = try gpu.Texture.create(device, 256, 256, gpu.Format.rgba8_linear)
defer second.destroy()
let rows = try new u8[256 * 256 * 4] --- in memory.heap
defer free(rows) in memory.heap
var batch = try gpu.UploadBatch.begin(device)
defer batch.finish()
# The whole of the first, then a band of it again, then a corner of the second.
for at in 0..<rows.length:
rows[at] = 10
try batch.add(first, rows)
for at in 0..<rows.length:
rows[at] = 20
try batch.add(first, rows[..<256 * 16 * 4], gpu.Region(0, 100, 256, 16))
try batch.add(second, rows[..<4 * 4 * 4], gpu.Region(250, 250, 4, 4))
assert(batch.pending() == 3)
# What a batch writes stays alive until it is submitted.
second.destroy()
try batch.submit()
assert(batch.pending() == 0 and batch.submitted() == 1)
var texel: u8[4]
try first.read(texel, gpu.Region(3, 99, 1, 1))
assert(texel[0] == 10)
try first.read(texel, gpu.Region(3, 100, 1, 1))
assert(texel[0] == 20)
try first.read(texel, gpu.Region(255, 115, 1, 1))
assert(texel[0] == 20)
try first.read(texel, gpu.Region(255, 116, 1, 1))
assert(texel[0] == 10)
# Enough cells to fill several ring slots: each still gets its own bytes.
var many: gpu.Texture[48] = ---
for index in 0..<(usize)48:
many[index] = try gpu.Texture.create(device, 256, 256, gpu.Format.rgba16_float)
let cell = try new u8[256 * 256 * 8] --- in memory.heap
defer free(cell) in memory.heap
for index in 0..<(usize)48:
for at in 0..<cell.length:
cell[at] = (u8)(index + 1)
try batch.add(many[index], cell)
try batch.submit()
for index in 0..<(usize)48:
var half: u8[8]
try many[index].read(half, gpu.Region(128, 200, 1, 1))
assert(half[0] == (u8)(index + 1) and half[7] == (u8)(index + 1))
many[index].destroy()