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

gpu_font.c « intern « gpu « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f355c9a72b49807987aedd213750b70d177a48e7 (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
/*
 * ***** 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): Jason Wilkins
 *
 * ***** END GPL LICENSE BLOCK *****
 */

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

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

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

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

/* external */

#include "BLI_dynstr.h"
#include "BLI_utildefines.h"

#include "MEM_guardedalloc.h"



static struct GPUShader*  FONT_SHADER = NULL;
static struct GPUcommon   FONT_COMMON = {0};
static bool               FONT_FAILED = false;

#if GPU_SAFETY
static bool FONT_BEGUN = false;
#endif



void gpu_font_init(void)
{
	FONT_SHADER = NULL;
}



void gpu_font_exit(void)
{
	GPU_shader_free(FONT_SHADER);
	
#if GPU_SAFETY
	FONT_BEGUN = false;
#endif
}



static void gpu_font_shader(void)
{
	/* GLSL code */
	extern const char datatoc_gpu_shader_font_vert_glsl[];
	extern const char datatoc_gpu_shader_font_frag_glsl[];

	/* Create shader if it doesn't exist yet. */
	if (FONT_SHADER != NULL) {
		GPU_shader_bind(FONT_SHADER);
		gpu_set_common(&FONT_COMMON);
	}
	else if (!FONT_FAILED) {
		DynStr* vert = BLI_dynstr_new();
		DynStr* frag = BLI_dynstr_new();
		DynStr* defs = BLI_dynstr_new();

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

		gpu_include_common_vert(vert);
		BLI_dynstr_append(vert, datatoc_gpu_shader_font_vert_glsl);

		gpu_include_common_frag(frag);
		BLI_dynstr_append(frag, datatoc_gpu_shader_font_frag_glsl);

		gpu_include_common_defs(defs);
		BLI_dynstr_append(defs, "#define USE_TEXTURE_2D\n");

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

		FONT_SHADER =
			GPU_shader_create("Font", 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 (FONT_SHADER != NULL) {
			gpu_common_get_symbols(&FONT_COMMON, FONT_SHADER);
			gpu_set_common(&FONT_COMMON);

			GPU_shader_bind(FONT_SHADER);
		}
		else {
			FONT_FAILED = true;
			gpu_set_common(NULL);
		}
	}
	else {
		gpu_set_common(NULL);
	}
}



/* Bind / Unbind */



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

	GPU_ASSERT(FONT_BEGUN);
	
	if (glsl_support)
		gpu_font_shader();

#if defined(WITH_GL_PROFILE_COMPAT)
	if (!glsl_support)
		GPU_CHECK(glEnable(GL_TEXTURE_2D));
#endif

	gpu_commit_matrix();
}



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

	GPU_ASSERT(FONT_BEGUN);
	
	if (glsl_support)
		GPU_shader_unbind();

#if defined(WITH_GL_PROFILE_COMPAT)
	if (!glsl_support)
		GPU_CHECK(glDisable(GL_TEXTURE_2D));
#endif
}



void GPU_font_begin(void)
{
#if GPU_SAFETY
	GPU_ASSERT(!FONT_BEGUN);
	FONT_BEGUN = true;
#endif

	GPU_aspect_end(); /* assuming was GPU_ASPECT_BASIC */

	GPU_aspect_begin(GPU_ASPECT_FONT, 0);
}



void GPU_font_end(void)
{
#if GPU_SAFETY
	GPU_ASSERT(FONT_BEGUN);
#endif

	GPU_aspect_end();
	
#if GPU_SAFETY
	FONT_BEGUN = false;
#endif

	GPU_aspect_begin(GPU_ASPECT_BASIC, 0);
}