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"
37 #include "main/shaderobj.h"
39 #include "prog_cache.h"
40 #include "prog_parameter.h"
41 #include "prog_instruction.h"
42 #include "util/ralloc.h"
46 * A pointer to this dummy program is put into the hash table when
47 * glGenPrograms is called.
49 struct gl_program _mesa_DummyProgram
;
53 * Init context's vertex/fragment program state
56 _mesa_init_program(struct gl_context
*ctx
)
59 * If this assertion fails, we need to increase the field
60 * size for register indexes (see INST_INDEX_BITS).
62 assert(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxUniformComponents
/ 4
63 <= (1 << INST_INDEX_BITS
));
64 assert(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxUniformComponents
/ 4
65 <= (1 << INST_INDEX_BITS
));
67 assert(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTemps
<= (1 << INST_INDEX_BITS
));
68 assert(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxLocalParams
<= (1 << INST_INDEX_BITS
));
69 assert(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTemps
<= (1 << INST_INDEX_BITS
));
70 assert(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxLocalParams
<= (1 << INST_INDEX_BITS
));
72 assert(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxUniformComponents
<= 4 * MAX_UNIFORMS
);
73 assert(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxUniformComponents
<= 4 * MAX_UNIFORMS
);
75 assert(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAddressOffset
<= (1 << INST_INDEX_BITS
));
76 assert(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxAddressOffset
<= (1 << INST_INDEX_BITS
));
78 /* If this fails, increase prog_instruction::TexSrcUnit size */
79 STATIC_ASSERT(MAX_TEXTURE_UNITS
<= (1 << 5));
81 /* If this fails, increase prog_instruction::TexSrcTarget size */
82 STATIC_ASSERT(NUM_TEXTURE_TARGETS
<= (1 << 4));
84 ctx
->Program
.ErrorPos
= -1;
85 ctx
->Program
.ErrorString
= strdup("");
87 ctx
->VertexProgram
.Enabled
= GL_FALSE
;
88 ctx
->VertexProgram
.PointSizeEnabled
=
89 (ctx
->API
== API_OPENGLES2
) ? GL_TRUE
: GL_FALSE
;
90 ctx
->VertexProgram
.TwoSideEnabled
= GL_FALSE
;
91 _mesa_reference_program(ctx
, &ctx
->VertexProgram
.Current
,
92 ctx
->Shared
->DefaultVertexProgram
);
93 assert(ctx
->VertexProgram
.Current
);
94 ctx
->VertexProgram
.Cache
= _mesa_new_program_cache();
96 ctx
->FragmentProgram
.Enabled
= GL_FALSE
;
97 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
.Current
,
98 ctx
->Shared
->DefaultFragmentProgram
);
99 assert(ctx
->FragmentProgram
.Current
);
100 ctx
->FragmentProgram
.Cache
= _mesa_new_program_cache();
102 /* XXX probably move this stuff */
103 ctx
->ATIFragmentShader
.Enabled
= GL_FALSE
;
104 ctx
->ATIFragmentShader
.Current
= ctx
->Shared
->DefaultFragmentShader
;
105 assert(ctx
->ATIFragmentShader
.Current
);
106 ctx
->ATIFragmentShader
.Current
->RefCount
++;
111 * Free a context's vertex/fragment program state
114 _mesa_free_program_data(struct gl_context
*ctx
)
116 _mesa_reference_program(ctx
, &ctx
->VertexProgram
.Current
, NULL
);
117 _mesa_delete_program_cache(ctx
, ctx
->VertexProgram
.Cache
);
118 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
.Current
, NULL
);
119 _mesa_delete_shader_cache(ctx
, ctx
->FragmentProgram
.Cache
);
121 /* XXX probably move this stuff */
122 if (ctx
->ATIFragmentShader
.Current
) {
123 ctx
->ATIFragmentShader
.Current
->RefCount
--;
124 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
125 free(ctx
->ATIFragmentShader
.Current
);
129 free((void *) ctx
->Program
.ErrorString
);
134 * Update the default program objects in the given context to reference those
135 * specified in the shared state and release those referencing the old
139 _mesa_update_default_objects_program(struct gl_context
*ctx
)
141 _mesa_reference_program(ctx
, &ctx
->VertexProgram
.Current
,
142 ctx
->Shared
->DefaultVertexProgram
);
143 assert(ctx
->VertexProgram
.Current
);
145 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
.Current
,
146 ctx
->Shared
->DefaultFragmentProgram
);
147 assert(ctx
->FragmentProgram
.Current
);
149 /* XXX probably move this stuff */
150 if (ctx
->ATIFragmentShader
.Current
) {
151 ctx
->ATIFragmentShader
.Current
->RefCount
--;
152 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
153 free(ctx
->ATIFragmentShader
.Current
);
156 ctx
->ATIFragmentShader
.Current
= (struct ati_fragment_shader
*) ctx
->Shared
->DefaultFragmentShader
;
157 assert(ctx
->ATIFragmentShader
.Current
);
158 ctx
->ATIFragmentShader
.Current
->RefCount
++;
163 * Set the vertex/fragment program error state (position and error string).
164 * This is generally called from within the parsers.
167 _mesa_set_program_error(struct gl_context
*ctx
, GLint pos
, const char *string
)
169 ctx
->Program
.ErrorPos
= pos
;
170 free((void *) ctx
->Program
.ErrorString
);
173 ctx
->Program
.ErrorString
= strdup(string
);
178 * Initialize a new gl_program object.
181 _mesa_init_gl_program(struct gl_program
*prog
, GLenum target
, GLuint id
)
188 memset(prog
, 0, sizeof(*prog
));
189 mtx_init(&prog
->Mutex
, mtx_plain
);
191 prog
->Target
= target
;
193 prog
->Format
= GL_PROGRAM_FORMAT_ASCII_ARB
;
194 prog
->info
.stage
= _mesa_program_enum_to_shader_stage(target
);
196 /* default mapping from samplers to texture units */
197 for (i
= 0; i
< MAX_SAMPLERS
; i
++)
198 prog
->SamplerUnits
[i
] = i
;
205 * Allocate and initialize a new fragment/vertex program object but
206 * don't put it into the program hash table. Called via
207 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
208 * device driver function to implement OO deriviation with additional
209 * types not understood by this function.
212 * \param id program id/number
213 * \param target program target/type
214 * \return pointer to new program object
217 _mesa_new_program(struct gl_context
*ctx
, GLenum target
, GLuint id
)
220 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
221 case GL_GEOMETRY_PROGRAM_NV
:
222 case GL_TESS_CONTROL_PROGRAM_NV
:
223 case GL_TESS_EVALUATION_PROGRAM_NV
:
224 case GL_FRAGMENT_PROGRAM_ARB
:
225 case GL_COMPUTE_PROGRAM_NV
: {
226 struct gl_program
*prog
= rzalloc(NULL
, struct gl_program
);
227 return _mesa_init_gl_program(prog
, target
, id
);
230 _mesa_problem(ctx
, "bad target in _mesa_new_program");
237 * Delete a program and remove it from the hash table, ignoring the
239 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
240 * by a device driver function.
243 _mesa_delete_program(struct gl_context
*ctx
, struct gl_program
*prog
)
247 assert(prog
->RefCount
==0);
249 if (prog
== &_mesa_DummyProgram
)
252 if (prog
->Parameters
) {
253 _mesa_free_parameter_list(prog
->Parameters
);
257 ralloc_free(prog
->nir
);
260 mtx_destroy(&prog
->Mutex
);
266 * Return the gl_program object for a given ID.
267 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
271 _mesa_lookup_program(struct gl_context
*ctx
, GLuint id
)
274 return (struct gl_program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
281 * Reference counting for vertex/fragment programs
282 * This is normally only called from the _mesa_reference_program() macro
283 * when there's a real pointer change.
286 _mesa_reference_program_(struct gl_context
*ctx
,
287 struct gl_program
**ptr
,
288 struct gl_program
*prog
)
294 if ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
)
295 assert(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
296 else if ((*ptr
)->Target
== GL_FRAGMENT_PROGRAM_ARB
)
297 assert(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
||
298 prog
->Target
== GL_FRAGMENT_PROGRAM_NV
);
299 else if ((*ptr
)->Target
== GL_GEOMETRY_PROGRAM_NV
)
300 assert(prog
->Target
== GL_GEOMETRY_PROGRAM_NV
);
305 GLboolean deleteFlag
;
306 struct gl_program
*oldProg
= *ptr
;
308 mtx_lock(&oldProg
->Mutex
);
309 assert(oldProg
->RefCount
> 0);
312 deleteFlag
= (oldProg
->RefCount
== 0);
313 mtx_unlock(&oldProg
->Mutex
);
317 _mesa_reference_shader_program_data(ctx
, &oldProg
->sh
.data
, NULL
);
318 ctx
->Driver
.DeleteProgram(ctx
, oldProg
);
326 mtx_lock(&prog
->Mutex
);
328 mtx_unlock(&prog
->Mutex
);
336 * Insert 'count' NOP instructions at 'start' in the given program.
337 * Adjust branch targets accordingly.
340 _mesa_insert_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
342 const GLuint origLen
= prog
->arb
.NumInstructions
;
343 const GLuint newLen
= origLen
+ count
;
344 struct prog_instruction
*newInst
;
347 /* adjust branches */
348 for (i
= 0; i
< prog
->arb
.NumInstructions
; i
++) {
349 struct prog_instruction
*inst
= prog
->arb
.Instructions
+ i
;
350 if (inst
->BranchTarget
> 0) {
351 if ((GLuint
)inst
->BranchTarget
>= start
) {
352 inst
->BranchTarget
+= count
;
357 /* Alloc storage for new instructions */
358 newInst
= rzalloc_array(prog
, struct prog_instruction
, newLen
);
363 /* Copy 'start' instructions into new instruction buffer */
364 _mesa_copy_instructions(newInst
, prog
->arb
.Instructions
, start
);
366 /* init the new instructions */
367 _mesa_init_instructions(newInst
+ start
, count
);
369 /* Copy the remaining/tail instructions to new inst buffer */
370 _mesa_copy_instructions(newInst
+ start
+ count
,
371 prog
->arb
.Instructions
+ start
,
374 /* free old instructions */
375 ralloc_free(prog
->arb
.Instructions
);
377 /* install new instructions */
378 prog
->arb
.Instructions
= newInst
;
379 prog
->arb
.NumInstructions
= newLen
;
385 * Delete 'count' instructions at 'start' in the given program.
386 * Adjust branch targets accordingly.
389 _mesa_delete_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
,
392 const GLuint origLen
= prog
->arb
.NumInstructions
;
393 const GLuint newLen
= origLen
- count
;
394 struct prog_instruction
*newInst
;
397 /* adjust branches */
398 for (i
= 0; i
< prog
->arb
.NumInstructions
; i
++) {
399 struct prog_instruction
*inst
= prog
->arb
.Instructions
+ i
;
400 if (inst
->BranchTarget
> 0) {
401 if (inst
->BranchTarget
> (GLint
) start
) {
402 inst
->BranchTarget
-= count
;
407 /* Alloc storage for new instructions */
408 newInst
= rzalloc_array(mem_ctx
, struct prog_instruction
, newLen
);
413 /* Copy 'start' instructions into new instruction buffer */
414 _mesa_copy_instructions(newInst
, prog
->arb
.Instructions
, start
);
416 /* Copy the remaining/tail instructions to new inst buffer */
417 _mesa_copy_instructions(newInst
+ start
,
418 prog
->arb
.Instructions
+ start
+ count
,
421 /* free old instructions */
422 ralloc_free(prog
->arb
.Instructions
);
424 /* install new instructions */
425 prog
->arb
.Instructions
= newInst
;
426 prog
->arb
.NumInstructions
= newLen
;
433 * Populate the 'used' array with flags indicating which registers (TEMPs,
434 * INPUTs, OUTPUTs, etc, are used by the given program.
435 * \param file type of register to scan for
436 * \param used returns true/false flags for in use / free
437 * \param usedSize size of the 'used' array
440 _mesa_find_used_registers(const struct gl_program
*prog
,
441 gl_register_file file
,
442 GLboolean used
[], GLuint usedSize
)
446 memset(used
, 0, usedSize
);
448 for (i
= 0; i
< prog
->arb
.NumInstructions
; i
++) {
449 const struct prog_instruction
*inst
= prog
->arb
.Instructions
+ i
;
450 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
452 if (inst
->DstReg
.File
== file
) {
453 assert(inst
->DstReg
.Index
< usedSize
);
454 if(inst
->DstReg
.Index
< usedSize
)
455 used
[inst
->DstReg
.Index
] = GL_TRUE
;
458 for (j
= 0; j
< n
; j
++) {
459 if (inst
->SrcReg
[j
].File
== file
) {
460 assert(inst
->SrcReg
[j
].Index
< (GLint
) usedSize
);
461 if (inst
->SrcReg
[j
].Index
< (GLint
) usedSize
)
462 used
[inst
->SrcReg
[j
].Index
] = GL_TRUE
;
470 * Scan the given 'used' register flag array for the first entry
471 * that's >= firstReg.
472 * \param used vector of flags indicating registers in use (as returned
473 * by _mesa_find_used_registers())
474 * \param usedSize size of the 'used' array
475 * \param firstReg first register to start searching at
476 * \return index of unused register, or -1 if none.
479 _mesa_find_free_register(const GLboolean used
[],
480 GLuint usedSize
, GLuint firstReg
)
484 assert(firstReg
< usedSize
);
486 for (i
= firstReg
; i
< usedSize
; i
++)
494 /* Gets the minimum number of shader invocations per fragment.
495 * This function is useful to determine if we need to do per
496 * sample shading or per fragment shading.
499 _mesa_get_min_invocations_per_fragment(struct gl_context
*ctx
,
500 const struct gl_program
*prog
,
501 bool ignore_sample_qualifier
)
503 /* From ARB_sample_shading specification:
504 * "Using gl_SampleID in a fragment shader causes the entire shader
505 * to be evaluated per-sample."
507 * "Using gl_SamplePosition in a fragment shader causes the entire
508 * shader to be evaluated per-sample."
510 * "If MULTISAMPLE or SAMPLE_SHADING_ARB is disabled, sample shading
513 if (ctx
->Multisample
.Enabled
) {
514 /* The ARB_gpu_shader5 specification says:
516 * "Use of the "sample" qualifier on a fragment shader input
517 * forces per-sample shading"
519 if (prog
->info
.fs
.uses_sample_qualifier
&& !ignore_sample_qualifier
)
520 return MAX2(_mesa_geometric_samples(ctx
->DrawBuffer
), 1);
522 if (prog
->info
.system_values_read
& (SYSTEM_BIT_SAMPLE_ID
|
523 SYSTEM_BIT_SAMPLE_POS
))
524 return MAX2(_mesa_geometric_samples(ctx
->DrawBuffer
), 1);
525 else if (ctx
->Multisample
.SampleShading
)
526 return MAX2(ceil(ctx
->Multisample
.MinSampleShadingValue
*
527 _mesa_geometric_samples(ctx
->DrawBuffer
)), 1);