Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gpu_basic.c « intern « gpu « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6169e8daf114664e7f576434536da9b5d10c1895 (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
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * 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 2
 * 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, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2013 Blender Foundation.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): Brecht Van Lommel, Jason Wilkins.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file source/blender/gpu/intern/gpu_basic.c
 *  \ingroup gpu
 */

/* GLSL shaders to replace fixed function OpenGL materials and lighting. These
 * are deprecated in newer OpenGL versions and missing in OpenGL ES 2.0. Also,
 * two sided lighting is no longer natively supported on NVidia cards which
 * results in slow software fallback.
 *
 * Todo:
 * x Replace glLight and glMaterial functions entirely with GLSL uniforms, to make OpenGL ES 2.0 work.
 * x Replace glTexCoord and glColor with generic attributes.
 * x Optimize for case where fewer than 3 or 8 lights are used.
 * - Optimize for case where specular is not used.
 * - Optimize for case where no texture matrix is used.
 */

#if WITH_GL_PROFILE_COMPAT
#define GPU_MANGLE_DEPRECATED 0 /* Allow use of deprecated OpenGL functions in this file */
#endif

/* my interface */
#include "intern/gpu_basic_intern.h"

/* my library */
#include "GPU_extensions.h"
#include "GPU_safety.h"
#include "GPU_state_latch.h"

/* internal */
#include "intern/gpu_clipping_intern.h"
#include "intern/gpu_common_intern.h"
#include "intern/gpu_lighting_intern.h"
#include "intern/gpu_matrix_intern.h"

/* external */

#include "BLI_math.h"
#include "BLI_dynstr.h"

#include "MEM_guardedalloc.h"

/* standard */
#include <string.h>



/* State */

static struct BASIC_SHADER {
	uint32_t options;

	GPUShader*   gpushader[GPU_BASIC_OPTION_COMBINATIONS];
	bool         failed   [GPU_BASIC_OPTION_COMBINATIONS];
	GPUcommon    common   [GPU_BASIC_OPTION_COMBINATIONS];

} BASIC_SHADER;



/* Init / exit */

void gpu_basic_init(void)
{
	memset(&BASIC_SHADER, 0, sizeof(BASIC_SHADER));

#if 0
	{ 	/* for testing purposes, mark all shaders as failed*/
		int i;
		for (i = 0; i < GPU_BASIC_OPTION_COMBINATIONS; i++)
			BASIC_SHADER.failed[i] = true;
	}
#endif
}



void gpu_basic_exit(void)
{
	int i;

	for (i = 0; i < GPU_BASIC_OPTION_COMBINATIONS; i++)
		if (BASIC_SHADER.gpushader[i] != NULL)
			GPU_shader_free(BASIC_SHADER.gpushader[i]);
}



/* Shader feature enable/disable */

void gpu_basic_enable(uint32_t options)
{
	GPU_ASSERT(!(options & GPU_BASIC_FAST_LIGHTING));

	BASIC_SHADER.options |= options;
}



void gpu_basic_disable(uint32_t options)
{
	BASIC_SHADER.options &= ~options;
}



/* Shader lookup / create */

static uint32_t tweak_options(void)
{
	uint32_t options;

	options = BASIC_SHADER.options;

	/* detect if we can do faster lighting for solid draw mode */
	if (  BASIC_SHADER.options & GPU_BASIC_LIGHTING      &&
		!(BASIC_SHADER.options & GPU_BASIC_LOCAL_VIEWER) &&
		gpu_lighting_is_fast())
	{
		options |= GPU_BASIC_FAST_LIGHTING;
	}

	if (gpuGetTextureBinding2D() == 0)
		options &= ~GPU_BASIC_TEXTURE_2D;

	return options;
}



