1 /*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /* This file declares stripped-down versions of functions that
25 * normally exist outside of the glsl folder, so that they can be used
26 * when running the GLSL compiler standalone (for unit testing or
27 * compiling builtins).
28 */
29
30 #include "standalone_scaffolding.h"
31
32 #include <assert.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include "util/ralloc.h"
36 #include "util/strtod.h"
37 #include "main/mtypes.h"
38 #include "string_to_uint_map.h"
39
40 void
_mesa_warning(struct gl_context * ctx,const char * fmt,...)41 _mesa_warning(struct gl_context *ctx, const char *fmt, ...)
42 {
43 va_list vargs;
44 (void) ctx;
45
46 va_start(vargs, fmt);
47
48 /* This output is not thread-safe, but that's good enough for the
49 * standalone compiler.
50 */
51 fprintf(stderr, "Mesa warning: ");
52 vfprintf(stderr, fmt, vargs);
53 fprintf(stderr, "\n");
54
55 va_end(vargs);
56 }
57
58 void
_mesa_problem(const struct gl_context * ctx,const char * fmt,...)59 _mesa_problem(const struct gl_context *ctx, const char *fmt, ...)
60 {
61 va_list vargs;
62 (void) ctx;
63
64 va_start(vargs, fmt);
65
66 /* This output is not thread-safe, but that's good enough for the
67 * standalone compiler.
68 */
69 fprintf(stderr, "Mesa problem: ");
70 vfprintf(stderr, fmt, vargs);
71 fprintf(stderr, "\n");
72
73 va_end(vargs);
74 }
75
76 void
_mesa_reference_shader_program_data(struct gl_shader_program_data ** ptr,struct gl_shader_program_data * data)77 _mesa_reference_shader_program_data(struct gl_shader_program_data **ptr,
78 struct gl_shader_program_data *data)
79 {
80 *ptr = data;
81 }
82
83 void
_mesa_reference_shader(struct gl_context * ctx,struct gl_shader ** ptr,struct gl_shader * sh)84 _mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
85 struct gl_shader *sh)
86 {
87 (void) ctx;
88 *ptr = sh;
89 }
90
91 void
_mesa_reference_program_(struct gl_context * ctx,struct gl_program ** ptr,struct gl_program * prog)92 _mesa_reference_program_(struct gl_context *ctx, struct gl_program **ptr,
93 struct gl_program *prog)
94 {
95 (void) ctx;
96 *ptr = prog;
97 }
98
99 void
_mesa_shader_debug(struct gl_context *,GLenum,GLuint *,const char *)100 _mesa_shader_debug(struct gl_context *, GLenum, GLuint *,
101 const char *)
102 {
103 }
104
105 struct gl_shader *
_mesa_new_shader(GLuint name,gl_shader_stage stage)106 _mesa_new_shader(GLuint name, gl_shader_stage stage)
107 {
108 struct gl_shader *shader;
109
110 assert(stage == MESA_SHADER_FRAGMENT || stage == MESA_SHADER_VERTEX);
111 shader = rzalloc(NULL, struct gl_shader);
112 if (shader) {
113 shader->Stage = stage;
114 shader->Name = name;
115 shader->RefCount = 1;
116 }
117 return shader;
118 }
119
120 GLbitfield
_mesa_program_state_flags(UNUSED const gl_state_index16 state[STATE_LENGTH])121 _mesa_program_state_flags(UNUSED const gl_state_index16 state[STATE_LENGTH])
122 {
123 return 0;
124 }
125
126 char *
_mesa_program_state_string(UNUSED const gl_state_index16 state[STATE_LENGTH])127 _mesa_program_state_string(UNUSED const gl_state_index16 state[STATE_LENGTH])
128 {
129 return NULL;
130 }
131
132 void
_mesa_delete_shader(struct gl_context *,struct gl_shader * sh)133 _mesa_delete_shader(struct gl_context *, struct gl_shader *sh)
134 {
135 free((void *)sh->Source);
136 free(sh->Label);
137 ralloc_free(sh);
138 }
139
140 void
_mesa_delete_linked_shader(struct gl_context *,struct gl_linked_shader * sh)141 _mesa_delete_linked_shader(struct gl_context *,
142 struct gl_linked_shader *sh)
143 {
144 ralloc_free(sh->Program);
145 ralloc_free(sh);
146 }
147
148 void
_mesa_clear_shader_program_data(struct gl_context * ctx,struct gl_shader_program * shProg)149 _mesa_clear_shader_program_data(struct gl_context *ctx,
150 struct gl_shader_program *shProg)
151 {
152 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
153 if (shProg->_LinkedShaders[i] != NULL) {
154 _mesa_delete_linked_shader(ctx, shProg->_LinkedShaders[i]);
155 shProg->_LinkedShaders[i] = NULL;
156 }
157 }
158
159 shProg->data->NumUniformStorage = 0;
160 shProg->data->UniformStorage = NULL;
161 shProg->NumUniformRemapTable = 0;
162 shProg->UniformRemapTable = NULL;
163
164 ralloc_free(shProg->data->InfoLog);
165 shProg->data->InfoLog = ralloc_strdup(shProg->data, "");
166
167 ralloc_free(shProg->data->UniformBlocks);
168 shProg->data->UniformBlocks = NULL;
169 shProg->data->NumUniformBlocks = 0;
170
171 ralloc_free(shProg->data->ShaderStorageBlocks);
172 shProg->data->ShaderStorageBlocks = NULL;
173 shProg->data->NumShaderStorageBlocks = 0;
174
175 ralloc_free(shProg->data->AtomicBuffers);
176 shProg->data->AtomicBuffers = NULL;
177 shProg->data->NumAtomicBuffers = 0;
178 }
179
180
181 static void
init_gl_program(struct gl_program * prog,bool is_arb_asm,gl_shader_stage stage)182 init_gl_program(struct gl_program *prog, bool is_arb_asm, gl_shader_stage stage)
183 {
184 prog->RefCount = 1;
185 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
186 prog->info.use_legacy_math_rules = is_arb_asm;
187 prog->info.stage = stage;
188 }
189
190 static struct gl_program *
standalone_new_program(UNUSED struct gl_context * ctx,gl_shader_stage stage,UNUSED GLuint id,bool is_arb_asm)191 standalone_new_program(UNUSED struct gl_context *ctx, gl_shader_stage stage,
192 UNUSED GLuint id, bool is_arb_asm)
193 {
194 struct gl_program *prog = rzalloc(NULL, struct gl_program);
195 init_gl_program(prog, is_arb_asm, stage);
196 return prog;
197 }
198
initialize_context_to_defaults(struct gl_context * ctx,gl_api api)199 void initialize_context_to_defaults(struct gl_context *ctx, gl_api api)
200 {
201 memset(ctx, 0, sizeof(*ctx));
202
203 ctx->API = api;
204
205 ctx->Extensions.dummy_true = true;
206 ctx->Extensions.ARB_compute_shader = true;
207 ctx->Extensions.ARB_compute_variable_group_size = true;
208 ctx->Extensions.ARB_conservative_depth = true;
209 ctx->Extensions.ARB_draw_instanced = true;
210 ctx->Extensions.ARB_ES2_compatibility = true;
211 ctx->Extensions.ARB_ES3_compatibility = true;
212 ctx->Extensions.ARB_explicit_attrib_location = true;
213 ctx->Extensions.ARB_fragment_coord_conventions = true;
214 ctx->Extensions.ARB_fragment_layer_viewport = true;
215 ctx->Extensions.ARB_gpu_shader5 = true;
216 ctx->Extensions.ARB_gpu_shader_fp64 = true;
217 ctx->Extensions.ARB_gpu_shader_int64 = true;
218 ctx->Extensions.ARB_sample_shading = true;
219 ctx->Extensions.ARB_shader_bit_encoding = true;
220 ctx->Extensions.ARB_shader_draw_parameters = true;
221 ctx->Extensions.ARB_shader_stencil_export = true;
222 ctx->Extensions.ARB_shader_texture_lod = true;
223 ctx->Extensions.ARB_shading_language_420pack = true;
224 ctx->Extensions.ARB_tessellation_shader = true;
225 ctx->Extensions.ARB_texture_cube_map_array = true;
226 ctx->Extensions.ARB_texture_gather = true;
227 ctx->Extensions.ARB_texture_multisample = true;
228 ctx->Extensions.ARB_texture_query_levels = true;
229 ctx->Extensions.ARB_texture_query_lod = true;
230 ctx->Extensions.ARB_uniform_buffer_object = true;
231 ctx->Extensions.ARB_viewport_array = true;
232 ctx->Extensions.ARB_cull_distance = true;
233 ctx->Extensions.ARB_bindless_texture = true;
234
235 ctx->Extensions.OES_EGL_image_external = true;
236 ctx->Extensions.OES_standard_derivatives = true;
237 ctx->Extensions.OES_texture_3D = true;
238
239 ctx->Extensions.EXT_gpu_shader4 = true;
240 ctx->Extensions.EXT_shader_integer_mix = true;
241 ctx->Extensions.EXT_shadow_samplers = true;
242 ctx->Extensions.EXT_texture_array = true;
243
244 ctx->Extensions.MESA_shader_integer_functions = true;
245
246 ctx->Extensions.NV_texture_rectangle = true;
247
248 ctx->Const.GLSLVersion = 120;
249
250 /* 1.20 minimums. */
251 ctx->Const.MaxLights = 8;
252 ctx->Const.MaxClipPlanes = 6;
253 ctx->Const.MaxTextureUnits = 2;
254 ctx->Const.MaxTextureCoordUnits = 2;
255 ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
256
257 ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 512;
258 ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32;
259 ctx->Const.MaxVarying = 8; /* == gl_MaxVaryingFloats / 4 */
260 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0;
261 ctx->Const.MaxCombinedTextureImageUnits = 2;
262 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 2;
263 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 64;
264 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = 32;
265
266 ctx->Const.MaxDrawBuffers = 1;
267 ctx->Const.MaxComputeWorkGroupCount[0] = 65535;
268 ctx->Const.MaxComputeWorkGroupCount[1] = 65535;
269 ctx->Const.MaxComputeWorkGroupCount[2] = 65535;
270 ctx->Const.MaxComputeWorkGroupSize[0] = 1024;
271 ctx->Const.MaxComputeWorkGroupSize[1] = 1024;
272 ctx->Const.MaxComputeWorkGroupSize[2] = 64;
273 ctx->Const.MaxComputeWorkGroupInvocations = 1024;
274 ctx->Const.MaxComputeVariableGroupSize[0] = 512;
275 ctx->Const.MaxComputeVariableGroupSize[1] = 512;
276 ctx->Const.MaxComputeVariableGroupSize[2] = 64;
277 ctx->Const.MaxComputeVariableGroupInvocations = 512;
278 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits = 16;
279 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformComponents = 1024;
280 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxInputComponents = 0; /* not used */
281 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxOutputComponents = 0; /* not used */
282
283 /* Set up default shader compiler options. */
284 struct gl_shader_compiler_options options;
285 memset(&options, 0, sizeof(options));
286 options.MaxIfDepth = UINT_MAX;
287
288 for (int sh = 0; sh < MESA_SHADER_STAGES; ++sh)
289 memcpy(&ctx->Const.ShaderCompilerOptions[sh], &options, sizeof(options));
290
291 ctx->Driver.NewProgram = standalone_new_program;
292 }
293
294 struct gl_shader_program *
standalone_create_shader_program(void)295 standalone_create_shader_program(void)
296 {
297 struct gl_shader_program *whole_program;
298
299 whole_program = rzalloc (NULL, struct gl_shader_program);
300 assert(whole_program != NULL);
301 whole_program->data = rzalloc(whole_program, struct gl_shader_program_data);
302 assert(whole_program->data != NULL);
303 whole_program->data->InfoLog = ralloc_strdup(whole_program->data, "");
304
305 /* Created just to avoid segmentation faults */
306 whole_program->AttributeBindings = new string_to_uint_map;
307 whole_program->FragDataBindings = new string_to_uint_map;
308 whole_program->FragDataIndexBindings = new string_to_uint_map;
309
310 exec_list_make_empty(&whole_program->EmptyUniformLocations);
311
312 return whole_program;
313 }
314
315 void
standalone_destroy_shader_program(struct gl_shader_program * whole_program)316 standalone_destroy_shader_program(struct gl_shader_program *whole_program)
317 {
318 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
319 if (whole_program->_LinkedShaders[i])
320 _mesa_delete_linked_shader(NULL, whole_program->_LinkedShaders[i]);
321 }
322
323 delete whole_program->AttributeBindings;
324 delete whole_program->FragDataBindings;
325 delete whole_program->FragDataIndexBindings;
326
327 ralloc_free(whole_program);
328 }
329
330 struct gl_shader *
standalone_add_shader_source(struct gl_context * ctx,struct gl_shader_program * whole_program,GLenum type,const char * source)331 standalone_add_shader_source(struct gl_context *ctx, struct gl_shader_program *whole_program, GLenum type, const char *source)
332 {
333 struct gl_shader *shader = rzalloc(whole_program, gl_shader);
334 shader->Type = type;
335 shader->Stage = _mesa_shader_enum_to_shader_stage(type);
336 shader->Source = source;
337
338 whole_program->Shaders = reralloc(whole_program, whole_program->Shaders,
339 struct gl_shader *, whole_program->NumShaders + 1);
340 assert(whole_program->Shaders != NULL);
341
342 whole_program->Shaders[whole_program->NumShaders] = shader;
343 whole_program->NumShaders++;
344
345 return shader;
346 }
347