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