Harbor

branch main
showing the latest snapshot on main
pipeline_desc_test.odin 4.3 KB · Plain text
tests/render_ir/pipeline_desc_test.odin 0644 Raw
package render_ir_tests

import ir "../../render_ir"
import "core:testing"

@(test)
test_default_graphics_pipeline_state :: proc(t: ^testing.T) {
	state := ir.default_graphics_pipeline_state()

	testing.expect(t, state.layout_variant == .PNU)
	testing.expect(t, state.topology == .Triangle_List)
	testing.expect(t, state.cull_mode == .Back)
	testing.expect(t, state.front_face == .Counter_Clockwise)
	testing.expect(t, state.enable_blending)
	testing.expect(t, state.blend_mode == .Alpha)
	testing.expect(t, !state.enable_depth_test)
	testing.expect(t, !state.enable_depth_write)
	testing.expect(t, state.color_format == .B8G8R8A8_SRGB)
	testing.expect(t, state.depth_format == .Undefined)
	testing.expect_value(t, state.push_constant_size, u32(0))
	testing.expect_value(t, ir.format_shader_stage_flags(state.push_constant_stages), "None")
}

@(test)
test_add_graphics_pipeline_stores_state :: proc(t: ^testing.T) {
	frame := ir.init_frame_ir()
	defer ir.destroy_frame_ir(&frame)

	vert := ir.add_shader_source(&frame, "rect.vert", .Vertex, "vertex main() {}")
	frag := ir.add_shader_source(&frame, "rect.frag", .Fragment, "fragment main() {}")
	state := ir.default_graphics_pipeline_state()
	state.vertex_shader = vert
	state.fragment_shader = frag
	state.layout_variant = .PNUC
	state.push_constant_size = 64
	state.push_constant_stages = {.Vertex, .Fragment}

	pipeline := ir.add_graphics_pipeline(&frame, "rect", state)
	desc, ok := ir.get_pipeline(&frame, pipeline)

	testing.expect(t, ok)
	testing.expect(t, desc.kind == .Graphics)
	testing.expect(t, desc.graphics.vertex_shader == vert)
	testing.expect(t, desc.graphics.fragment_shader == frag)
	testing.expect(t, desc.graphics.layout_variant == .PNUC)
	testing.expect_value(t, desc.graphics.push_constant_size, u32(64))
	testing.expect(t, .Vertex in desc.graphics.push_constant_stages)
	testing.expect(t, .Fragment in desc.graphics.push_constant_stages)
}

@(test)
test_add_compute_pipeline_stores_state :: proc(t: ^testing.T) {
	frame := ir.init_frame_ir()
	defer ir.destroy_frame_ir(&frame)

	shader := ir.add_shader_source(&frame, "particles.comp", .Compute, "compute main() {}")
	state := ir.default_compute_pipeline_state(shader)
	state.push_constant_size = 16
	state.push_constant_stages = {.Compute}

	pipeline := ir.add_compute_pipeline(&frame, "particles", state)
	desc, ok := ir.get_pipeline(&frame, pipeline)

	testing.expect(t, ok)
	testing.expect(t, desc.kind == .Compute)
	testing.expect(t, desc.compute.shader == shader)
	testing.expect_value(t, desc.compute.push_constant_size, u32(16))
	testing.expect(t, .Compute in desc.compute.push_constant_stages)
}

@(test)
test_pipeline_cache_keys_are_deterministic :: proc(t: ^testing.T) {
	gfx := ir.default_graphics_pipeline_state()
	gfx.vertex_shader = ir.Shader_Handle(1)
	gfx.fragment_shader = ir.Shader_Handle(2)
	gfx.enable_depth_test = true
	gfx.enable_depth_write = true
	gfx.depth_format = .D32_SFLOAT
	gfx.push_constant_size = 64
	gfx.push_constant_stages = {.Vertex, .Fragment}

	gfx_key := ir.format_pipeline_cache_key({kind = .Graphics, name = "rect", graphics = gfx})
	defer delete(gfx_key)
	testing.expect_value(
		t,
		gfx_key,
		"gfx:vs=1;fs=2;layouts=None;layout=PNU;instance_layout=false/PNU;topology=Triangle_List;cull=Back;front=Counter_Clockwise;blend=true/Alpha;depth=true/true;color=B8G8R8A8_SRGB;depth_format=D32_SFLOAT;pc=64;pc_stages=Vertex|Fragment",
	)

	compute_key := ir.format_pipeline_cache_key(
		{
			kind = .Compute,
			name = "particles",
			compute = {shader = 3, push_constant_size = 16, push_constant_stages = {.Compute}},
		},
	)
	defer delete(compute_key)
	testing.expect_value(t, compute_key, "compute:shader=3;layouts=None;pc=16;pc_stages=Compute")
}

@(test)
test_pipeline_cache_key_includes_descriptor_layouts :: proc(t: ^testing.T) {
	desc := ir.Pipeline_Desc {
		kind     = .Graphics,
		name     = "rect",
		graphics = ir.default_graphics_pipeline_state(),
	}
	_ = ir.pipeline_add_descriptor_layout(&desc, ir.Descriptor_Layout_Handle(1))
	_ = ir.pipeline_add_descriptor_layout(&desc, ir.Descriptor_Layout_Handle(2))

	key := ir.format_pipeline_cache_key(desc)
	defer delete(key)

	testing.expect_value(
		t,
		key,
		"gfx:vs=0;fs=0;layouts=1|2;layout=PNU;instance_layout=false/PNU;topology=Triangle_List;cull=Back;front=Counter_Clockwise;blend=true/Alpha;depth=false/false;color=B8G8R8A8_SRGB;depth_format=Undefined;pc=0;pc_stages=None",
	)
}