mesa: allow setting of patch parameters.
[mesa.git] / src / mesa / main / shaderapi.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2004-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009-2010 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /**
27 * \file shaderapi.c
28 * \author Brian Paul
29 *
30 * Implementation of GLSL-related API functions.
31 * The glUniform* functions are in uniforms.c
32 *
33 *
34 * XXX things to do:
35 * 1. Check that the right error code is generated for all _mesa_error() calls.
36 * 2. Insert FLUSH_VERTICES calls in various places
37 */
38
39
40 #include "main/glheader.h"
41 #include "main/context.h"
42 #include "main/dispatch.h"
43 #include "main/enums.h"
44 #include "main/hash.h"
45 #include "main/mtypes.h"
46 #include "main/pipelineobj.h"
47 #include "main/shaderapi.h"
48 #include "main/shaderobj.h"
49 #include "main/transformfeedback.h"
50 #include "main/uniforms.h"
51 #include "program/program.h"
52 #include "program/prog_print.h"
53 #include "program/prog_parameter.h"
54 #include "util/ralloc.h"
55 #include "util/hash_table.h"
56 #include <stdbool.h>
57 #include "../glsl/glsl_parser_extras.h"
58 #include "../glsl/ir.h"
59 #include "../glsl/ir_uniform.h"
60 #include "../glsl/program.h"
61
62 /** Define this to enable shader substitution (see below) */
63 #define SHADER_SUBST 0
64
65
66 /**
67 * Return mask of GLSL_x flags by examining the MESA_GLSL env var.
68 */
69 GLbitfield
70 _mesa_get_shader_flags(void)
71 {
72 GLbitfield flags = 0x0;
73 const char *env = getenv("MESA_GLSL");
74
75 if (env) {
76 if (strstr(env, "dump_on_error"))
77 flags |= GLSL_DUMP_ON_ERROR;
78 else if (strstr(env, "dump"))
79 flags |= GLSL_DUMP;
80 if (strstr(env, "log"))
81 flags |= GLSL_LOG;
82 if (strstr(env, "nopvert"))
83 flags |= GLSL_NOP_VERT;
84 if (strstr(env, "nopfrag"))
85 flags |= GLSL_NOP_FRAG;
86 if (strstr(env, "nopt"))
87 flags |= GLSL_NO_OPT;
88 else if (strstr(env, "opt"))
89 flags |= GLSL_OPT;
90 if (strstr(env, "uniform"))
91 flags |= GLSL_UNIFORMS;
92 if (strstr(env, "useprog"))
93 flags |= GLSL_USE_PROG;
94 if (strstr(env, "errors"))
95 flags |= GLSL_REPORT_ERRORS;
96 }
97
98 return flags;
99 }
100
101
102 /**
103 * Initialize context's shader state.
104 */
105 void
106 _mesa_init_shader_state(struct gl_context *ctx)
107 {
108 /* Device drivers may override these to control what kind of instructions
109 * are generated by the GLSL compiler.
110 */
111 struct gl_shader_compiler_options options;
112 gl_shader_stage sh;
113 int i;
114
115 memset(&options, 0, sizeof(options));
116 options.MaxUnrollIterations = 32;
117 options.MaxIfDepth = UINT_MAX;
118
119 for (sh = 0; sh < MESA_SHADER_STAGES; ++sh)
120 memcpy(&ctx->Const.ShaderCompilerOptions[sh], &options, sizeof(options));
121
122 ctx->Shader.Flags = _mesa_get_shader_flags();
123
124 if (ctx->Shader.Flags != 0)
125 ctx->Const.GenerateTemporaryNames = true;
126
127 /* Extended for ARB_separate_shader_objects */
128 ctx->Shader.RefCount = 1;
129 mtx_init(&ctx->Shader.Mutex, mtx_plain);
130
131 ctx->TessCtrlProgram.patch_vertices = 3;
132 for (i = 0; i < 4; ++i)
133 ctx->TessCtrlProgram.patch_default_outer_level[i] = 1.0;
134 for (i = 0; i < 2; ++i)
135 ctx->TessCtrlProgram.patch_default_inner_level[i] = 1.0;
136 }
137
138
139 /**
140 * Free the per-context shader-related state.
141 */
142 void
143 _mesa_free_shader_state(struct gl_context *ctx)
144 {
145 int i;
146 for (i = 0; i < MESA_SHADER_STAGES; i++) {
147 _mesa_reference_shader_program(ctx, &ctx->Shader.CurrentProgram[i],
148 NULL);
149 }
150 _mesa_reference_shader_program(ctx, &ctx->Shader._CurrentFragmentProgram,
151 NULL);
152 _mesa_reference_shader_program(ctx, &ctx->Shader.ActiveProgram, NULL);
153
154 /* Extended for ARB_separate_shader_objects */
155 _mesa_reference_pipeline_object(ctx, &ctx->_Shader, NULL);
156
157 assert(ctx->Shader.RefCount == 1);
158 mtx_destroy(&ctx->Shader.Mutex);
159 }
160
161
162 /**
163 * Copy string from <src> to <dst>, up to maxLength characters, returning
164 * length of <dst> in <length>.
165 * \param src the strings source
166 * \param maxLength max chars to copy
167 * \param length returns number of chars copied
168 * \param dst the string destination
169 */
170 void
171 _mesa_copy_string(GLchar *dst, GLsizei maxLength,
172 GLsizei *length, const GLchar *src)
173 {
174 GLsizei len;
175 for (len = 0; len < maxLength - 1 && src && src[len]; len++)
176 dst[len] = src[len];
177 if (maxLength > 0)
178 dst[len] = 0;
179 if (length)
180 *length = len;
181 }
182
183
184
185 /**
186 * Confirm that the a shader type is valid and supported by the implementation
187 *
188 * \param ctx Current GL context
189 * \param type Shader target
190 *
191 */
192 bool
193 _mesa_validate_shader_target(const struct gl_context *ctx, GLenum type)
194 {
195 /* Note: when building built-in GLSL functions, this function may be
196 * invoked with ctx == NULL. In that case, we can only validate that it's
197 * a shader target we recognize, not that it's supported in the current
198 * context. But that's fine--we don't need any further validation than
199 * that when building built-in GLSL functions.
200 */
201
202 switch (type) {
203 case GL_FRAGMENT_SHADER:
204 return ctx == NULL || ctx->Extensions.ARB_fragment_shader;
205 case GL_VERTEX_SHADER:
206 return ctx == NULL || ctx->Extensions.ARB_vertex_shader;
207 case GL_GEOMETRY_SHADER_ARB:
208 return ctx == NULL || _mesa_has_geometry_shaders(ctx);
209 case GL_COMPUTE_SHADER:
210 return ctx == NULL || ctx->Extensions.ARB_compute_shader;
211 default:
212 return false;
213 }
214 }
215
216
217 static GLboolean
218 is_program(struct gl_context *ctx, GLuint name)
219 {
220 struct gl_shader_program *shProg = _mesa_lookup_shader_program(ctx, name);
221 return shProg ? GL_TRUE : GL_FALSE;
222 }
223
224
225 static GLboolean
226 is_shader(struct gl_context *ctx, GLuint name)
227 {
228 struct gl_shader *shader = _mesa_lookup_shader(ctx, name);
229 return shader ? GL_TRUE : GL_FALSE;
230 }
231
232
233 /**
234 * Attach shader to a shader program.
235 */
236 static void
237 attach_shader(struct gl_context *ctx, GLuint program, GLuint shader)
238 {
239 struct gl_shader_program *shProg;
240 struct gl_shader *sh;
241 GLuint i, n;
242
243 const bool same_type_disallowed = _mesa_is_gles(ctx);
244
245 shProg = _mesa_lookup_shader_program_err(ctx, program, "glAttachShader");
246 if (!shProg)
247 return;
248
249 sh = _mesa_lookup_shader_err(ctx, shader, "glAttachShader");
250 if (!sh) {
251 return;
252 }
253
254 n = shProg->NumShaders;
255 for (i = 0; i < n; i++) {
256 if (shProg->Shaders[i] == sh) {
257 /* The shader is already attched to this program. The
258 * GL_ARB_shader_objects spec says:
259 *
260 * "The error INVALID_OPERATION is generated by AttachObjectARB
261 * if <obj> is already attached to <containerObj>."
262 */
263 _mesa_error(ctx, GL_INVALID_OPERATION, "glAttachShader");
264 return;
265 } else if (same_type_disallowed &&
266 shProg->Shaders[i]->Type == sh->Type) {
267 /* Shader with the same type is already attached to this program,
268 * OpenGL ES 2.0 and 3.0 specs say:
269 *
270 * "Multiple shader objects of the same type may not be attached
271 * to a single program object. [...] The error INVALID_OPERATION
272 * is generated if [...] another shader object of the same type
273 * as shader is already attached to program."
274 */
275 _mesa_error(ctx, GL_INVALID_OPERATION, "glAttachShader");
276 return;
277 }
278 }
279
280 /* grow list */
281 shProg->Shaders = realloc(shProg->Shaders,
282 (n + 1) * sizeof(struct gl_shader *));
283 if (!shProg->Shaders) {
284 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glAttachShader");
285 return;
286 }
287
288 /* append */
289 shProg->Shaders[n] = NULL; /* since realloc() didn't zero the new space */
290 _mesa_reference_shader(ctx, &shProg->Shaders[n], sh);
291 shProg->NumShaders++;
292 }
293
294
295 static GLuint
296 create_shader(struct gl_context *ctx, GLenum type)
297 {
298 struct gl_shader *sh;
299 GLuint name;
300
301 if (!_mesa_validate_shader_target(ctx, type)) {
302 _mesa_error(ctx, GL_INVALID_ENUM, "CreateShader(type)");
303 return 0;
304 }
305
306 name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
307 sh = ctx->Driver.NewShader(ctx, name, type);
308 _mesa_HashInsert(ctx->Shared->ShaderObjects, name, sh);
309
310 return name;
311 }
312
313
314 static GLuint
315 create_shader_program(struct gl_context *ctx)
316 {
317 GLuint name;
318 struct gl_shader_program *shProg;
319
320 name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
321
322 shProg = ctx->Driver.NewShaderProgram(name);
323
324 _mesa_HashInsert(ctx->Shared->ShaderObjects, name, shProg);
325
326 assert(shProg->RefCount == 1);
327
328 return name;
329 }
330
331
332 /**
333 * Delete a shader program. Actually, just decrement the program's
334 * reference count and mark it as DeletePending.
335 * Used to implement glDeleteProgram() and glDeleteObjectARB().
336 */
337 static void
338 delete_shader_program(struct gl_context *ctx, GLuint name)
339 {
340 /*
341 * NOTE: deleting shaders/programs works a bit differently than
342 * texture objects (and buffer objects, etc). Shader/program
343 * handles/IDs exist in the hash table until the object is really
344 * deleted (refcount==0). With texture objects, the handle/ID is
345 * removed from the hash table in glDeleteTextures() while the tex
346 * object itself might linger until its refcount goes to zero.
347 */
348 struct gl_shader_program *shProg;
349
350 shProg = _mesa_lookup_shader_program_err(ctx, name, "glDeleteProgram");
351 if (!shProg)
352 return;
353
354 if (!shProg->DeletePending) {
355 shProg->DeletePending = GL_TRUE;
356
357 /* effectively, decr shProg's refcount */
358 _mesa_reference_shader_program(ctx, &shProg, NULL);
359 }
360 }
361
362
363 static void
364 delete_shader(struct gl_context *ctx, GLuint shader)
365 {
366 struct gl_shader *sh;
367
368 sh = _mesa_lookup_shader_err(ctx, shader, "glDeleteShader");
369 if (!sh)
370 return;
371
372 if (!sh->DeletePending) {
373 sh->DeletePending = GL_TRUE;
374
375 /* effectively, decr sh's refcount */
376 _mesa_reference_shader(ctx, &sh, NULL);
377 }
378 }
379
380
381 static void
382 detach_shader(struct gl_context *ctx, GLuint program, GLuint shader)
383 {
384 struct gl_shader_program *shProg;
385 GLuint n;
386 GLuint i, j;
387
388 shProg = _mesa_lookup_shader_program_err(ctx, program, "glDetachShader");
389 if (!shProg)
390 return;
391
392 n = shProg->NumShaders;
393
394 for (i = 0; i < n; i++) {
395 if (shProg->Shaders[i]->Name == shader) {
396 /* found it */
397 struct gl_shader **newList;
398
399 /* release */
400 _mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
401
402 /* alloc new, smaller array */
403 newList = malloc((n - 1) * sizeof(struct gl_shader *));
404 if (!newList) {
405 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glDetachShader");
406 return;
407 }
408 /* Copy old list entries to new list, skipping removed entry at [i] */
409 for (j = 0; j < i; j++) {
410 newList[j] = shProg->Shaders[j];
411 }
412 while (++i < n) {
413 newList[j++] = shProg->Shaders[i];
414 }
415
416 /* Free old list and install new one */
417 free(shProg->Shaders);
418 shProg->Shaders = newList;
419 shProg->NumShaders = n - 1;
420
421 #ifdef DEBUG
422 /* sanity check - make sure the new list's entries are sensible */
423 for (j = 0; j < shProg->NumShaders; j++) {
424 assert(shProg->Shaders[j]->Type == GL_VERTEX_SHADER ||
425 shProg->Shaders[j]->Type == GL_GEOMETRY_SHADER ||
426 shProg->Shaders[j]->Type == GL_FRAGMENT_SHADER);
427 assert(shProg->Shaders[j]->RefCount > 0);
428 }
429 #endif
430
431 return;
432 }
433 }
434
435 /* not found */
436 {
437 GLenum err;
438 if (is_shader(ctx, shader) || is_program(ctx, shader))
439 err = GL_INVALID_OPERATION;
440 else
441 err = GL_INVALID_VALUE;
442 _mesa_error(ctx, err, "glDetachShader(shader)");
443 return;
444 }
445 }
446
447
448 /**
449 * Return list of shaders attached to shader program.
450 */
451 static void
452 get_attached_shaders(struct gl_context *ctx, GLuint program, GLsizei maxCount,
453 GLsizei *count, GLuint *obj)
454 {
455 struct gl_shader_program *shProg;
456
457 if (maxCount < 0) {
458 _mesa_error(ctx, GL_INVALID_VALUE, "glGetAttachedShaders(maxCount < 0)");
459 return;
460 }
461
462 shProg =
463 _mesa_lookup_shader_program_err(ctx, program, "glGetAttachedShaders");
464
465 if (shProg) {
466 GLuint i;
467 for (i = 0; i < (GLuint) maxCount && i < shProg->NumShaders; i++) {
468 obj[i] = shProg->Shaders[i]->Name;
469 }
470 if (count)
471 *count = i;
472 }
473 }
474
475
476 /**
477 * glGetHandleARB() - return ID/name of currently bound shader program.
478 */
479 static GLuint
480 get_handle(struct gl_context *ctx, GLenum pname)
481 {
482 if (pname == GL_PROGRAM_OBJECT_ARB) {
483 if (ctx->_Shader->ActiveProgram)
484 return ctx->_Shader->ActiveProgram->Name;
485 else
486 return 0;
487 }
488 else {
489 _mesa_error(ctx, GL_INVALID_ENUM, "glGetHandleARB");
490 return 0;
491 }
492 }
493
494
495 /**
496 * Check if a geometry shader query is valid at this time. If not, report an
497 * error and return false.
498 *
499 * From GL 3.2 section 6.1.16 (Shader and Program Queries):
500 *
501 * "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
502 * are queried for a program which has not been linked successfully, or
503 * which does not contain objects to form a geometry shader, then an
504 * INVALID_OPERATION error is generated."
505 */
506 static bool
507 check_gs_query(struct gl_context *ctx, const struct gl_shader_program *shProg)
508 {
509 if (shProg->LinkStatus &&
510 shProg->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
511 return true;
512 }
513
514 _mesa_error(ctx, GL_INVALID_OPERATION,
515 "glGetProgramv(linked geometry shader required)");
516 return false;
517 }
518
519
520 /**
521 * glGetProgramiv() - get shader program state.
522 * Note that this is for GLSL shader programs, not ARB vertex/fragment
523 * programs (see glGetProgramivARB).
524 */
525 static void
526 get_programiv(struct gl_context *ctx, GLuint program, GLenum pname,
527 GLint *params)
528 {
529 struct gl_shader_program *shProg
530 = _mesa_lookup_shader_program_err(ctx, program, "glGetProgramiv(program)");
531
532 /* Is transform feedback available in this context?
533 */
534 const bool has_xfb =
535 (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.EXT_transform_feedback)
536 || ctx->API == API_OPENGL_CORE
537 || _mesa_is_gles3(ctx);
538
539 /* True if geometry shaders (of the form that was adopted into GLSL 1.50
540 * and GL 3.2) are available in this context
541 */
542 const bool has_core_gs = _mesa_has_geometry_shaders(ctx);
543
544 /* Are uniform buffer objects available in this context?
545 */
546 const bool has_ubo =
547 (ctx->API == API_OPENGL_COMPAT &&
548 ctx->Extensions.ARB_uniform_buffer_object)
549 || ctx->API == API_OPENGL_CORE
550 || _mesa_is_gles3(ctx);
551
552 if (!shProg) {
553 return;
554 }
555
556 switch (pname) {
557 case GL_DELETE_STATUS:
558 *params = shProg->DeletePending;
559 return;
560 case GL_LINK_STATUS:
561 *params = shProg->LinkStatus;
562 return;
563 case GL_VALIDATE_STATUS:
564 *params = shProg->Validated;
565 return;
566 case GL_INFO_LOG_LENGTH:
567 *params = shProg->InfoLog ? strlen(shProg->InfoLog) + 1 : 0;
568 return;
569 case GL_ATTACHED_SHADERS:
570 *params = shProg->NumShaders;
571 return;
572 case GL_ACTIVE_ATTRIBUTES:
573 *params = _mesa_count_active_attribs(shProg);
574 return;
575 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
576 *params = _mesa_longest_attribute_name_length(shProg);
577 return;
578 case GL_ACTIVE_UNIFORMS:
579 *params = shProg->NumUniformStorage - shProg->NumHiddenUniforms;
580 return;
581 case GL_ACTIVE_UNIFORM_MAX_LENGTH: {
582 unsigned i;
583 GLint max_len = 0;
584 const unsigned num_uniforms =
585 shProg->NumUniformStorage - shProg->NumHiddenUniforms;
586
587 for (i = 0; i < num_uniforms; i++) {
588 /* Add one for the terminating NUL character for a non-array, and
589 * 4 for the "[0]" and the NUL for an array.
590 */
591 const GLint len = strlen(shProg->UniformStorage[i].name) + 1 +
592 ((shProg->UniformStorage[i].array_elements != 0) ? 3 : 0);
593
594 if (len > max_len)
595 max_len = len;
596 }
597
598 *params = max_len;
599 return;
600 }
601 case GL_TRANSFORM_FEEDBACK_VARYINGS:
602 if (!has_xfb)
603 break;
604 *params = shProg->TransformFeedback.NumVarying;
605 return;
606 case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH: {
607 unsigned i;
608 GLint max_len = 0;
609 if (!has_xfb)
610 break;
611
612 for (i = 0; i < shProg->TransformFeedback.NumVarying; i++) {
613 /* Add one for the terminating NUL character.
614 */
615 const GLint len =
616 strlen(shProg->TransformFeedback.VaryingNames[i]) + 1;
617
618 if (len > max_len)
619 max_len = len;
620 }
621
622 *params = max_len;
623 return;
624 }
625 case GL_TRANSFORM_FEEDBACK_BUFFER_MODE:
626 if (!has_xfb)
627 break;
628 *params = shProg->TransformFeedback.BufferMode;
629 return;
630 case GL_GEOMETRY_VERTICES_OUT:
631 if (!has_core_gs)
632 break;
633 if (check_gs_query(ctx, shProg))
634 *params = shProg->Geom.VerticesOut;
635 return;
636 case GL_GEOMETRY_SHADER_INVOCATIONS:
637 if (!has_core_gs || !ctx->Extensions.ARB_gpu_shader5)
638 break;
639 if (check_gs_query(ctx, shProg))
640 *params = shProg->Geom.Invocations;
641 return;
642 case GL_GEOMETRY_INPUT_TYPE:
643 if (!has_core_gs)
644 break;
645 if (check_gs_query(ctx, shProg))
646 *params = shProg->Geom.InputType;
647 return;
648 case GL_GEOMETRY_OUTPUT_TYPE:
649 if (!has_core_gs)
650 break;
651 if (check_gs_query(ctx, shProg))
652 *params = shProg->Geom.OutputType;
653 return;
654 case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH: {
655 unsigned i;
656 GLint max_len = 0;
657
658 if (!has_ubo)
659 break;
660
661 for (i = 0; i < shProg->NumUniformBlocks; i++) {
662 /* Add one for the terminating NUL character.
663 */
664 const GLint len = strlen(shProg->UniformBlocks[i].Name) + 1;
665
666 if (len > max_len)
667 max_len = len;
668 }
669
670 *params = max_len;
671 return;
672 }
673 case GL_ACTIVE_UNIFORM_BLOCKS:
674 if (!has_ubo)
675 break;
676
677 *params = shProg->NumUniformBlocks;
678 return;
679 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT:
680 /* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
681 * only available with desktop OpenGL 3.0+ with the
682 * GL_ARB_get_program_binary extension or OpenGL ES 3.0.
683 *
684 * On desktop, we ignore the 3.0+ requirement because it is silly.
685 */
686 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
687 break;
688
689 *params = shProg->BinaryRetreivableHint;
690 return;
691 case GL_PROGRAM_BINARY_LENGTH:
692 *params = 0;
693 return;
694 case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS:
695 if (!ctx->Extensions.ARB_shader_atomic_counters)
696 break;
697
698 *params = shProg->NumAtomicBuffers;
699 return;
700 case GL_COMPUTE_WORK_GROUP_SIZE: {
701 int i;
702 if (!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_compute_shader)
703 break;
704 if (!shProg->LinkStatus) {
705 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramiv(program not "
706 "linked)");
707 return;
708 }
709 if (shProg->_LinkedShaders[MESA_SHADER_COMPUTE] == NULL) {
710 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramiv(no compute "
711 "shaders)");
712 return;
713 }
714 for (i = 0; i < 3; i++)
715 params[i] = shProg->Comp.LocalSize[i];
716 return;
717 }
718 case GL_PROGRAM_SEPARABLE:
719 *params = shProg->SeparateShader;
720 return;
721 default:
722 break;
723 }
724
725 _mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramiv(pname=%s)",
726 _mesa_enum_to_string(pname));
727 }
728
729
730 /**
731 * glGetShaderiv() - get GLSL shader state
732 */
733 static void
734 get_shaderiv(struct gl_context *ctx, GLuint name, GLenum pname, GLint *params)
735 {
736 struct gl_shader *shader =
737 _mesa_lookup_shader_err(ctx, name, "glGetShaderiv");
738
739 if (!shader) {
740 return;
741 }
742
743 switch (pname) {
744 case GL_SHADER_TYPE:
745 *params = shader->Type;
746 break;
747 case GL_DELETE_STATUS:
748 *params = shader->DeletePending;
749 break;
750 case GL_COMPILE_STATUS:
751 *params = shader->CompileStatus;
752 break;
753 case GL_INFO_LOG_LENGTH:
754 *params = shader->InfoLog ? strlen(shader->InfoLog) + 1 : 0;
755 break;
756 case GL_SHADER_SOURCE_LENGTH:
757 *params = shader->Source ? strlen((char *) shader->Source) + 1 : 0;
758 break;
759 default:
760 _mesa_error(ctx, GL_INVALID_ENUM, "glGetShaderiv(pname)");
761 return;
762 }
763 }
764
765
766 static void
767 get_program_info_log(struct gl_context *ctx, GLuint program, GLsizei bufSize,
768 GLsizei *length, GLchar *infoLog)
769 {
770 struct gl_shader_program *shProg;
771
772 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
773 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
774 *
775 * "If a negative number is provided where an argument of type sizei or
776 * sizeiptr is specified, an INVALID_VALUE error is generated."
777 */
778 if (bufSize < 0) {
779 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramInfoLog(bufSize < 0)");
780 return;
781 }
782
783 shProg = _mesa_lookup_shader_program_err(ctx, program,
784 "glGetProgramInfoLog(program)");
785 if (!shProg) {
786 return;
787 }
788
789 _mesa_copy_string(infoLog, bufSize, length, shProg->InfoLog);
790 }
791
792
793 static void
794 get_shader_info_log(struct gl_context *ctx, GLuint shader, GLsizei bufSize,
795 GLsizei *length, GLchar *infoLog)
796 {
797 struct gl_shader *sh;
798
799 /* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
800 * section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
801 *
802 * "If a negative number is provided where an argument of type sizei or
803 * sizeiptr is specified, an INVALID_VALUE error is generated."
804 */
805 if (bufSize < 0) {
806 _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderInfoLog(bufSize < 0)");
807 return;
808 }
809
810 sh = _mesa_lookup_shader_err(ctx, shader, "glGetShaderInfoLog(shader)");
811 if (!sh) {
812 return;
813 }
814
815 _mesa_copy_string(infoLog, bufSize, length, sh->InfoLog);
816 }
817
818
819 /**
820 * Return shader source code.
821 */
822 static void
823 get_shader_source(struct gl_context *ctx, GLuint shader, GLsizei maxLength,
824 GLsizei *length, GLchar *sourceOut)
825 {
826 struct gl_shader *sh;
827
828 if (maxLength < 0) {
829 _mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderSource(bufSize < 0)");
830 return;
831 }
832
833 sh = _mesa_lookup_shader_err(ctx, shader, "glGetShaderSource");
834 if (!sh) {
835 return;
836 }
837 _mesa_copy_string(sourceOut, maxLength, length, sh->Source);
838 }
839
840
841 /**
842 * Set/replace shader source code. A helper function used by
843 * glShaderSource[ARB].
844 */
845 static void
846 shader_source(struct gl_context *ctx, GLuint shader, const GLchar *source)
847 {
848 struct gl_shader *sh;
849
850 sh = _mesa_lookup_shader_err(ctx, shader, "glShaderSource");
851 if (!sh)
852 return;
853
854 /* free old shader source string and install new one */
855 free((void *)sh->Source);
856 sh->Source = source;
857 sh->CompileStatus = GL_FALSE;
858 #ifdef DEBUG
859 sh->SourceChecksum = _mesa_str_checksum(sh->Source);
860 #endif
861 }
862
863
864 /**
865 * Compile a shader.
866 */
867 static void
868 compile_shader(struct gl_context *ctx, GLuint shaderObj)
869 {
870 struct gl_shader *sh;
871
872 sh = _mesa_lookup_shader_err(ctx, shaderObj, "glCompileShader");
873 if (!sh)
874 return;
875
876 if (!sh->Source) {
877 /* If the user called glCompileShader without first calling
878 * glShaderSource, we should fail to compile, but not raise a GL_ERROR.
879 */
880 sh->CompileStatus = GL_FALSE;
881 } else {
882 if (ctx->_Shader->Flags & GLSL_DUMP) {
883 _mesa_log("GLSL source for %s shader %d:\n",
884 _mesa_shader_stage_to_string(sh->Stage), sh->Name);
885 _mesa_log("%s\n", sh->Source);
886 }
887
888 /* this call will set the shader->CompileStatus field to indicate if
889 * compilation was successful.
890 */
891 _mesa_glsl_compile_shader(ctx, sh, false, false);
892
893 if (ctx->_Shader->Flags & GLSL_LOG) {
894 _mesa_write_shader_to_file(sh);
895 }
896
897 if (ctx->_Shader->Flags & GLSL_DUMP) {
898 if (sh->CompileStatus) {
899 _mesa_log("GLSL IR for shader %d:\n", sh->Name);
900 _mesa_print_ir(_mesa_get_log_file(), sh->ir, NULL);
901 _mesa_log("\n\n");
902 } else {
903 _mesa_log("GLSL shader %d failed to compile.\n", sh->Name);
904 }
905 if (sh->InfoLog && sh->InfoLog[0] != 0) {
906 _mesa_log("GLSL shader %d info log:\n", sh->Name);
907 _mesa_log("%s\n", sh->InfoLog);
908 }
909 }
910 }
911
912 if (!sh->CompileStatus) {
913 if (ctx->_Shader->Flags & GLSL_DUMP_ON_ERROR) {
914 _mesa_log("GLSL source for %s shader %d:\n",
915 _mesa_shader_stage_to_string(sh->Stage), sh->Name);
916 _mesa_log("%s\n", sh->Source);
917 _mesa_log("Info Log:\n%s\n", sh->InfoLog);
918 }
919
920 if (ctx->_Shader->Flags & GLSL_REPORT_ERRORS) {
921 _mesa_debug(ctx, "Error compiling shader %u:\n%s\n",
922 sh->Name, sh->InfoLog);
923 }
924 }
925 }
926
927
928 /**
929 * Link a program's shaders.
930 */
931 static void
932 link_program(struct gl_context *ctx, GLuint program)
933 {
934 struct gl_shader_program *shProg;
935
936 shProg = _mesa_lookup_shader_program_err(ctx, program, "glLinkProgram");
937 if (!shProg)
938 return;
939
940 /* From the ARB_transform_feedback2 specification:
941 * "The error INVALID_OPERATION is generated by LinkProgram if <program> is
942 * the name of a program being used by one or more transform feedback
943 * objects, even if the objects are not currently bound or are paused."
944 */
945 if (_mesa_transform_feedback_is_using_program(ctx, shProg)) {
946 _mesa_error(ctx, GL_INVALID_OPERATION,
947 "glLinkProgram(transform feedback is using the program)");
948 return;
949 }
950
951 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
952
953 _mesa_glsl_link_shader(ctx, shProg);
954
955 if (shProg->LinkStatus == GL_FALSE &&
956 (ctx->_Shader->Flags & GLSL_REPORT_ERRORS)) {
957 _mesa_debug(ctx, "Error linking program %u:\n%s\n",
958 shProg->Name, shProg->InfoLog);
959 }
960
961 /* debug code */
962 if (0) {
963 GLuint i;
964
965 printf("Link %u shaders in program %u: %s\n",
966 shProg->NumShaders, shProg->Name,
967 shProg->LinkStatus ? "Success" : "Failed");
968
969 for (i = 0; i < shProg->NumShaders; i++) {
970 printf(" shader %u, type 0x%x\n",
971 shProg->Shaders[i]->Name,
972 shProg->Shaders[i]->Type);
973 }
974 }
975 }
976
977
978 /**
979 * Print basic shader info (for debug).
980 */
981 static void
982 print_shader_info(const struct gl_shader_program *shProg)
983 {
984 GLuint i;
985
986 printf("Mesa: glUseProgram(%u)\n", shProg->Name);
987 for (i = 0; i < shProg->NumShaders; i++) {
988 printf(" %s shader %u, checksum %u\n",
989 _mesa_shader_stage_to_string(shProg->Shaders[i]->Stage),
990 shProg->Shaders[i]->Name,
991 shProg->Shaders[i]->SourceChecksum);
992 }
993 if (shProg->_LinkedShaders[MESA_SHADER_VERTEX])
994 printf(" vert prog %u\n",
995 shProg->_LinkedShaders[MESA_SHADER_VERTEX]->Program->Id);
996 if (shProg->_LinkedShaders[MESA_SHADER_FRAGMENT])
997 printf(" frag prog %u\n",
998 shProg->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program->Id);
999 if (shProg->_LinkedShaders[MESA_SHADER_GEOMETRY])
1000 printf(" geom prog %u\n",
1001 shProg->_LinkedShaders[MESA_SHADER_GEOMETRY]->Program->Id);
1002 }
1003
1004
1005 /**
1006 * Use the named shader program for subsequent glUniform calls
1007 */
1008 void
1009 _mesa_active_program(struct gl_context *ctx, struct gl_shader_program *shProg,
1010 const char *caller)
1011 {
1012 if ((shProg != NULL) && !shProg->LinkStatus) {
1013 _mesa_error(ctx, GL_INVALID_OPERATION,
1014 "%s(program %u not linked)", caller, shProg->Name);
1015 return;
1016 }
1017
1018 if (ctx->Shader.ActiveProgram != shProg) {
1019 _mesa_reference_shader_program(ctx, &ctx->Shader.ActiveProgram, shProg);
1020 }
1021 }
1022
1023
1024 static void
1025 use_shader_program(struct gl_context *ctx, gl_shader_stage stage,
1026 struct gl_shader_program *shProg,
1027 struct gl_pipeline_object *shTarget)
1028 {
1029 struct gl_shader_program **target;
1030
1031 target = &shTarget->CurrentProgram[stage];
1032 if ((shProg != NULL) && (shProg->_LinkedShaders[stage] == NULL))
1033 shProg = NULL;
1034
1035 if (*target != shProg) {
1036 /* Program is current, flush it */
1037 if (shTarget == ctx->_Shader) {
1038 FLUSH_VERTICES(ctx, _NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS);
1039 }
1040
1041 /* If the shader is also bound as the current rendering shader, unbind
1042 * it from that binding point as well. This ensures that the correct
1043 * semantics of glDeleteProgram are maintained.
1044 */
1045 switch (stage) {
1046 case MESA_SHADER_VERTEX:
1047 /* Empty for now. */
1048 break;
1049 case MESA_SHADER_GEOMETRY:
1050 /* Empty for now. */
1051 break;
1052 case MESA_SHADER_COMPUTE:
1053 /* Empty for now. */
1054 break;
1055 case MESA_SHADER_FRAGMENT:
1056 if (*target == ctx->_Shader->_CurrentFragmentProgram) {
1057 _mesa_reference_shader_program(ctx,
1058 &ctx->_Shader->_CurrentFragmentProgram,
1059 NULL);
1060 }
1061 break;
1062 }
1063
1064 _mesa_reference_shader_program(ctx, target, shProg);
1065 return;
1066 }
1067 }
1068
1069
1070 /**
1071 * Use the named shader program for subsequent rendering.
1072 */
1073 void
1074 _mesa_use_program(struct gl_context *ctx, struct gl_shader_program *shProg)
1075 {
1076 int i;
1077 for (i = 0; i < MESA_SHADER_STAGES; i++)
1078 use_shader_program(ctx, i, shProg, &ctx->Shader);
1079 _mesa_active_program(ctx, shProg, "glUseProgram");
1080
1081 if (ctx->Driver.UseProgram)
1082 ctx->Driver.UseProgram(ctx, shProg);
1083 }
1084
1085
1086 /**
1087 * Do validation of the given shader program.
1088 * \param errMsg returns error message if validation fails.
1089 * \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
1090 */
1091 static GLboolean
1092 validate_shader_program(const struct gl_shader_program *shProg,
1093 char *errMsg)
1094 {
1095 if (!shProg->LinkStatus) {
1096 return GL_FALSE;
1097 }
1098
1099 /* From the GL spec, a program is invalid if any of these are true:
1100
1101 any two active samplers in the current program object are of
1102 different types, but refer to the same texture image unit,
1103
1104 any active sampler in the current program object refers to a texture
1105 image unit where fixed-function fragment processing accesses a
1106 texture target that does not match the sampler type, or
1107
1108 the sum of the number of active samplers in the program and the
1109 number of texture image units enabled for fixed-function fragment
1110 processing exceeds the combined limit on the total number of texture
1111 image units allowed.
1112 */
1113
1114 /*
1115 * Check: any two active samplers in the current program object are of
1116 * different types, but refer to the same texture image unit,
1117 */
1118 if (!_mesa_sampler_uniforms_are_valid(shProg, errMsg, 100))
1119 return GL_FALSE;
1120
1121 return GL_TRUE;
1122 }
1123
1124
1125 /**
1126 * Called via glValidateProgram()
1127 */
1128 static void
1129 validate_program(struct gl_context *ctx, GLuint program)
1130 {
1131 struct gl_shader_program *shProg;
1132 char errMsg[100] = "";
1133
1134 shProg = _mesa_lookup_shader_program_err(ctx, program, "glValidateProgram");
1135 if (!shProg) {
1136 return;
1137 }
1138
1139 shProg->Validated = validate_shader_program(shProg, errMsg);
1140 if (!shProg->Validated) {
1141 /* update info log */
1142 if (shProg->InfoLog) {
1143 ralloc_free(shProg->InfoLog);
1144 }
1145 shProg->InfoLog = ralloc_strdup(shProg, errMsg);
1146 }
1147 }
1148
1149
1150
1151 void GLAPIENTRY
1152 _mesa_AttachObjectARB(GLhandleARB program, GLhandleARB shader)
1153 {
1154 GET_CURRENT_CONTEXT(ctx);
1155 attach_shader(ctx, program, shader);
1156 }
1157
1158
1159 void GLAPIENTRY
1160 _mesa_AttachShader(GLuint program, GLuint shader)
1161 {
1162 GET_CURRENT_CONTEXT(ctx);
1163 attach_shader(ctx, program, shader);
1164 }
1165
1166
1167 void GLAPIENTRY
1168 _mesa_CompileShader(GLhandleARB shaderObj)
1169 {
1170 GET_CURRENT_CONTEXT(ctx);
1171 if (MESA_VERBOSE & VERBOSE_API)
1172 _mesa_debug(ctx, "glCompileShader %u\n", shaderObj);
1173 compile_shader(ctx, shaderObj);
1174 }
1175
1176
1177 GLuint GLAPIENTRY
1178 _mesa_CreateShader(GLenum type)
1179 {
1180 GET_CURRENT_CONTEXT(ctx);
1181 if (MESA_VERBOSE & VERBOSE_API)
1182 _mesa_debug(ctx, "glCreateShader %s\n", _mesa_enum_to_string(type));
1183 return create_shader(ctx, type);
1184 }
1185
1186
1187 GLhandleARB GLAPIENTRY
1188 _mesa_CreateShaderObjectARB(GLenum type)
1189 {
1190 GET_CURRENT_CONTEXT(ctx);
1191 return create_shader(ctx, type);
1192 }
1193
1194
1195 GLuint GLAPIENTRY
1196 _mesa_CreateProgram(void)
1197 {
1198 GET_CURRENT_CONTEXT(ctx);
1199 if (MESA_VERBOSE & VERBOSE_API)
1200 _mesa_debug(ctx, "glCreateProgram\n");
1201 return create_shader_program(ctx);
1202 }
1203
1204
1205 GLhandleARB GLAPIENTRY
1206 _mesa_CreateProgramObjectARB(void)
1207 {
1208 GET_CURRENT_CONTEXT(ctx);
1209 return create_shader_program(ctx);
1210 }
1211
1212
1213 void GLAPIENTRY
1214 _mesa_DeleteObjectARB(GLhandleARB obj)
1215 {
1216 if (MESA_VERBOSE & VERBOSE_API) {
1217 GET_CURRENT_CONTEXT(ctx);
1218 _mesa_debug(ctx, "glDeleteObjectARB(%u)\n", obj);
1219 }
1220
1221 if (obj) {
1222 GET_CURRENT_CONTEXT(ctx);
1223 FLUSH_VERTICES(ctx, 0);
1224 if (is_program(ctx, obj)) {
1225 delete_shader_program(ctx, obj);
1226 }
1227 else if (is_shader(ctx, obj)) {
1228 delete_shader(ctx, obj);
1229 }
1230 else {
1231 /* error? */
1232 }
1233 }
1234 }
1235
1236
1237 void GLAPIENTRY
1238 _mesa_DeleteProgram(GLuint name)
1239 {
1240 if (name) {
1241 GET_CURRENT_CONTEXT(ctx);
1242 FLUSH_VERTICES(ctx, 0);
1243 delete_shader_program(ctx, name);
1244 }
1245 }
1246
1247
1248 void GLAPIENTRY
1249 _mesa_DeleteShader(GLuint name)
1250 {
1251 if (name) {
1252 GET_CURRENT_CONTEXT(ctx);
1253 FLUSH_VERTICES(ctx, 0);
1254 delete_shader(ctx, name);
1255 }
1256 }
1257
1258
1259 void GLAPIENTRY
1260 _mesa_DetachObjectARB(GLhandleARB program, GLhandleARB shader)
1261 {
1262 GET_CURRENT_CONTEXT(ctx);
1263 detach_shader(ctx, program, shader);
1264 }
1265
1266
1267 void GLAPIENTRY
1268 _mesa_DetachShader(GLuint program, GLuint shader)
1269 {
1270 GET_CURRENT_CONTEXT(ctx);
1271 detach_shader(ctx, program, shader);
1272 }
1273
1274
1275 void GLAPIENTRY
1276 _mesa_GetAttachedObjectsARB(GLhandleARB container, GLsizei maxCount,
1277 GLsizei * count, GLhandleARB * obj)
1278 {
1279 GET_CURRENT_CONTEXT(ctx);
1280 get_attached_shaders(ctx, container, maxCount, count, obj);
1281 }
1282
1283
1284 void GLAPIENTRY
1285 _mesa_GetAttachedShaders(GLuint program, GLsizei maxCount,
1286 GLsizei *count, GLuint *obj)
1287 {
1288 GET_CURRENT_CONTEXT(ctx);
1289 get_attached_shaders(ctx, program, maxCount, count, obj);
1290 }
1291
1292
1293 void GLAPIENTRY
1294 _mesa_GetInfoLogARB(GLhandleARB object, GLsizei maxLength, GLsizei * length,
1295 GLcharARB * infoLog)
1296 {
1297 GET_CURRENT_CONTEXT(ctx);
1298 if (is_program(ctx, object)) {
1299 get_program_info_log(ctx, object, maxLength, length, infoLog);
1300 }
1301 else if (is_shader(ctx, object)) {
1302 get_shader_info_log(ctx, object, maxLength, length, infoLog);
1303 }
1304 else {
1305 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetInfoLogARB");
1306 }
1307 }
1308
1309
1310 void GLAPIENTRY
1311 _mesa_GetObjectParameterivARB(GLhandleARB object, GLenum pname, GLint *params)
1312 {
1313 GET_CURRENT_CONTEXT(ctx);
1314 /* Implement in terms of GetProgramiv, GetShaderiv */
1315 if (is_program(ctx, object)) {
1316 if (pname == GL_OBJECT_TYPE_ARB) {
1317 *params = GL_PROGRAM_OBJECT_ARB;
1318 }
1319 else {
1320 get_programiv(ctx, object, pname, params);
1321 }
1322 }
1323 else if (is_shader(ctx, object)) {
1324 if (pname == GL_OBJECT_TYPE_ARB) {
1325 *params = GL_SHADER_OBJECT_ARB;
1326 }
1327 else {
1328 get_shaderiv(ctx, object, pname, params);
1329 }
1330 }
1331 else {
1332 _mesa_error(ctx, GL_INVALID_VALUE, "glGetObjectParameterivARB");
1333 }
1334 }
1335
1336
1337 void GLAPIENTRY
1338 _mesa_GetObjectParameterfvARB(GLhandleARB object, GLenum pname,
1339 GLfloat *params)
1340 {
1341 GLint iparams[1]; /* XXX is one element enough? */
1342 _mesa_GetObjectParameterivARB(object, pname, iparams);
1343 params[0] = (GLfloat) iparams[0];
1344 }
1345
1346
1347 void GLAPIENTRY
1348 _mesa_GetProgramiv(GLuint program, GLenum pname, GLint *params)
1349 {
1350 GET_CURRENT_CONTEXT(ctx);
1351 get_programiv(ctx, program, pname, params);
1352 }
1353
1354
1355 void GLAPIENTRY
1356 _mesa_GetShaderiv(GLuint shader, GLenum pname, GLint *params)
1357 {
1358 GET_CURRENT_CONTEXT(ctx);
1359 get_shaderiv(ctx, shader, pname, params);
1360 }
1361
1362
1363 void GLAPIENTRY
1364 _mesa_GetProgramInfoLog(GLuint program, GLsizei bufSize,
1365 GLsizei *length, GLchar *infoLog)
1366 {
1367 GET_CURRENT_CONTEXT(ctx);
1368 get_program_info_log(ctx, program, bufSize, length, infoLog);
1369 }
1370
1371
1372 void GLAPIENTRY
1373 _mesa_GetShaderInfoLog(GLuint shader, GLsizei bufSize,
1374 GLsizei *length, GLchar *infoLog)
1375 {
1376 GET_CURRENT_CONTEXT(ctx);
1377 get_shader_info_log(ctx, shader, bufSize, length, infoLog);
1378 }
1379
1380
1381 void GLAPIENTRY
1382 _mesa_GetShaderSource(GLhandleARB shader, GLsizei maxLength,
1383 GLsizei *length, GLcharARB *sourceOut)
1384 {
1385 GET_CURRENT_CONTEXT(ctx);
1386 get_shader_source(ctx, shader, maxLength, length, sourceOut);
1387 }
1388
1389
1390 GLhandleARB GLAPIENTRY
1391 _mesa_GetHandleARB(GLenum pname)
1392 {
1393 GET_CURRENT_CONTEXT(ctx);
1394 return get_handle(ctx, pname);
1395 }
1396
1397
1398 GLboolean GLAPIENTRY
1399 _mesa_IsProgram(GLuint name)
1400 {
1401 GET_CURRENT_CONTEXT(ctx);
1402 return is_program(ctx, name);
1403 }
1404
1405
1406 GLboolean GLAPIENTRY
1407 _mesa_IsShader(GLuint name)
1408 {
1409 GET_CURRENT_CONTEXT(ctx);
1410 return is_shader(ctx, name);
1411 }
1412
1413
1414 void GLAPIENTRY
1415 _mesa_LinkProgram(GLhandleARB programObj)
1416 {
1417 GET_CURRENT_CONTEXT(ctx);
1418 link_program(ctx, programObj);
1419 }
1420
1421
1422
1423 /**
1424 * Read shader source code from a file.
1425 * Useful for debugging to override an app's shader.
1426 */
1427 static GLcharARB *
1428 read_shader(const char *fname)
1429 {
1430 int shader_size = 0;
1431 FILE *f = fopen(fname, "r");
1432 GLcharARB *buffer, *shader;
1433 int len;
1434
1435 if (!f) {
1436 return NULL;
1437 }
1438
1439 /* allocate enough room for the entire shader */
1440 fseek(f, 0, SEEK_END);
1441 shader_size = ftell(f);
1442 rewind(f);
1443 assert(shader_size);
1444
1445 /* add one for terminating zero */
1446 shader_size++;
1447
1448 buffer = malloc(shader_size);
1449 assert(buffer);
1450
1451 len = fread(buffer, 1, shader_size, f);
1452 buffer[len] = 0;
1453
1454 fclose(f);
1455
1456 shader = strdup(buffer);
1457 free(buffer);
1458
1459 return shader;
1460 }
1461
1462
1463 /**
1464 * Called via glShaderSource() and glShaderSourceARB() API functions.
1465 * Basically, concatenate the source code strings into one long string
1466 * and pass it to _mesa_shader_source().
1467 */
1468 void GLAPIENTRY
1469 _mesa_ShaderSource(GLhandleARB shaderObj, GLsizei count,
1470 const GLcharARB * const * string, const GLint * length)
1471 {
1472 GET_CURRENT_CONTEXT(ctx);
1473 GLint *offsets;
1474 GLsizei i, totalLength;
1475 GLcharARB *source;
1476 GLuint checksum;
1477
1478 if (!shaderObj || string == NULL) {
1479 _mesa_error(ctx, GL_INVALID_VALUE, "glShaderSourceARB");
1480 return;
1481 }
1482
1483 /*
1484 * This array holds offsets of where the appropriate string ends, thus the
1485 * last element will be set to the total length of the source code.
1486 */
1487 offsets = malloc(count * sizeof(GLint));
1488 if (offsets == NULL) {
1489 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glShaderSourceARB");
1490 return;
1491 }
1492
1493 for (i = 0; i < count; i++) {
1494 if (string[i] == NULL) {
1495 free((GLvoid *) offsets);
1496 _mesa_error(ctx, GL_INVALID_OPERATION,
1497 "glShaderSourceARB(null string)");
1498 return;
1499 }
1500 if (length == NULL || length[i] < 0)
1501 offsets[i] = strlen(string[i]);
1502 else
1503 offsets[i] = length[i];
1504 /* accumulate string lengths */
1505 if (i > 0)
1506 offsets[i] += offsets[i - 1];
1507 }
1508
1509 /* Total length of source string is sum off all strings plus two.
1510 * One extra byte for terminating zero, another extra byte to silence
1511 * valgrind warnings in the parser/grammer code.
1512 */
1513 totalLength = offsets[count - 1] + 2;
1514 source = malloc(totalLength * sizeof(GLcharARB));
1515 if (source == NULL) {
1516 free((GLvoid *) offsets);
1517 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glShaderSourceARB");
1518 return;
1519 }
1520
1521 for (i = 0; i < count; i++) {
1522 GLint start = (i > 0) ? offsets[i - 1] : 0;
1523 memcpy(source + start, string[i],
1524 (offsets[i] - start) * sizeof(GLcharARB));
1525 }
1526 source[totalLength - 1] = '\0';
1527 source[totalLength - 2] = '\0';
1528
1529 if (SHADER_SUBST) {
1530 /* Compute the shader's source code checksum then try to open a file
1531 * named newshader_<CHECKSUM>. If it exists, use it in place of the
1532 * original shader source code. For debugging.
1533 */
1534 char filename[100];
1535 GLcharARB *newSource;
1536
1537 checksum = _mesa_str_checksum(source);
1538
1539 _mesa_snprintf(filename, sizeof(filename), "newshader_%d", checksum);
1540
1541 newSource = read_shader(filename);
1542 if (newSource) {
1543 fprintf(stderr, "Mesa: Replacing shader %u chksum=%d with %s\n",
1544 shaderObj, checksum, filename);
1545 free(source);
1546 source = newSource;
1547 }
1548 }
1549
1550 shader_source(ctx, shaderObj, source);
1551
1552 if (SHADER_SUBST) {
1553 struct gl_shader *sh = _mesa_lookup_shader(ctx, shaderObj);
1554 if (sh)
1555 sh->SourceChecksum = checksum; /* save original checksum */
1556 }
1557
1558 free(offsets);
1559 }
1560
1561
1562 void GLAPIENTRY
1563 _mesa_UseProgram(GLhandleARB program)
1564 {
1565 GET_CURRENT_CONTEXT(ctx);
1566 struct gl_shader_program *shProg;
1567
1568 if (_mesa_is_xfb_active_and_unpaused(ctx)) {
1569 _mesa_error(ctx, GL_INVALID_OPERATION,
1570 "glUseProgram(transform feedback active)");
1571 return;
1572 }
1573
1574 if (program) {
1575 shProg = _mesa_lookup_shader_program_err(ctx, program, "glUseProgram");
1576 if (!shProg) {
1577 return;
1578 }
1579 if (!shProg->LinkStatus) {
1580 _mesa_error(ctx, GL_INVALID_OPERATION,
1581 "glUseProgram(program %u not linked)", program);
1582 return;
1583 }
1584
1585 /* debug code */
1586 if (ctx->_Shader->Flags & GLSL_USE_PROG) {
1587 print_shader_info(shProg);
1588 }
1589 }
1590 else {
1591 shProg = NULL;
1592 }
1593
1594 /* The ARB_separate_shader_object spec says:
1595 *
1596 * "The executable code for an individual shader stage is taken from
1597 * the current program for that stage. If there is a current program
1598 * object established by UseProgram, that program is considered current
1599 * for all stages. Otherwise, if there is a bound program pipeline
1600 * object (section 2.14.PPO), the program bound to the appropriate
1601 * stage of the pipeline object is considered current."
1602 */
1603 if (program) {
1604 /* Attach shader state to the binding point */
1605 _mesa_reference_pipeline_object(ctx, &ctx->_Shader, &ctx->Shader);
1606 /* Update the program */
1607 _mesa_use_program(ctx, shProg);
1608 } else {
1609 /* Must be done first: detach the progam */
1610 _mesa_use_program(ctx, shProg);
1611 /* Unattach shader_state binding point */
1612 _mesa_reference_pipeline_object(ctx, &ctx->_Shader, ctx->Pipeline.Default);
1613 /* If a pipeline was bound, rebind it */
1614 if (ctx->Pipeline.Current) {
1615 _mesa_BindProgramPipeline(ctx->Pipeline.Current->Name);
1616 }
1617 }
1618 }
1619
1620
1621 void GLAPIENTRY
1622 _mesa_ValidateProgram(GLhandleARB program)
1623 {
1624 GET_CURRENT_CONTEXT(ctx);
1625 validate_program(ctx, program);
1626 }
1627
1628
1629 /**
1630 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1631 */
1632 void GLAPIENTRY
1633 _mesa_GetShaderPrecisionFormat(GLenum shadertype, GLenum precisiontype,
1634 GLint* range, GLint* precision)
1635 {
1636 const struct gl_program_constants *limits;
1637 const struct gl_precision *p;
1638 GET_CURRENT_CONTEXT(ctx);
1639
1640 switch (shadertype) {
1641 case GL_VERTEX_SHADER:
1642 limits = &ctx->Const.Program[MESA_SHADER_VERTEX];
1643 break;
1644 case GL_FRAGMENT_SHADER:
1645 limits = &ctx->Const.Program[MESA_SHADER_FRAGMENT];
1646 break;
1647 default:
1648 _mesa_error(ctx, GL_INVALID_ENUM,
1649 "glGetShaderPrecisionFormat(shadertype)");
1650 return;
1651 }
1652
1653 switch (precisiontype) {
1654 case GL_LOW_FLOAT:
1655 p = &limits->LowFloat;
1656 break;
1657 case GL_MEDIUM_FLOAT:
1658 p = &limits->MediumFloat;
1659 break;
1660 case GL_HIGH_FLOAT:
1661 p = &limits->HighFloat;
1662 break;
1663 case GL_LOW_INT:
1664 p = &limits->LowInt;
1665 break;
1666 case GL_MEDIUM_INT:
1667 p = &limits->MediumInt;
1668 break;
1669 case GL_HIGH_INT:
1670 p = &limits->HighInt;
1671 break;
1672 default:
1673 _mesa_error(ctx, GL_INVALID_ENUM,
1674 "glGetShaderPrecisionFormat(precisiontype)");
1675 return;
1676 }
1677
1678 range[0] = p->RangeMin;
1679 range[1] = p->RangeMax;
1680 precision[0] = p->Precision;
1681 }
1682
1683
1684 /**
1685 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1686 */
1687 void GLAPIENTRY
1688 _mesa_ReleaseShaderCompiler(void)
1689 {
1690 _mesa_destroy_shader_compiler_caches();
1691 }
1692
1693
1694 /**
1695 * For OpenGL ES 2.0, GL_ARB_ES2_compatibility
1696 */
1697 void GLAPIENTRY
1698 _mesa_ShaderBinary(GLint n, const GLuint* shaders, GLenum binaryformat,
1699 const void* binary, GLint length)
1700 {
1701 GET_CURRENT_CONTEXT(ctx);
1702 (void) n;
1703 (void) shaders;
1704 (void) binaryformat;
1705 (void) binary;
1706 (void) length;
1707 _mesa_error(ctx, GL_INVALID_OPERATION, "glShaderBinary");
1708 }
1709
1710
1711 void GLAPIENTRY
1712 _mesa_GetProgramBinary(GLuint program, GLsizei bufSize, GLsizei *length,
1713 GLenum *binaryFormat, GLvoid *binary)
1714 {
1715 struct gl_shader_program *shProg;
1716 GLsizei length_dummy;
1717 GET_CURRENT_CONTEXT(ctx);
1718
1719 if (bufSize < 0){
1720 _mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramBinary(bufSize < 0)");
1721 return;
1722 }
1723
1724 shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetProgramBinary");
1725 if (!shProg)
1726 return;
1727
1728 /* The ARB_get_program_binary spec says:
1729 *
1730 * "If <length> is NULL, then no length is returned."
1731 *
1732 * Ensure that length always points to valid storage to avoid multiple NULL
1733 * pointer checks below.
1734 */
1735 if (length == NULL)
1736 length = &length_dummy;
1737
1738
1739 /* The ARB_get_program_binary spec says:
1740 *
1741 * "When a program object's LINK_STATUS is FALSE, its program binary
1742 * length is zero, and a call to GetProgramBinary will generate an
1743 * INVALID_OPERATION error.
1744 */
1745 if (!shProg->LinkStatus) {
1746 _mesa_error(ctx, GL_INVALID_OPERATION,
1747 "glGetProgramBinary(program %u not linked)",
1748 shProg->Name);
1749 *length = 0;
1750 return;
1751 }
1752
1753 *length = 0;
1754 _mesa_error(ctx, GL_INVALID_OPERATION,
1755 "glGetProgramBinary(driver supports zero binary formats)");
1756
1757 (void) binaryFormat;
1758 (void) binary;
1759 }
1760
1761 void GLAPIENTRY
1762 _mesa_ProgramBinary(GLuint program, GLenum binaryFormat,
1763 const GLvoid *binary, GLsizei length)
1764 {
1765 struct gl_shader_program *shProg;
1766 GET_CURRENT_CONTEXT(ctx);
1767
1768 shProg = _mesa_lookup_shader_program_err(ctx, program, "glProgramBinary");
1769 if (!shProg)
1770 return;
1771
1772 (void) binaryFormat;
1773 (void) binary;
1774
1775 /* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
1776 *
1777 * "If a negative number is provided where an argument of type sizei or
1778 * sizeiptr is specified, an INVALID_VALUE error is generated."
1779 */
1780 if (length < 0) {
1781 _mesa_error(ctx, GL_INVALID_VALUE, "glProgramBinary(length < 0)");
1782 return;
1783 }
1784
1785 /* The ARB_get_program_binary spec says:
1786 *
1787 * "<binaryFormat> and <binary> must be those returned by a previous
1788 * call to GetProgramBinary, and <length> must be the length of the
1789 * program binary as returned by GetProgramBinary or GetProgramiv with
1790 * <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
1791 * setting the LINK_STATUS of <program> to FALSE, if these conditions
1792 * are not met."
1793 *
1794 * Since any value of binaryFormat passed "is not one of those specified as
1795 * allowable for [this] command, an INVALID_ENUM error is generated."
1796 */
1797 shProg->LinkStatus = GL_FALSE;
1798 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramBinary");
1799 }
1800
1801
1802 void GLAPIENTRY
1803 _mesa_ProgramParameteri(GLuint program, GLenum pname, GLint value)
1804 {
1805 struct gl_shader_program *shProg;
1806 GET_CURRENT_CONTEXT(ctx);
1807
1808 shProg = _mesa_lookup_shader_program_err(ctx, program,
1809 "glProgramParameteri");
1810 if (!shProg)
1811 return;
1812
1813 switch (pname) {
1814 case GL_PROGRAM_BINARY_RETRIEVABLE_HINT:
1815 /* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
1816 * is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
1817 * even be in the dispatch table, so we shouldn't need to expclicitly
1818 * check here.
1819 *
1820 * On desktop, we ignore the 3.0+ requirement because it is silly.
1821 */
1822
1823 /* The ARB_get_program_binary extension spec says:
1824 *
1825 * "An INVALID_VALUE error is generated if the <value> argument to
1826 * ProgramParameteri is not TRUE or FALSE."
1827 */
1828 if (value != GL_TRUE && value != GL_FALSE) {
1829 goto invalid_value;
1830 }
1831
1832 /* No need to notify the driver. Any changes will actually take effect
1833 * the next time the shader is linked.
1834 *
1835 * The ARB_get_program_binary extension spec says:
1836 *
1837 * "To indicate that a program binary is likely to be retrieved,
1838 * ProgramParameteri should be called with <pname>
1839 * PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
1840 * will not be in effect until the next time LinkProgram or
1841 * ProgramBinary has been called successfully."
1842 *
1843 * The resloution of issue 9 in the extension spec also says:
1844 *
1845 * "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
1846 * to indicate to the GL implementation that this program will
1847 * likely be saved with GetProgramBinary at some point. This will
1848 * give the GL implementation the opportunity to track any state
1849 * changes made to the program before being saved such that when it
1850 * is loaded again a recompile can be avoided."
1851 */
1852 shProg->BinaryRetreivableHint = value;
1853 return;
1854
1855 case GL_PROGRAM_SEPARABLE:
1856 /* Spec imply that the behavior is the same as ARB_get_program_binary
1857 * Chapter 7.3 Program Objects
1858 */
1859 if (value != GL_TRUE && value != GL_FALSE) {
1860 goto invalid_value;
1861 }
1862 shProg->SeparateShader = value;
1863 return;
1864
1865 default:
1866 _mesa_error(ctx, GL_INVALID_ENUM, "glProgramParameteri(pname=%s)",
1867 _mesa_enum_to_string(pname));
1868 return;
1869 }
1870
1871 invalid_value:
1872 _mesa_error(ctx, GL_INVALID_VALUE,
1873 "glProgramParameteri(pname=%s, value=%d): "
1874 "value must be 0 or 1.",
1875 _mesa_enum_to_string(pname),
1876 value);
1877 }
1878
1879
1880 void
1881 _mesa_use_shader_program(struct gl_context *ctx, GLenum type,
1882 struct gl_shader_program *shProg,
1883 struct gl_pipeline_object *shTarget)
1884 {
1885 gl_shader_stage stage = _mesa_shader_enum_to_shader_stage(type);
1886 use_shader_program(ctx, stage, shProg, shTarget);
1887
1888 if (ctx->Driver.UseProgram)
1889 ctx->Driver.UseProgram(ctx, shProg);
1890 }
1891
1892
1893 static GLuint
1894 _mesa_create_shader_program(struct gl_context* ctx, GLboolean separate,
1895 GLenum type, GLsizei count, const GLchar* const *strings)
1896 {
1897 const GLuint shader = create_shader(ctx, type);
1898 GLuint program = 0;
1899
1900 if (shader) {
1901 _mesa_ShaderSource(shader, count, strings, NULL);
1902
1903 compile_shader(ctx, shader);
1904
1905 program = create_shader_program(ctx);
1906 if (program) {
1907 struct gl_shader_program *shProg;
1908 struct gl_shader *sh;
1909 GLint compiled = GL_FALSE;
1910
1911 shProg = _mesa_lookup_shader_program(ctx, program);
1912 sh = _mesa_lookup_shader(ctx, shader);
1913
1914 shProg->SeparateShader = separate;
1915
1916 get_shaderiv(ctx, shader, GL_COMPILE_STATUS, &compiled);
1917 if (compiled) {
1918 attach_shader(ctx, program, shader);
1919 link_program(ctx, program);
1920 detach_shader(ctx, program, shader);
1921
1922 #if 0
1923 /* Possibly... */
1924 if (active-user-defined-varyings-in-linked-program) {
1925 append-error-to-info-log;
1926 shProg->LinkStatus = GL_FALSE;
1927 }
1928 #endif
1929 }
1930
1931 ralloc_strcat(&shProg->InfoLog, sh->InfoLog);
1932 }
1933
1934 delete_shader(ctx, shader);
1935 }
1936
1937 return program;
1938 }
1939
1940
1941 /**
1942 * Copy program-specific data generated by linking from the gl_shader_program
1943 * object to a specific gl_program object.
1944 */
1945 void
1946 _mesa_copy_linked_program_data(gl_shader_stage type,
1947 const struct gl_shader_program *src,
1948 struct gl_program *dst)
1949 {
1950 switch (type) {
1951 case MESA_SHADER_VERTEX:
1952 dst->UsesClipDistanceOut = src->Vert.UsesClipDistance;
1953 break;
1954 case MESA_SHADER_GEOMETRY: {
1955 struct gl_geometry_program *dst_gp = (struct gl_geometry_program *) dst;
1956 dst_gp->VerticesIn = src->Geom.VerticesIn;
1957 dst_gp->VerticesOut = src->Geom.VerticesOut;
1958 dst_gp->Invocations = src->Geom.Invocations;
1959 dst_gp->InputType = src->Geom.InputType;
1960 dst_gp->OutputType = src->Geom.OutputType;
1961 dst->UsesClipDistanceOut = src->Geom.UsesClipDistance;
1962 dst_gp->UsesEndPrimitive = src->Geom.UsesEndPrimitive;
1963 dst_gp->UsesStreams = src->Geom.UsesStreams;
1964 }
1965 break;
1966 case MESA_SHADER_FRAGMENT: {
1967 struct gl_fragment_program *dst_fp = (struct gl_fragment_program *) dst;
1968 dst_fp->FragDepthLayout = src->FragDepthLayout;
1969 }
1970 break;
1971 case MESA_SHADER_COMPUTE: {
1972 struct gl_compute_program *dst_cp = (struct gl_compute_program *) dst;
1973 int i;
1974 for (i = 0; i < 3; i++)
1975 dst_cp->LocalSize[i] = src->Comp.LocalSize[i];
1976 }
1977 break;
1978 default:
1979 break;
1980 }
1981 }
1982
1983 /**
1984 * ARB_separate_shader_objects: Compile & Link Program
1985 */
1986 GLuint GLAPIENTRY
1987 _mesa_CreateShaderProgramv(GLenum type, GLsizei count,
1988 const GLchar* const *strings)
1989 {
1990 GET_CURRENT_CONTEXT(ctx);
1991
1992 return _mesa_create_shader_program(ctx, GL_TRUE, type, count, strings);
1993 }
1994
1995
1996 /**
1997 * For GL_ARB_tessellation_shader
1998 */
1999 extern void GLAPIENTRY
2000 _mesa_PatchParameteri(GLenum pname, GLint value)
2001 {
2002 GET_CURRENT_CONTEXT(ctx);
2003
2004 if (!_mesa_has_tessellation(ctx)) {
2005 _mesa_error(ctx, GL_INVALID_OPERATION, "glPatchParameteri");
2006 return;
2007 }
2008
2009 if (pname != GL_PATCH_VERTICES) {
2010 _mesa_error(ctx, GL_INVALID_ENUM, "glPatchParameteri");
2011 return;
2012 }
2013
2014 if (value <= 0 || value > ctx->Const.MaxPatchVertices) {
2015 _mesa_error(ctx, GL_INVALID_VALUE, "glPatchParameteri");
2016 return;
2017 }
2018
2019 ctx->TessCtrlProgram.patch_vertices = value;
2020 }
2021
2022
2023 extern void GLAPIENTRY
2024 _mesa_PatchParameterfv(GLenum pname, const GLfloat *values)
2025 {
2026 GET_CURRENT_CONTEXT(ctx);
2027
2028 if (!_mesa_has_tessellation(ctx)) {
2029 _mesa_error(ctx, GL_INVALID_OPERATION, "glPatchParameterfv");
2030 return;
2031 }
2032
2033 switch(pname) {
2034 case GL_PATCH_DEFAULT_OUTER_LEVEL:
2035 FLUSH_VERTICES(ctx, 0);
2036 memcpy(ctx->TessCtrlProgram.patch_default_outer_level, values,
2037 4 * sizeof(GLfloat));
2038 ctx->NewDriverState |= ctx->DriverFlags.NewDefaultTessLevels;
2039 return;
2040 case GL_PATCH_DEFAULT_INNER_LEVEL:
2041 FLUSH_VERTICES(ctx, 0);
2042 memcpy(ctx->TessCtrlProgram.patch_default_inner_level, values,
2043 2 * sizeof(GLfloat));
2044 ctx->NewDriverState |= ctx->DriverFlags.NewDefaultTessLevels;
2045 return;
2046 default:
2047 _mesa_error(ctx, GL_INVALID_ENUM, "glPatchParameterfv");
2048 return;
2049 }
2050 }
2051