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