2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
27 * Vertex and fragment program support functions.
32 #include "main/glheader.h"
33 #include "main/context.h"
34 #include "main/framebuffer.h"
35 #include "main/hash.h"
36 #include "main/macros.h"
38 #include "prog_cache.h"
39 #include "prog_parameter.h"
40 #include "prog_instruction.h"
41 #include "util/ralloc.h"
45 * A pointer to this dummy program is put into the hash table when
46 * glGenPrograms is called.
48 struct gl_program _mesa_DummyProgram
;
52 * Init context's vertex/fragment program state
55 _mesa_init_program(struct gl_context
*ctx
)
58 * If this assertion fails, we need to increase the field
59 * size for register indexes (see INST_INDEX_BITS).
61 assert(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxUniformComponents
/ 4
62 <= (1 << INST_INDEX_BITS
));
63 assert(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxUniformComponents
/ 4
64 <= (1 << INST_INDEX_BITS
));
66 assert(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTemps
<= (1 << INST_INDEX_BITS
));
67 assert(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxLocalParams
<= (1 << INST_INDEX_BITS
));
68 assert(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTemps
<= (1 << INST_INDEX_BITS
));
69 assert(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxLocalParams
<= (1 << INST_INDEX_BITS
));
71 assert(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxUniformComponents
<= 4 * MAX_UNIFORMS
);
72 assert(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxUniformComponents
<= 4 * MAX_UNIFORMS
);
74 assert(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAddressOffset
<= (1 << INST_INDEX_BITS
));
75 assert(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxAddressOffset
<= (1 << INST_INDEX_BITS
));
77 /* If this fails, increase prog_instruction::TexSrcUnit size */
78 STATIC_ASSERT(MAX_TEXTURE_UNITS
<= (1 << 5));
80 /* If this fails, increase prog_instruction::TexSrcTarget size */
81 STATIC_ASSERT(NUM_TEXTURE_TARGETS
<= (1 << 4));
83 ctx
->Program
.ErrorPos
= -1;
84 ctx
->Program
.ErrorString
= strdup("");
86 ctx
->VertexProgram
.Enabled
= GL_FALSE
;
87 ctx
->VertexProgram
.PointSizeEnabled
=
88 (ctx
->API
== API_OPENGLES2
) ? GL_TRUE
: GL_FALSE
;
89 ctx
->VertexProgram
.TwoSideEnabled
= GL_FALSE
;
90 _mesa_reference_program(ctx
, &ctx
->VertexProgram
.Current
,
91 ctx
->Shared
->DefaultVertexProgram
);
92 assert(ctx
->VertexProgram
.Current
);
93 ctx
->VertexProgram
.Cache
= _mesa_new_program_cache();
95 ctx
->FragmentProgram
.Enabled
= GL_FALSE
;
96 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
97 ctx
->Shared
->DefaultFragmentProgram
);
98 assert(ctx
->FragmentProgram
.Current
);
99 ctx
->FragmentProgram
.Cache
= _mesa_new_program_cache();
101 /* XXX probably move this stuff */
102 ctx
->ATIFragmentShader
.Enabled
= GL_FALSE
;
103 ctx
->ATIFragmentShader
.Current
= ctx
->Shared
->DefaultFragmentShader
;
104 assert(ctx
->ATIFragmentShader
.Current
);
105 ctx
->ATIFragmentShader
.Current
->RefCount
++;
110 * Free a context's vertex/fragment program state
113 _mesa_free_program_data(struct gl_context
*ctx
)
115 _mesa_reference_program(ctx
, &ctx
->VertexProgram
.Current
, NULL
);
116 _mesa_delete_program_cache(ctx
, ctx
->VertexProgram
.Cache
);
117 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
, NULL
);
118 _mesa_delete_shader_cache(ctx
, ctx
->FragmentProgram
.Cache
);
120 /* XXX probably move this stuff */
121 if (ctx
->ATIFragmentShader
.Current
) {
122 ctx
->ATIFragmentShader
.Current
->RefCount
--;
123 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
124 free(ctx
->ATIFragmentShader
.Current
);
128 free((void *) ctx
->Program
.ErrorString
);
133 * Update the default program objects in the given context to reference those
134 * specified in the shared state and release those referencing the old
138 _mesa_update_default_objects_program(struct gl_context
*ctx
)
140 _mesa_reference_program(ctx
, &ctx
->VertexProgram
.Current
,
141 ctx
->Shared
->DefaultVertexProgram
);
142 assert(ctx
->VertexProgram
.Current
);
144 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
145 ctx
->Shared
->DefaultFragmentProgram
);
146 assert(ctx
->FragmentProgram
.Current
);
148 /* XXX probably move this stuff */
149 if (ctx
->ATIFragmentShader
.Current
) {
150 ctx
->ATIFragmentShader
.Current
->RefCount
--;
151 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
152 free(ctx
->ATIFragmentShader
.Current
);
155 ctx
->ATIFragmentShader
.Current
= (struct ati_fragment_shader
*) ctx
->Shared
->DefaultFragmentShader
;
156 assert(ctx
->ATIFragmentShader
.Current
);
157 ctx
->ATIFragmentShader
.Current
->RefCount
++;
162 * Set the vertex/fragment program error state (position and error string).
163 * This is generally called from within the parsers.
166 _mesa_set_program_error(struct gl_context
*ctx
, GLint pos
, const char *string
)
168 ctx
->Program
.ErrorPos
= pos
;
169 free((void *) ctx
->Program
.ErrorString
);
172 ctx
->Program
.ErrorString
= strdup(string
);
177 * Initialize a new gl_program object.
180 _mesa_init_gl_program(struct gl_program
*prog
, GLenum target
, GLuint id
)
187 memset(prog
, 0, sizeof(*prog
));
188 mtx_init(&prog
->Mutex
, mtx_plain
);
190 prog
->Target
= target
;
192 prog
->Format
= GL_PROGRAM_FORMAT_ASCII_ARB
;
194 /* default mapping from samplers to texture units */
195 for (i
= 0; i
< MAX_SAMPLERS
; i
++)
196 prog
->SamplerUnits
[i
] = i
;
203 * Allocate and initialize a new fragment/vertex program object but
204 * don't put it into the program hash table. Called via
205 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
206 * device driver function to implement OO deriviation with additional
207 * types not understood by this function.
210 * \param id program id/number
211 * \param target program target/type
212 * \return pointer to new program object
215 _mesa_new_program(struct gl_context
*ctx
, GLenum target
, GLuint id
)
218 case GL_FRAGMENT_PROGRAM_ARB
: {
219 struct gl_fragment_program
*prog
= CALLOC_STRUCT(gl_fragment_program
);
220 return _mesa_init_gl_program(&prog
->Base
, target
, id
);
222 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
223 case GL_GEOMETRY_PROGRAM_NV
:
224 case GL_TESS_CONTROL_PROGRAM_NV
:
225 case GL_TESS_EVALUATION_PROGRAM_NV
:
226 case GL_COMPUTE_PROGRAM_NV
: {
227 struct gl_program
*prog
= CALLOC_STRUCT(gl_program
);
228 return _mesa_init_gl_program(prog
, target
, id
);
231 _mesa_problem(ctx
, "bad target in _mesa_new_program");
238 * Delete a program and remove it from the hash table, ignoring the
240 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
241 * by a device driver function.
244 _mesa_delete_program(struct gl_context
*ctx
, struct gl_program
*prog
)
248 assert(prog
->RefCount
==0);
250 if (prog
== &_mesa_DummyProgram
)
254 free(prog
->LocalParams
);
256 if (prog
->Instructions
) {
257 _mesa_free_instructions(prog
->Instructions
, prog
->NumInstructions
);
259 if (prog
->Parameters
) {
260 _mesa_free_parameter_list(prog
->Parameters
);
264 ralloc_free(prog
->nir
);
267 mtx_destroy(&prog
->Mutex
);
273 * Return the gl_program object for a given ID.
274 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
278 _mesa_lookup_program(struct gl_context
*ctx
, GLuint id
)
281 return (struct gl_program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
288 * Reference counting for vertex/fragment programs
289 * This is normally only called from the _mesa_reference_program() macro
290 * when there's a real pointer change.
293 _mesa_reference_program_(struct gl_context
*ctx
,
294 struct gl_program
**ptr
,
295 struct gl_program
*prog
)
301 if ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
)
302 assert(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
303 else if ((*ptr
)->Target
== GL_FRAGMENT_PROGRAM_ARB
)
304 assert(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
||
305 prog
->Target
== GL_FRAGMENT_PROGRAM_NV
);
306 else if ((*ptr
)->Target
== GL_GEOMETRY_PROGRAM_NV
)
307 assert(prog
->Target
== GL_GEOMETRY_PROGRAM_NV
);
312 GLboolean deleteFlag
;
313 struct gl_program
*oldProg
= *ptr
;
315 mtx_lock(&oldProg
->Mutex
);
316 assert(oldProg
->RefCount
> 0);
319 deleteFlag
= (oldProg
->RefCount
== 0);
320 mtx_unlock(&oldProg
->Mutex
);
324 ctx
->Driver
.DeleteProgram(ctx
, oldProg
);
332 mtx_lock(&prog
->Mutex
);
334 mtx_unlock(&prog
->Mutex
);
342 * Insert 'count' NOP instructions at 'start' in the given program.
343 * Adjust branch targets accordingly.
346 _mesa_insert_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
348 const GLuint origLen
= prog
->NumInstructions
;
349 const GLuint newLen
= origLen
+ count
;
350 struct prog_instruction
*newInst
;
353 /* adjust branches */
354 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
355 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
356 if (inst
->BranchTarget
> 0) {
357 if ((GLuint
)inst
->BranchTarget
>= start
) {
358 inst
->BranchTarget
+= count
;
363 /* Alloc storage for new instructions */
364 newInst
= _mesa_alloc_instructions(newLen
);
369 /* Copy 'start' instructions into new instruction buffer */
370 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
372 /* init the new instructions */
373 _mesa_init_instructions(newInst
+ start
, count
);
375 /* Copy the remaining/tail instructions to new inst buffer */
376 _mesa_copy_instructions(newInst
+ start
+ count
,
377 prog
->Instructions
+ start
,
380 /* free old instructions */
381 _mesa_free_instructions(prog
->Instructions
, origLen
);
383 /* install new instructions */
384 prog
->Instructions
= newInst
;
385 prog
->NumInstructions
= newLen
;
391 * Delete 'count' instructions at 'start' in the given program.
392 * Adjust branch targets accordingly.
395 _mesa_delete_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
397 const GLuint origLen
= prog
->NumInstructions
;
398 const GLuint newLen
= origLen
- count
;
399 struct prog_instruction
*newInst
;
402 /* adjust branches */
403 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
404 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
405 if (inst
->BranchTarget
> 0) {
406 if (inst
->BranchTarget
> (GLint
) start
) {
407 inst
->BranchTarget
-= count
;
412 /* Alloc storage for new instructions */
413 newInst
= _mesa_alloc_instructions(newLen
);
418 /* Copy 'start' instructions into new instruction buffer */
419 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
421 /* Copy the remaining/tail instructions to new inst buffer */
422 _mesa_copy_instructions(newInst
+ start
,
423 prog
->Instructions
+ start
+ count
,
426 /* free old instructions */
427 _mesa_free_instructions(prog
->Instructions
, origLen
);
429 /* install new instructions */
430 prog
->Instructions
= newInst
;
431 prog
->NumInstructions
= newLen
;
438 * Populate the 'used' array with flags indicating which registers (TEMPs,
439 * INPUTs, OUTPUTs, etc, are used by the given program.
440 * \param file type of register to scan for
441 * \param used returns true/false flags for in use / free
442 * \param usedSize size of the 'used' array
445 _mesa_find_used_registers(const struct gl_program
*prog
,
446 gl_register_file file
,
447 GLboolean used
[], GLuint usedSize
)
451 memset(used
, 0, usedSize
);
453 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
454 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
455 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
457 if (inst
->DstReg
.File
== file
) {
458 assert(inst
->DstReg
.Index
< usedSize
);
459 if(inst
->DstReg
.Index
< usedSize
)
460 used
[inst
->DstReg
.Index
] = GL_TRUE
;
463 for (j
= 0; j
< n
; j
++) {
464 if (inst
->SrcReg
[j
].File
== file
) {
465 assert(inst
->SrcReg
[j
].Index
< (GLint
) usedSize
);
466 if (inst
->SrcReg
[j
].Index
< (GLint
) usedSize
)
467 used
[inst
->SrcReg
[j
].Index
] = GL_TRUE
;
475 * Scan the given 'used' register flag array for the first entry
476 * that's >= firstReg.
477 * \param used vector of flags indicating registers in use (as returned
478 * by _mesa_find_used_registers())
479 * \param usedSize size of the 'used' array
480 * \param firstReg first register to start searching at
481 * \return index of unused register, or -1 if none.
484 _mesa_find_free_register(const GLboolean used
[],
485 GLuint usedSize
, GLuint firstReg
)
489 assert(firstReg
< usedSize
);
491 for (i
= firstReg
; i
< usedSize
; i
++)
499 /* Gets the minimum number of shader invocations per fragment.
500 * This function is useful to determine if we need to do per
501 * sample shading or per fragment shading.
504 _mesa_get_min_invocations_per_fragment(struct gl_context
*ctx
,
505 const struct gl_fragment_program
*prog
,
506 bool ignore_sample_qualifier
)
508 /* From ARB_sample_shading specification:
509 * "Using gl_SampleID in a fragment shader causes the entire shader
510 * to be evaluated per-sample."
512 * "Using gl_SamplePosition in a fragment shader causes the entire
513 * shader to be evaluated per-sample."
515 * "If MULTISAMPLE or SAMPLE_SHADING_ARB is disabled, sample shading
518 if (ctx
->Multisample
.Enabled
) {
519 /* The ARB_gpu_shader5 specification says:
521 * "Use of the "sample" qualifier on a fragment shader input
522 * forces per-sample shading"
524 if (prog
->Base
.info
.fs
.uses_sample_qualifier
&& !ignore_sample_qualifier
)
525 return MAX2(_mesa_geometric_samples(ctx
->DrawBuffer
), 1);
527 if (prog
->Base
.SystemValuesRead
& (SYSTEM_BIT_SAMPLE_ID
|
528 SYSTEM_BIT_SAMPLE_POS
))
529 return MAX2(_mesa_geometric_samples(ctx
->DrawBuffer
), 1);
530 else if (ctx
->Multisample
.SampleShading
)
531 return MAX2(ceil(ctx
->Multisample
.MinSampleShadingValue
*
532 _mesa_geometric_samples(ctx
->DrawBuffer
)), 1);