~rycwo/forge

ref: HEAD forge/src/gui_opengl_46.c -rw-r--r-- 7.4 KiB
d7ee94d6Ryan Chan Fix missing cd in .build.yml 8 days ago
                                                                                
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// This file is part of Forge, the foundation library for Forge tools.
//
// Copyright (C) 2021 Ryan Chan <rycwo@posteo.net>
// SPDX-License-Identifier: GPL-3.0-only
//
// This Source Code Form is subject to the terms of the GNU General Public
// License v3.0 only. You should have received a copy of the license along with
// this program. If not, see <https://www.gnu.org/licenses/>.

#include "forge/gui_opengl_46.h"

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <GL/glew.h>

#include "forge/allocator.h"
#include "forge/gui_input.h"
#include "forge/type.h"

struct opengl_prim {
	GLuint program;
	GLuint uniform;
	GLuint prims;
};

struct opengl_resources {
	GLuint vert_array; // Dummy. A VAO is necessary to call glDrawArrays.
	GLuint clips;
	struct opengl_prim rects;
};

static struct opengl_resources resources_g;

struct shader_context {
	float projection[16];
	float viewport[2];
};

struct shader_result {
	bool ok;
	char* log;
};

static struct shader_result
check_shader(GLuint shader, struct fg_allocator const* alloc) {
	struct shader_result result;
	memset(&result, 0, sizeof(struct shader_result));

	GLint ok;
	glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
	result.ok = (bool)ok;

	if (ok != GL_TRUE) {
		// Note the returned log length includes the null terminator
		GLint log_length;
		glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length);

		result.log = (char*)alloc->alloc(log_length);
		glGetShaderInfoLog(shader, log_length, NULL, result.log);
	}
	return result;
}

struct shader_result
check_program(GLuint program, struct fg_allocator const* alloc) {
	struct shader_result result;
	memset(&result, 0, sizeof(struct shader_result));

	GLint ok;
	glGetProgramiv(program, GL_LINK_STATUS, &ok);
	result.ok = (bool)ok;

	if (ok != GL_TRUE) {
		// Note the returned log length includes the null terminator
		GLint log_length;
		glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_length);

		result.log = (char*)alloc->alloc(log_length);
		glGetProgramInfoLog(program, log_length, NULL, result.log);
	}
	return result;
}

static GLuint
load_shader(
		uint32_t const* source,
		size_t size,
		GLenum type,
		struct fg_allocator const* alloc) {
	GLuint shader = glCreateShader(type);
	glShaderBinary(
			1, &shader,
			GL_SHADER_BINARY_FORMAT_SPIR_V,
			(void const*)source,
			size);
	glSpecializeShader(shader, "main", 0, NULL, NULL);

	struct shader_result result = check_shader(shader, alloc);
	if (!result.ok) {
		fprintf(stderr, "Error compiling shader:\n%s", result.log);
		alloc->free(result.log);
	}

	return shader;
}

static GLuint
load_rect_program(struct fg_allocator const* alloc) {
	// Pre-compiled SPIR-V from glslangValidator. See meson.build.
	static uint32_t const vert_spirv[] = {
		#include "gui_rect.vert.glsl.spv"
	};
	static uint32_t const frag_spirv[] = {
		#include "gui_rect.frag.glsl.spv"
	};

	GLuint vert_shader = load_shader(
			vert_spirv, sizeof(vert_spirv),
			GL_VERTEX_SHADER,
			alloc);
	GLuint frag_shader = load_shader(
			frag_spirv, sizeof(frag_spirv),
			GL_FRAGMENT_SHADER,
			alloc);

	GLuint program = glCreateProgram();
	glAttachShader(program, vert_shader);
	glAttachShader(program, frag_shader);
	glLinkProgram(program);

	struct shader_result result = check_program(program, alloc);
	if (!result.ok) {
		fprintf(stderr, "Error linking program:\n%s", result.log);
		alloc->free(result.log);
	}

	glDetachShader(program, vert_shader);
	glDetachShader(program, frag_shader);
	glDeleteShader(vert_shader);
	glDeleteShader(frag_shader);
	return program;
}

static GLuint
load_text_program(struct fg_allocator const* alloc) {
	return 0; // TODO
}

static GLuint
load_line_program(struct fg_allocator const* alloc) {
	return 0; // TODO
}

