-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathtriangle.cpp
More file actions
75 lines (63 loc) · 2.54 KB
/
Copy pathtriangle.cpp
File metadata and controls
75 lines (63 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "example_support.hpp"
#include <stdio.h>
#include <stdlib.h>
using namespace gpu;
int main()
{
constexpr uint32 width = 512;
constexpr uint32 height = 512;
void* window = open_example_window("NoGraphicsAPI triangle", width, height);
Device* device = create_device({.window = window, .swapchain_format = Format::bgra8_srgb}).device;
if (!window || !device)
{
destroy_device(device);
close_example_window(window);
return 1;
}
printf("Using %s\n", get_device_caps(device).device_name);
const Span<byte> vertex_code = read_shader(NOGRAPHICSAPI_VERTEX_SHADER_PATH);
const Span<byte> fragment_code = read_shader(NOGRAPHICSAPI_FRAGMENT_SHADER_PATH);
PSO* triangle_pso = create_graphics_pso(device, {
.vertex = {.code = {vertex_code.data, vertex_code.size}, .entry_point = "vertexMain"},
.fragment = {.code = {fragment_code.data, fragment_code.size}, .entry_point = "fragmentMain"},
.color_targets = { { .format = Format::bgra8_srgb } }
});
free(fragment_code.data);
free(vertex_code.data);
if (!triangle_pso)
{
destroy_device(device);
close_example_window(window);
return 1;
}
TimelinePoint latest_completion{ .semaphore = create_timeline_semaphore(device) };
CommandPool* command_pools[] = {create_command_pool(device), create_command_pool(device)};
while (pump_example_window(window))
{
if (latest_completion.value >= 2)
wait_timeline({.semaphore = latest_completion.semaphore, .value = latest_completion.value - 1});
CommandPool* command_pool = command_pools[latest_completion.value % 2];
reset_command_pool(command_pool);
CommandBuffer* commands = begin_commands(command_pool);
const SwapchainFrame frame = acquire(commands);
if (!frame.render_view)
continue;
begin_render_pass(commands, {
.colors = { { .render_view = frame.render_view, .load = LoadOp::clear } },
});
bind_pso(commands, triangle_pso);
draw(commands, {}, 3);
end_render_pass(commands);
end_commands(commands);
latest_completion.value++;
submit_and_present(device, {.commands = {commands}, .completion = latest_completion});
}
wait_idle(device);
destroy_command_pool(command_pools[1]);
destroy_command_pool(command_pools[0]);
destroy_timeline_semaphore(latest_completion.semaphore);
destroy_pso(triangle_pso);
destroy_device(device);
close_example_window(window);
return 0;
}