summaryrefslogtreecommitdiff
path: root/pipeline.cpp
blob: 88b2009e1a061387382b07bb130b702437032281 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "video.hpp"

#include <string.h>

extern "C" {
#include "memory.h"
#include "plat.h"
#include "str.h"
}

Pipeline_Builder::Pipeline_Builder(Arena* arena):
	arena(arena) {}

void Pipeline_Builder::begin_rp() {
	pass = (Render_Pass*)arena_alloc(arena, sizeof *pass);
	pass->target = 0;
	pass->clear = { 0, 0, 0, 0 };
}

void Pipeline_Builder::rp_target(Texture_Id id, Colour clear) {
	pass->target = id;
	pass->clear = clear;
	pass->mode = Clear_Mode::clear;
}

void Pipeline_Builder::rp_target(Texture_Id id, Clear_Mode clear) {
	pass->target = id;
	assert(clear != Clear_Mode::clear);
	pass->mode = clear;
}

void Pipeline_Builder::validate_rp() {
	assert(pass->target);
}

Render_Pass& Pipeline_Builder::build_rp() {
	validate_rp();
	return *pass;
}

void Pipeline_Builder::begin(Device* dev) {
	pip = (Pipeline*)arena_alloc(arena, sizeof *pip);
	zero(pip, sizeof *pip);
	if (dev) {
		Texture_Id backbuffer_id = dev->get_backbuffer();
		Texture& backbuffer = dev->get_texture(backbuffer_id);
		pip->scissor[0] = 0;
		pip->scissor[1] = 0;
		pip->scissor[2] = backbuffer.w;
		pip->scissor[3] = backbuffer.h;
		pip->viewport[0] = 0;
		pip->viewport[1] = 0;
		pip->viewport[2] = backbuffer.w;
		pip->viewport[3] = backbuffer.h;
	}
}

void Pipeline_Builder::viewport(
	float x,
	float y,
	float w,
	float h
) {
	pip->viewport[0] = x;
	pip->viewport[1] = y;
	pip->viewport[2] = w;
	pip->viewport[3] = h;
}

void Pipeline_Builder::scissor(
	float x,
	float y,
	float w,
	float h
) {
	pip->scissor[0] = x;
	pip->scissor[1] = y;
	pip->scissor[2] = w;
	pip->scissor[3] = h;
}

void Pipeline_Builder::shader(Shader_Id s) {
	pip->shader = s;
}

void Pipeline_Builder::texture(
	int binding,
	Texture_Id t,
	Sampler_Id s
) {
	Descriptor* d;
	Texture_Descriptor* td;
	assert(pip->descriptor_count < pipeline_max_descriptors);
	d = &pip->descriptors[pip->descriptor_count++];
	td = (Texture_Descriptor*)d->payload;
	d->slot = binding;
	d->type = Descriptor::Type::texture;
	td->sampler = s;
	td->texture = t;
}

void Pipeline_Builder::cbuffer(int binding, Buffer_Id id) {
	Descriptor* d;
	Constant_Buffer_Descriptor* cd;
	assert(pip->descriptor_count < pipeline_max_descriptors);
	d = &pip->descriptors[pip->descriptor_count++];
	cd = (Constant_Buffer_Descriptor*)d->payload;
	d->slot = binding;
	d->type = Descriptor::Type::constant_buffer;
	cd->buffer = id;
}

void Pipeline_Builder::vertex_format(Vertex_Format_Id vf) {
	pip->vertex_format = vf;
}

Pipeline& Pipeline_Builder::build() {
#define h(n, v) \
	n = fnv1a64_2(n, (uint8_t*)&v, sizeof v)
	validate();
	pip->pipeline_hash = fnv1a64(0, 0);
	h(pip->pipeline_hash, pip->vertex_format);
	h(pip->pipeline_hash, pip->shader);
	h(pip->pipeline_hash, pip->descriptor_count);
	{
		int i, e = pip->descriptor_count;
		pip->descriptor_resource_hash = fnv1a64(0, 0);
		for (i = 0; i < e; i++) {
			Descriptor* d = &pip->descriptors[i];
			h(pip->pipeline_hash, d->type);
			h(pip->pipeline_hash, d->slot);
			h(pip->descriptor_resource_hash, d->type);
			h(pip->descriptor_resource_hash, d->slot);
			switch (d->type) {
				case Descriptor::Type::texture: {
					auto td = (Texture_Descriptor*)d->payload;
					h(pip->descriptor_resource_hash, td->sampler);
					h(pip->descriptor_resource_hash, td->texture);
				} break;
				case Descriptor::Type::constant_buffer: {
					auto cd = (Constant_Buffer_Descriptor*)d->payload;
					h(pip->descriptor_resource_hash, cd->buffer);
				} break;
			}
		}
	}
	return *pip;
#undef h
}

void Pipeline_Builder::validate() {
	assert(pip->vertex_format);
	assert(pip->shader);
}