summaryrefslogtreecommitdiff
path: root/c2.cpp
blob: 37e487686bfd84481fb0af9e0588020e7f4a9eba (plain) (blame)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "app.hpp"
#include "video.hpp"
extern "C" {
#include "plat.h"
#include "memory.h"
}
#include <string.h>

#define video_arena_size (1024 * 1024 * 16)
#define asset_arena_size (1024 * 1024 * 4)
#define per_frame_memory_size (1024 * 1024)

static float verts[] = {
	 0.5f,  0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
	-0.5f,  0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
	 0.0f, -0.5f, 0.0f, 0.0f, 1.0f, 0.5f, 0.0f
};

struct C2 : public App {
	Device* dev;
	void on_resize() override {
		dev->on_resize();
	}
};

static Buffer_Id upload_verts(Device* dev) {
	Buffer_Id stage = dev->create_buffer(
		sizeof verts,
		Buffer_Flags::copy_src |
		Buffer_Flags::cpu_readwrite
	);
	Buffer_Id vbo = dev->create_buffer(
		sizeof verts,
		Buffer_Flags::vertex_buffer |
		Buffer_Flags::copy_dst
	);
	{
		void* mem;
		mem = dev->map_buffer(stage, 0, sizeof verts);
		memcpy(mem, verts, sizeof verts);
		dev->unmap_buffer(stage);
	}
	{
		Context& ctx = dev->acquire();
		ctx.copy(vbo, stage);
		dev->submit(ctx);
	}
	dev->destroy_buffer(stage);
	return vbo;
}

static Sampler_Id create_clamped_linear(Device* dev) {
	Sampler_State s{};
	s.min = Filter_Mode::linear;
	s.mag = Filter_Mode::linear;
	s.address_u = Address_Mode::repeat;
	s.address_v = Address_Mode::repeat;
	return dev->create_sampler(s);
}

int main() {
	Arena video_arena;
	Arena asset_arena;
	Asset_Arena assets;
	Device* dev;
	Shader* shader;
	Texture* texture;
	Buffer_Id vbo;
	Sampler_Id clamped_linear;
	C2* app = App::create<C2>("c2");
	void* per_frame;
	app->running = 1;
	init_arena(
		&video_arena,
		arena_alloc(app->arena, video_arena_size),
		video_arena_size
	);
	init_arena(
		&asset_arena,
		arena_alloc(app->arena, asset_arena_size),
		asset_arena_size
	);
	assets.init(&asset_arena, "pack");
	dev = Device::create(&video_arena, app); 
	app->dev = dev;
	shader = (Shader*)assets.load("triangle.csh");
	texture = (Texture*)assets.load("22.tex");
	per_frame = heap_alloc(
		dev->heap,
		per_frame_memory_size
	);
	clamped_linear = create_clamped_linear(dev);
	assert(per_frame != 0);
	uint8_t r = 0;
	vbo = upload_verts(dev);
	while (app->running) {
		Arena frame_arena;
		init_arena(&frame_arena, per_frame, per_frame_memory_size);

		app->begin();
		dev->begin_frame();

		Pipeline_Builder pb(&frame_arena);
		pb.begin_rp();
		pb.rp_target(dev->get_backbuffer(), { r, 0x00, 0xff, 0xff });
		Render_Pass& pass = pb.build_rp();

		pb.begin();
		pb.shader(shader->id);
		pb.vertex_format(shader->vf);
		pb.texture(
			shader->descriptor_index("colour_texture"),
			texture->id,
			clamped_linear
		);
		Pipeline& pip = pb.build();

		Vertex_Buffer_Binding binding[] = {{
			.id = vbo,
			.offset = 0,
			.target = shader->binding_index("verts")
		}, {}};

		Draw draw{};
		draw.verts = binding;
		draw.vertex_count = 3;
		draw.instance_count = 1;

		dev->get_ctx().submit(draw, pip, pass);
	/*	dev->get_ctx().submit(*dev, pass);*/

		r += 10;
		dev->present();
		app->end();
	}
	assets.destroy();
	dev->destroy_sampler(clamped_linear);
	dev->destroy_buffer(vbo);
	dev->destroy();
	app->destroy();
}