• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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->nir);
145    ralloc_free(sh->Program);
146    ralloc_free(sh);
147 }
148 
149 void
_mesa_clear_shader_program_data(struct gl_context * ctx,struct gl_shader_program * shProg)150 _mesa_clear_shader_program_data(struct gl_context *ctx,
151                                 struct gl_shader_program *shProg)
152 {
153    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
154       if (shProg->_LinkedShaders[i] != NULL) {
155          _mesa_delete_linked_shader(ctx, shProg->_LinkedShaders[i]);
156          shProg->_LinkedShaders[i] = NULL;
157       }
158    }
159 
160    shProg->data->NumUniformStorage = 0;
161    shProg->data->UniformStorage = NULL;
162    shProg->NumUniformRemapTable = 0;
163    shProg->UniformRemapTable = NULL;
164 
165    ralloc_free(shProg->data->InfoLog);
166    shProg->data->InfoLog = ralloc_strdup(shProg->data, "");
167 
168    ralloc_free(shProg->data->UniformBlocks);
169    shProg->data->UniformBlocks = NULL;
170    shProg->data->NumUniformBlocks = 0;
171 
172    ralloc_free(shProg->data->ShaderStorageBlocks);
173    shProg->data->ShaderStorageBlocks = NULL;
174    shProg->data->NumShaderStorageBlocks = 0;
175 
176    ralloc_free(shProg->data->AtomicBuffers);
177    shProg->data->AtomicBuffers = NULL;
178    shProg->data->NumAtomicBuffers = 0;
179 }
180 
181 
182 static void
init_gl_program(struct gl_program * prog,bool is_arb_asm,gl_shader_stage stage)183 init_gl_program(struct gl_program *prog, bool is_arb_asm, gl_shader_stage stage)
184 {
185    prog->RefCount = 1;
186    prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
187    prog->info.use_legacy_math_rules = is_arb_asm;
188    prog->info.stage = stage;
189 }
190 
191 static struct gl_program *
standalone_new_program(UNUSED struct gl_context * ctx,gl_shader_stage stage,UNUSED GLuint id,bool is_arb_asm)192 standalone_new_program(UNUSED struct gl_context *ctx, gl_shader_stage stage,
193                        UNUSED GLuint id, bool is_arb_asm)
194 {
195    struct gl_program *prog = rzalloc(NULL, struct gl_program);
196    init_gl_program(prog, is_arb_asm, stage);
197    return prog;
198 }
199 
initialize_context_to_defaults(struct gl_context * ctx,gl_api api)200 void initialize_context_to_defaults(struct gl_context *ctx, gl_api api)
201 {
202    memset(ctx, 0, sizeof(*ctx));
203 
204    ctx->API = api;
205 
206    ctx->Extensions.dummy_true = true;
207    ctx->Extensions.ARB_compute_shader = true;
208    ctx->Extensions.ARB_compute_variable_group_size = true;
209    ctx->Extensions.ARB_conservative_depth = true;
210    ctx->Extensions.ARB_draw_instanced = true;
211    ctx->Extensions.ARB_ES2_compatibility = true;
212    ctx->Extensions.ARB_ES3_compatibility = true;
213    ctx->Extensions.ARB_explicit_attrib_location = true;
214    ctx->Extensions.ARB_fragment_coord_conventions = true;
215    ctx->Extensions.ARB_fragment_layer_viewport = true;
216    ctx->Extensions.ARB_gpu_shader5 = true;
217    ctx->Extensions.ARB_gpu_shader_fp64 = true;
218    ctx->Extensions.ARB_gpu_shader_int64 = true;
219    ctx->Extensions.ARB_sample_shading = true;
220    ctx->Extensions.ARB_shader_bit_encoding = true;
221    ctx->Extensions.ARB_shader_draw_parameters = true;
222    ctx->Extensions.ARB_shader_stencil_export = true;
223    ctx->Extensions.ARB_shader_texture_lod = true;
224    ctx->Extensions.ARB_shading_language_420pack = true;
225    ctx->Extensions.ARB_tessellation_shader = true;
226    ctx->Extensions.ARB_texture_cube_map_array = true;
227    ctx->Extensions.ARB_texture_gather = true;
228    ctx->Extensions.ARB_texture_multisample = true;
229    ctx->Extensions.ARB_texture_query_levels = true;
230    ctx->Extensions.ARB_texture_query_lod = true;
231    ctx->Extensions.ARB_uniform_buffer_object = true;
232    ctx->Extensions.ARB_viewport_array = true;
233    ctx->Extensions.ARB_cull_distance = true;
234    ctx->Extensions.ARB_bindless_texture = true;
235 
236    ctx->Extensions.OES_EGL_image_external = true;
237    ctx->Extensions.OES_standard_derivatives = true;
238    ctx->Extensions.OES_texture_3D = true;
239 
240    ctx->Extensions.EXT_gpu_shader4 = true;
241    ctx->Extensions.EXT_shader_integer_mix = true;
242    ctx->Extensions.EXT_shadow_samplers = true;
243    ctx->Extensions.EXT_texture_array = true;
244 
245    ctx->Extensions.MESA_shader_integer_functions = true;
246 
247    ctx->Extensions.NV_texture_rectangle = true;
248 
249    ctx->Const.GLSLVersion = 120;
250 
251    /* 1.20 minimums. */
252    ctx->Const.MaxLights = 8;
253    ctx->Const.MaxClipPlanes = 6;
254    ctx->Const.MaxTextureUnits = 2;
255    ctx->Const.MaxTextureCoordUnits = 2;
256    ctx->Const.Program[MESA_SHADER_VERTEX].MaxAttribs = 16;
257 
258    ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents = 512;
259    ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents = 32;
260    ctx->Const.MaxVarying = 8; /* == gl_MaxVaryingFloats / 4 */
261    ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = 0;
262    ctx->Const.MaxCombinedTextureImageUnits = 2;
263    ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = 2;
264    ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents = 64;
265    ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = 32;
266 
267    ctx->Const.MaxDrawBuffers = 1;
268    ctx->Const.MaxComputeWorkGroupCount[0] = 65535;
269    ctx->Const.MaxComputeWorkGroupCount[1] = 65535;
270    ctx->Const.MaxComputeWorkGroupCount[2] = 65535;
271    ctx->Const.MaxComputeWorkGroupSize[0] = 1024;
272    ctx->Const.MaxComputeWorkGroupSize[1] = 1024;
273    ctx->Const.MaxComputeWorkGroupSize[2] = 64;
274    ctx->Const.MaxComputeWorkGroupInvocations = 1024;
275    ctx->Const.MaxComputeVariableGroupSize[0] = 512;
276    ctx->Const.MaxComputeVariableGroupSize[1] = 512;
277    ctx->Const.MaxComputeVariableGroupSize[2] = 64;
278    ctx->Const.MaxComputeVariableGroupInvocations = 512;
279    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits = 16;
280    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxUniformComponents = 1024;
281    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxInputComponents = 0; /* not used */
282    ctx->Const.Program[MESA_SHADER_COMPUTE].MaxOutputComponents = 0; /* not used */
283 
284    /* Set up default shader compiler options. */
285    struct gl_shader_compiler_options options;
286    memset(&options, 0, sizeof(options));
287    options.MaxIfDepth = UINT_MAX;
288 
289    for (int sh = 0; sh < MESA_SHADER_STAGES; ++sh)
290       memcpy(&ctx->Const.ShaderCompilerOptions[sh], &options, sizeof(options));
291 
292    ctx->Driver.NewProgram = standalone_new_program;
293 }
294 
295 struct gl_shader_program *
standalone_create_shader_program(void)296 standalone_create_shader_program(void)
297 {
298    struct gl_shader_program *whole_program;
299 
300    whole_program = rzalloc (NULL, struct gl_shader_program);
301    assert(whole_program != NULL);
302    whole_program->data = rzalloc(whole_program, struct gl_shader_program_data);
303    assert(whole_program->data != NULL);
304    whole_program->data->InfoLog = ralloc_strdup(whole_program->data, "");
305 
306    /* Created just to avoid segmentation faults */
307    whole_program->AttributeBindings = new string_to_uint_map;
308    whole_program->FragDataBindings = new string_to_uint_map;
309    whole_program->FragDataIndexBindings = new string_to_uint_map;
310 
311    exec_list_make_empty(&whole_program->EmptyUniformLocations);
312 
313    return whole_program;
314 }
315 
316 void
standalone_destroy_shader_program(struct gl_shader_program * whole_program)317 standalone_destroy_shader_program(struct gl_shader_program *whole_program)
318 {
319    for (unsigned i = 0; i < whole_program->NumShaders; i++) {
320          ralloc_free(whole_program->Shaders[i]->nir);
321    }
322 
323    for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
324       if (whole_program->_LinkedShaders[i]) {
325          if (whole_program->_LinkedShaders[i]->Program->Parameters)
326             _mesa_free_parameter_list(whole_program->_LinkedShaders[i]->Program->Parameters);
327          _mesa_delete_linked_shader(NULL, whole_program->_LinkedShaders[i]);
328       }
329    }
330 
331    delete whole_program->AttributeBindings;
332    delete whole_program->FragDataBindings;
333    delete whole_program->FragDataIndexBindings;
334 
335    ralloc_free(whole_program);
336 }
337 
338 struct gl_shader *
standalone_add_shader_source(struct gl_context * ctx,struct gl_shader_program * whole_program,GLenum type,const char * source)339 standalone_add_shader_source(struct gl_context *ctx, struct gl_shader_program *whole_program, GLenum type, const char *source)
340 {
341    blake3_hash source_blake3;
342    _mesa_blake3_compute(source, strlen(source), source_blake3);
343 
344    struct gl_shader *shader = rzalloc(whole_program, gl_shader);
345    shader->Type = type;
346    shader->Stage = _mesa_shader_enum_to_shader_stage(type);
347    shader->Source = source;
348    memcpy(shader->source_blake3, source_blake3, BLAKE3_OUT_LEN);
349 
350    whole_program->Shaders = reralloc(whole_program, whole_program->Shaders,
351                                      struct gl_shader *, whole_program->NumShaders + 1);
352    assert(whole_program->Shaders != NULL);
353 
354    whole_program->Shaders[whole_program->NumShaders] = shader;
355    whole_program->NumShaders++;
356 
357    return shader;
358 }
359