tweak renderer API
This commit is contained in:
parent
177e52b0d0
commit
2014c67bfd
10 changed files with 424 additions and 247 deletions
132
test_renderer.c3
132
test_renderer.c3
|
|
@ -5,18 +5,23 @@ import sdl3::sdl;
|
|||
import std::compression::qoi;
|
||||
import std::core::mem::allocator;
|
||||
|
||||
struct Viewsize @align(16) {
|
||||
int w, h;
|
||||
int ox, oy;
|
||||
}
|
||||
char[*] shader_rect_vert = $embed("resources/shaders/compiled/rect.vert.spv");
|
||||
char[*] shader_rect_frag = $embed("resources/shaders/compiled/rect.frag.spv");
|
||||
|
||||
char[*] shader_sprite_vert = $embed("resources/shaders/compiled/sprite.vert.spv");
|
||||
char[*] shader_sprite_frag = $embed("resources/shaders/compiled/sprite.frag.spv");
|
||||
|
||||
const uint WINDOW_WIDTH = 640;
|
||||
const uint WINDOW_HEIGHT = 480;
|
||||
|
||||
|
||||
fn int main()
|
||||
{
|
||||
ren::Renderer ren;
|
||||
ren.init("test window");
|
||||
ren.init("test window", WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||
|
||||
// TODO: these could be the same function
|
||||
ren.load_spirv_shader_from_file("rect shader", "resources/shaders/compiled/rect.vert.spv", "resources/shaders/compiled/rect.frag.spv", 0, 1);
|
||||
ren.load_spirv_shader_from_mem("rect shader", &shader_rect_vert, &shader_rect_frag, 0, 0);
|
||||
ren.create_pipeline("rect shader", RECT);
|
||||
|
||||
// load the tux qoi image
|
||||
|
|
@ -25,41 +30,30 @@ fn int main()
|
|||
// and put it in a texture
|
||||
ren.new_texture("tux", FULL_COLOR, img_pixels, img_desc.width, img_desc.height);
|
||||
// create a new pipeline to use the texture
|
||||
ren.load_spirv_shader_from_file("sprite shader", "resources/shaders/compiled/sprite.vert.spv", "resources/shaders/compiled/sprite.frag.spv", 1, 1);
|
||||
ren.load_spirv_shader_from_mem("sprite shader", &shader_sprite_vert, &shader_sprite_frag, 1, 0);
|
||||
ren.create_pipeline("sprite shader", SPRITE);
|
||||
|
||||
for (int i = 0; i < 20; i++) {
|
||||
GPUCommandBuffer* cmdbuf = sdl::acquire_gpu_command_buffer(ren.gpu);
|
||||
GPUTexture* swapchain_texture;
|
||||
sdl::wait_and_acquire_gpu_swapchain_texture(cmdbuf, ren.win, &swapchain_texture, null, null);
|
||||
|
||||
GPURenderPass* pass;
|
||||
GPUGraphicsPipeline* p;
|
||||
Viewsize v = {.w = 640, .h = 480};
|
||||
sdl::Event e;
|
||||
bool quit = false;
|
||||
for (usz i = 0; !quit; i++) {
|
||||
|
||||
if (sdl::poll_event(&e)) {
|
||||
if (e.type == EVENT_QUIT) {
|
||||
quit = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (i == 300) {
|
||||
io::printn("ciao!");
|
||||
img_pixels = qoi::read(allocator::temp(), "resources/tux_inv.qoi", &img_desc)!!;
|
||||
ren.update_texture("tux", img_pixels, img_desc.width, img_desc.height);
|
||||
}
|
||||
|
||||
ren.begin_render(true);
|
||||
|
||||
// Colored Rectangles Render Pass
|
||||
// FIXME: if doing damage tracking DO NOT clear the screen
|
||||
pass = sdl::begin_gpu_render_pass(cmdbuf,
|
||||
&&(GPUColorTargetInfo){
|
||||
.texture = swapchain_texture,
|
||||
.mip_level = 0,
|
||||
.layer_or_depth_plane = 0,
|
||||
.clear_color = {.r = 0.0, .g = 0.0, .b = 0.0, .a = 1.0},
|
||||
.load_op = GPU_LOADOP_CLEAR, // clear the screen at the start of the render pass
|
||||
.store_op = GPU_STOREOP_STORE,
|
||||
.resolve_texture = null,
|
||||
.resolve_mip_level = 0,
|
||||
.resolve_layer = 0,
|
||||
.cycle = false,
|
||||
.cycle_resolve_texture = false
|
||||
},
|
||||
1,
|
||||
null // huh
|
||||
);
|
||||
|
||||
if (pass == null) {
|
||||
unreachable("render pass creation went wrong: %s", sdl::get_error());
|
||||
}
|
||||
ren.start_render_pass("rect shader");
|
||||
|
||||
// rect 1
|
||||
ren.push_quad(100,100,100,100,0xff00ff00, 20);
|
||||
|
|
@ -68,75 +62,31 @@ fn int main()
|
|||
// rect 3
|
||||
ren.push_quad(200,300,50,50,0xffff0000);
|
||||
|
||||
p = ren.pipelines.get_from_name("rect shader").pipeline;
|
||||
if (p == null) {
|
||||
unreachable("no pipeline");
|
||||
}
|
||||
//ren.set_scissor(0,50,200,300);
|
||||
|
||||
sdl::bind_gpu_graphics_pipeline(pass, p);
|
||||
ren.draw_quads();
|
||||
|
||||
v.ox = 10*i;
|
||||
v.oy = 10*i;
|
||||
sdl::push_gpu_vertex_uniform_data(cmdbuf, 1, &v, Viewsize.sizeof);
|
||||
|
||||
ren.draw_quads(pass);
|
||||
|
||||
sdl::end_gpu_render_pass(pass);
|
||||
ren.end_render_pass();
|
||||
// End Rectangle Render Pass
|
||||
|
||||
|
||||
// Textured Rectangles Render Pass
|
||||
pass = sdl::begin_gpu_render_pass(cmdbuf,
|
||||
&&(GPUColorTargetInfo){
|
||||
.texture = swapchain_texture,
|
||||
.mip_level = 0,
|
||||
.layer_or_depth_plane = 0,
|
||||
.clear_color = {.r = 0.0, .g = 0.0, .b = 0.0, .a = 1.0},
|
||||
.load_op = GPU_LOADOP_DONT_CARE, // clear the screen at the start of the render pass
|
||||
.store_op = GPU_STOREOP_STORE,
|
||||
.resolve_texture = null,
|
||||
.resolve_mip_level = 0,
|
||||
.resolve_layer = 0,
|
||||
.cycle = false,
|
||||
.cycle_resolve_texture = false
|
||||
},
|
||||
1,
|
||||
null // huh
|
||||
);
|
||||
|
||||
if (pass == null) {
|
||||
unreachable("render pass creation went wrong: %s", sdl::get_error());
|
||||
}
|
||||
|
||||
p = ren.pipelines.get_from_name("sprite shader").pipeline;
|
||||
if (p == null) {
|
||||
unreachable("no pipeline");
|
||||
}
|
||||
|
||||
sdl::bind_gpu_graphics_pipeline(pass, p);
|
||||
// in this case it is not an offset but the texture size in pixels
|
||||
v.ox = 54;
|
||||
v.oy = 64;
|
||||
sdl::push_gpu_vertex_uniform_data(cmdbuf, 1, &v, Viewsize.sizeof);
|
||||
|
||||
ren.start_render_pass("sprite shader");
|
||||
|
||||
// bind the pipeline's sampler
|
||||
ren::Texture* tx = ren.textures.get_from_name("tux");
|
||||
sdl::bind_gpu_fragment_samplers(pass, 0,
|
||||
(GPUTextureSamplerBinding[]){{.texture = tx.texture, .sampler = tx.sampler}}, 1
|
||||
);
|
||||
ren.bind_texture("tux");
|
||||
|
||||
// tux
|
||||
ren.push_sprite(300, 0, 54, 64, 0, 0);
|
||||
|
||||
ren.draw_quads(pass);
|
||||
ren.reset_scissor();
|
||||
|
||||
sdl::end_gpu_render_pass(pass);
|
||||
ren.draw_quads();
|
||||
|
||||
ren.end_render_pass();
|
||||
// End Textured Rectangle Render Pass
|
||||
|
||||
sdl::submit_gpu_command_buffer(cmdbuf);
|
||||
|
||||
ren.reset_quads();
|
||||
thread::sleep_ms(250);
|
||||
ren.end_render();
|
||||
}
|
||||
|
||||
ren.free();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue