aboutsummaryrefslogtreecommitdiffstats
path: root/lib/fosphor/gl_cmap.c
blob: cf347ac03c00cfd75c862bf78fe332079a819ae0 (plain)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
 * gl_cmap.c
 *
 * OpenGL float texture -> color mapping
 *
 * Copyright (C) 2013-2014 Sylvain Munaut
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*! \addtogroup gl/cmap
 *  @{
 */

/*! \file gl_cmap.c
 *  \brief OpenGL float texture to color mapping
 */

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "gl_platform.h"

#include "gl_cmap.h"
#include "resource.h"


struct gl_cmap_shader
{
	/* Status */
	int loaded;

	/* Shader handles */
	GLuint prog;
	GLuint shader;

	/* Uniforms */
	GLint u_tex;
	GLint u_palette;
	GLint u_range;
};

enum gl_cmap_shader_type
{
	GL_CMAP_SHADER_SIMPLE,
	GL_CMAP_SHADER_BICUBIC,
	GL_CMAP_SHADER_FALLBACK,
	_GL_CMAP_SHADER_NUM
};

struct fosphor_gl_cmap_ctx {
	struct gl_cmap_shader shaders[_GL_CMAP_SHADER_NUM];
};


/* -------------------------------------------------------------------------- */
/* Helpers / Internal API                                                     */
/* -------------------------------------------------------------------------- */

static int
gl_cmap_init_shader(struct gl_cmap_shader *shader, const char *name)
{
	const char *shader_src;
	GLint buf_len, orv;

	/* Load shader sources */
	shader_src = resource_get(name, NULL);
	if (!shader_src)
		return -ENOENT;

	/* Allocate shader */
	shader->prog = glCreateProgram();
	shader->shader = glCreateShader(GL_FRAGMENT_SHADER);

	/* Compile / Link / Attach */
	glShaderSource(shader->shader, 1, (const char **)&shader_src, NULL);
	glCompileShader(shader->shader);

	/* Check success and compile log */
	glGetShaderiv(shader->shader, GL_COMPILE_STATUS, &orv);
	glGetShaderiv(shader->shader, GL_INFO_LOG_LENGTH, &buf_len);

#if 1
	if ((buf_len > 0) && (orv != GL_TRUE))
#else
	if (buf_len > 0)
#endif
	{
		char *buf = malloc(buf_len+1);

		glGetShaderInfoLog(shader->shader, buf_len, 0, buf);
		buf[buf_len] = '\0';

		fprintf(stderr, "[!] gl_cmap shader compile log :\n%s\n", buf);

		free(buf);
	}

	if (orv != GL_TRUE) {
		fprintf(stderr, "[!] gl_cmap shader compilation failed (%s)\n", name);
		return -EINVAL;
	}

	/* Attach to program */
	glAttachShader(shader->prog, shader->shader);
	glLinkProgram(shader->prog);

	/* Grab the uniform locations */
	shader->u_tex     = glGetUniformLocation(shader->prog, "tex");
	shader->u_palette = glGetUniformLocation(shader->prog, "palette");
	shader->u_range   = glGetUniformLocation(shader->prog, "range");

	/* Success */
	shader->loaded = 1;

	return 0;
}

static void
gl_cmap_release_shader(struct gl_cmap_shader *shader)
{
	if (!shader->loaded)
		return;

	glDetachShader(shader->prog, shader->shader);
	glDeleteShader(shader->shader);
	glDeleteProgram(shader->prog);

	memset(shader, 0x00, sizeof(struct gl_cmap_shader));
}


/* -------------------------------------------------------------------------- */
/* Exposed API                                                                */
/* -------------------------------------------------------------------------- */

struct fosphor_gl_cmap_ctx *
fosphor_gl_cmap_init(void)
{
	struct fosphor_gl_cmap_ctx *cmap_ctx;
	int rv;
	int need_fallback = 0;

	/* Allocate structure */
	cmap_ctx = malloc(sizeof(struct fosphor_gl_cmap_ctx));
	if (!cmap_ctx)
		return NULL;

	memset(cmap_ctx, 0, sizeof(struct fosphor_gl_cmap_ctx));

	/* Init shaders */
	rv = gl_cmap_init_shader(
		&cmap_ctx->shaders[GL_CMAP_SHADER_SIMPLE],
		"cmap_simple.glsl"
	);
	if (rv) {
		fprintf(stderr, "[w] Color map shader 'simple' failed to load, will use fallback\n");
		need_fallback = 1;
	}

	rv = gl_cmap_init_shader(
		&cmap_ctx->shaders[GL_CMAP_SHADER_BICUBIC],
		"cmap_bicubic.glsl"
	);
	if (rv) {
		fprintf(stderr, "[w] Color map shader 'bicubic' failed to load, will use fallback\n");
		need_fallback = 1;
	}

	if (need_fallback) {
		rv = gl_cmap_init_shader(
			&cmap_ctx->shaders[GL_CMAP_SHADER_FALLBACK],
			"cmap_fallback.glsl"
		);
		if (rv) {
			fprintf(stderr, "[!] Color map shader 'fallback' failed, aborting\n");
			goto error;
		}
	}

	/* All done */
	return cmap_ctx;

	/* Error path */
error:
	fosphor_gl_cmap_release(cmap_ctx);

	return NULL;
}

void
fosphor_gl_cmap_release(struct fosphor_gl_cmap_ctx *cmap_ctx)
{
	/* Safety */
	if (!cmap_ctx)
		return;

	/* Disable program */
	glUseProgram(0);

	/* Release shaders */
	gl_cmap_release_shader(&cmap_ctx->shaders[GL_CMAP_SHADER_SIMPLE]);
	gl_cmap_release_shader(&cmap_ctx->shaders[GL_CMAP_SHADER_BICUBIC]);
	gl_cmap_release_shader(&cmap_ctx->shaders[GL_CMAP_SHADER_FALLBACK]);
}


void
fosphor_gl_cmap_enable(struct fosphor_gl_cmap_ctx *cmap_ctx,
                       GLuint tex_id, GLuint cmap_id,
                       float scale, float offset,
                       enum fosphor_gl_cmap_mode mode)
{
	struct gl_cmap_shader *shader;
	float range[2];
	int fmode;

	shader = &cmap_ctx->shaders[
		mode == GL_CMAP_MODE_BICUBIC ?
			GL_CMAP_SHADER_BICUBIC :
			GL_CMAP_SHADER_SIMPLE
	];

	if (!shader->loaded)
		shader = &cmap_ctx->shaders[GL_CMAP_SHADER_FALLBACK];

	/* Enable program */
	glUseProgram(shader->prog);

	/* Texture unit 0: Main texture */
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, tex_id);
	glUniform1i(shader->u_tex, 0);

	fmode = (mode == GL_CMAP_MODE_NEAREST) ? GL_NEAREST : GL_LINEAR;
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, fmode);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, fmode);

	/* Texture unit 1: Palette 1D texture */
	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_1D, cmap_id);
	glUniform1i(shader->u_palette, 1);

	/* Range definition */
	range[0] = scale;
	range[1] = offset;
	glUniform2fv(shader->u_range, 1, range);
}

void
fosphor_gl_cmap_disable(void)
{
	/* Default to texture unit 0 */
	glActiveTexture(GL_TEXTURE0);

	/* Disable program */
	glUseProgram(0);
}

void
fosphor_gl_cmap_draw_scale(GLuint cmap_id,
                           float x0, float x1, float y0, float y1)
{
	/* Enable texture-1D */
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_1D, cmap_id);
	glEnable(GL_TEXTURE_1D);

	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

	/* Draw QUAD */
	glBegin( GL_QUADS );
	glTexCoord1f(0.0f); glVertex2f(x0, y0);
	glTexCoord1f(0.0f); glVertex2f(x1, y0);
	glTexCoord1f(1.0f); glVertex2f(x1, y1);
	glTexCoord1f(1.0f); glVertex2f(x0, y1);
	glEnd();

	/* Disable texturing */
	glDisable(GL_TEXTURE_1D);
}


int
fosphor_gl_cmap_generate(GLuint *cmap_id, gl_cmap_gen_func_t gfn, void *gfn_arg, int N)
{
	uint32_t *rgba;

	/* Temp array for data */
	rgba = malloc(sizeof(uint32_t) * N);
	if (!rgba)
		return -1;

	/* Allocate texture ID */
	glGenTextures(1, cmap_id);

	/* Select it */
	glBindTexture(GL_TEXTURE_1D, *cmap_id);

	/* Generate texture data */
	gfn(rgba, N, gfn_arg);

	/* Upload data */
	glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, N, 0,
		GL_RGBA, GL_UNSIGNED_BYTE, rgba);

	/* Configure texture behavior */
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);

	/* Release temp array */
	free(rgba);

	return 0;
}

/*! @} */