static void basic_shader_bind(void)
{
	/* glsl code */
	extern const char datatoc_gpu_shader_basic_vert_glsl[];
	extern const char datatoc_gpu_shader_basic_frag_glsl[];

	const uint32_t tweaked_options = tweak_options();

	/* create shader if it doesn't exist yet */
	if (BASIC_SHADER.gpushader[tweaked_options] != NULL) {
		GPU_shader_bind(BASIC_SHADER.gpushader[tweaked_options]);
		gpu_set_common(BASIC_SHADER.common + tweaked_options);
	}
	else if (!BASIC_SHADER.failed[tweaked_options]) {
		DynStr* vert = BLI_dynstr_new();
		DynStr* frag = BLI_dynstr_new();
		DynStr* defs = BLI_dynstr_new();

		char* vert_cstring;
		char* frag_cstring;
		char* defs_cstring;

		char nickname[20];

		gpu_include_common_vert(vert);
		BLI_dynstr_append(vert, datatoc_gpu_shader_basic_vert_glsl);

		gpu_include_common_frag(frag);
		BLI_dynstr_append(frag, datatoc_gpu_shader_basic_frag_glsl);

		gpu_include_common_defs(defs);

		if (tweaked_options & GPU_BASIC_TWO_SIDE)
			BLI_dynstr_append(defs, "#define USE_TWO_SIDE\n");

		if (tweaked_options & GPU_BASIC_TEXTURE_2D)
			BLI_dynstr_append(defs, "#define USE_TEXTURE_2D\n");

		if (tweaked_options & GPU_BASIC_LOCAL_VIEWER)
			BLI_dynstr_append(defs, "#define USE_LOCAL_VIEWER\n");

		if (tweaked_options & GPU_BASIC_SMOOTH)
			BLI_dynstr_append(defs, "#define USE_SMOOTH\n");

		if (tweaked_options & GPU_BASIC_LIGHTING) {
			BLI_dynstr_append(defs, "#define USE_LIGHTING\n");
			BLI_dynstr_append(defs, "#define USE_SPECULAR\n");

			if (tweaked_options & GPU_BASIC_FAST_LIGHTING)
				BLI_dynstr_append(defs, "#define USE_FAST_LIGHTING\n");
		}

		vert_cstring = BLI_dynstr_get_cstring(vert);
		frag_cstring = BLI_dynstr_get_cstring(frag);
		defs_cstring = BLI_dynstr_get_cstring(defs);

		sprintf(nickname, "Basic[0x%04X]", tweaked_options);

		BASIC_SHADER.gpushader[tweaked_options] =
			GPU_shader_create(nickname, vert_cstring, frag_cstring, NULL, defs_cstring);

		MEM_freeN(vert_cstring);
		MEM_freeN(frag_cstring);
		MEM_freeN(defs_cstring);

		BLI_dynstr_free(vert);
		BLI_dynstr_free(frag);
		BLI_dynstr_free(defs);

		if (BASIC_SHADER.gpushader[tweaked_options] != NULL) {
			gpu_common_get_symbols(BASIC_SHADER.common + tweaked_options, BASIC_SHADER.gpushader[tweaked_options]);
			gpu_set_common(BASIC_SHADER.common + tweaked_options);

			GPU_shader_bind(BASIC_SHADER.gpushader[tweaked_options]);
		}
		else {
			BASIC_SHADER.failed[tweaked_options] = true;
			gpu_set_common(NULL);
		}
	}
	else {
		gpu_set_common(NULL);
	}
}



/* Bind / Unbind */



void gpu_basic_bind(void)
{
	bool glsl_support = GPU_glsl_support();

	if (glsl_support) {
		basic_shader_bind();
	}

#if defined(WITH_GL_PROFILE_COMPAT)
	GPU_CHECK_NO_ERROR();

	if (!glsl_support) {
		if (BASIC_SHADER.options & GPU_BASIC_LIGHTING)
			glEnable(GL_LIGHTING);
		else
			glDisable(GL_LIGHTING);

		if (BASIC_SHADER.options & GPU_BASIC_TEXTURE_2D)
			glEnable(GL_TEXTURE_2D);
		else
			glDisable(GL_TEXTURE_2D);

		if (BASIC_SHADER.options & GPU_BASIC_TWO_SIDE)
			glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
		else
			glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);

		if (BASIC_SHADER.options & GPU_BASIC_LOCAL_VIEWER)
			glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
		else
			glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);

		if (BASIC_SHADER.options & GPU_BASIC_SMOOTH)
			glShadeModel(GL_SMOOTH);
		else
			glShadeModel(GL_FLAT);

		gpu_toggle_clipping(BASIC_SHADER.options & GPU_BASIC_CLIPPING);
	}

	GPU_CHECK_NO_ERROR();
#endif

	if (BASIC_SHADER.options & GPU_BASIC_LIGHTING) {
		gpu_commit_lighting();
		gpu_commit_material();
	}

	if (BASIC_SHADER.options & GPU_BASIC_CLIPPING) {
		gpu_commit_clipping();
	}

	gpu_commit_matrix();
}



void gpu_basic_unbind(void)
{
	if (GPU_glsl_support())
		GPU_shader_unbind();

#if defined(WITH_GL_PROFILE_COMPAT)
	GPU_CHECK_NO_ERROR();

	glDisable(GL_LIGHTING);
	glDisable(GL_TEXTURE_2D);
	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
	glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
	glShadeModel(GL_FLAT);
	gpu_toggle_clipping(false);

	GPU_CHECK_NO_ERROR();
#endif
}



bool GPU_basic_needs_normals(void)
{
	return BASIC_SHADER.options & GPU_BASIC_LIGHTING; // Temporary hack. Should be solved outside of this file.
}