static inline void
orthographic(float out[16], float l, float r, float b, float t) {
	float w = r - l;
	float h = t - b;

	// Assuming our vertices are expressed in window-space coords,
	// i.e. top-left corner is the origin, then we include a term to
	// flip the y coordinate so that it can be performed all in a single
	// transformation.

	out[0] = 2.0 / w;
	out[1] = 0.0;
	out[2] = 0.0;
	out[3] = 0.0;

	out[4] = 0.0;
	out[5] = -2.0 / h;
	out[6] = 0.0;
	out[7] = 0.0;

	out[8] = 0.0;
	out[9] = 0.0;
	out[10] = 1.0;
	out[11] = 0.0;

	out[12] = -(r + l) / w;
	out[13] = ((2.0 * h) - (t + b)) / h;
	out[14] = 0.0;
	out[15] = 1.0;
}

static void
draw_rects(struct fg_gui_context* context) {
	int const count = context->rects.size_front + context->rects.size_back;
	if (count <= 0)
		return;

	GLuint const buffers[] = {
		resources_g.clips,
		resources_g.rects.prims,
	};
	glBindBuffersBase(
			GL_SHADER_STORAGE_BUFFER,
			0, sizeof(buffers) / sizeof(GLuint),
			&buffers[0]);

	glBindBufferBase(GL_UNIFORM_BUFFER, 0, resources_g.rects.uniform);

	glUseProgram(resources_g.rects.program);
	// Assumes a dummy vertex array has been bound
	glDrawArrays(GL_TRIANGLES, 0, count * 6);
}

void
fg_init_gui_opengl_46(struct fg_gui_context* context) {
	resources_g.rects.program = load_rect_program(&context->alloc);

	glCreateVertexArrays(1, &resources_g.vert_array);

	glCreateBuffers(1, &resources_g.clips);
	glNamedBufferData(
			resources_g.clips,
			sizeof(float) * 4 * context->clips.capacity,
			NULL, GL_STREAM_DRAW);

	glCreateBuffers(1, &resources_g.rects.uniform);
	glNamedBufferData(
			resources_g.rects.uniform,
			sizeof(struct shader_context),
			NULL, GL_STREAM_DRAW);

	glCreateBuffers(1, &resources_g.rects.prims);
	glNamedBufferData(
			resources_g.rects.prims,
			sizeof(struct fg_gui_rect) * context->rects.capacity,
			NULL, GL_STREAM_DRAW);
}

void
fg_term_gui_opengl_46(void) {
	glDeleteProgram(resources_g.rects.program);
	glDeleteBuffers(1, &resources_g.clips);
	glDeleteBuffers(1, &resources_g.rects.uniform);
	glDeleteBuffers(1, &resources_g.rects.prims);
}

void
fg_commit_gui_opengl_46(struct fg_gui_context* context) {
	glNamedBufferSubData(
			resources_g.clips,
			0, sizeof(float) * 4 * context->clips.size,
			(void const*)context->clips.buf);

	{
		int const count = context->rects.size_front + context->rects.size_back;
		glNamedBufferSubData(
				resources_g.rects.prims,
				0, sizeof(struct fg_gui_rect) * count,
				(void const*)context->rects.prims);
	}
}

void
fg_draw_gui_opengl_46(struct fg_gui_context* context) {
	// Compress quad data into a single fg_gui_rect and expand vertex positions
	// and other vertex attributes in the vertex shader (which at this point is
	// a glorified "parallel for" loop). Doing this in OpenGL *may* circumvent
	// the post-transform cache, which could negatively affect performance.
	// https://www.khronos.org/opengl/wiki/Post_Transform_Cache

	// Bind dummy vertex array to pass glDrawArrays() checks
	glBindVertexArray(resources_g.vert_array);

	fg_vec2 viewport_size;
	fg_gui_viewport_size(context, viewport_size);

	glViewport(0, 0, (GLsizei)viewport_size[0], (GLsizei)viewport_size[1]);

	struct shader_context shd_context;
	orthographic(
			shd_context.projection,
			0.0, (float)viewport_size[0],
			0.0, (float)viewport_size[1]);
	shd_context.viewport[0] = (float)viewport_size[0];
	shd_context.viewport[1] = (float)viewport_size[1];

	glNamedBufferSubData(
			resources_g.rects.uniform,
			0, sizeof(struct shader_context),
			(void const*)&shd_context);

	glDepthMask(GL_FALSE);  // Disable depth writes

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glBlendEquation(GL_FUNC_ADD);

	draw_rects(context);

	glBindVertexArray(0);
	glUseProgram(0);
}