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/hash.h"
35 #include "main/macros.h"
37 #include "prog_cache.h"
38 #include "prog_parameter.h"
39 #include "prog_instruction.h"
40 #include "util/ralloc.h"
44 * A pointer to this dummy program is put into the hash table when
45 * glGenPrograms is called.
47 struct gl_program _mesa_DummyProgram
;
51 * Init context's vertex/fragment program state
54 _mesa_init_program(struct gl_context
*ctx
)
57 * If this assertion fails, we need to increase the field
58 * size for register indexes (see INST_INDEX_BITS).
60 assert(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxUniformComponents
/ 4
61 <= (1 << INST_INDEX_BITS
));
62 assert(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxUniformComponents
/ 4
63 <= (1 << INST_INDEX_BITS
));
65 assert(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxTemps
<= (1 << INST_INDEX_BITS
));
66 assert(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxLocalParams
<= (1 << INST_INDEX_BITS
));
67 assert(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxTemps
<= (1 << INST_INDEX_BITS
));
68 assert(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxLocalParams
<= (1 << INST_INDEX_BITS
));
70 assert(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxUniformComponents
<= 4 * MAX_UNIFORMS
);
71 assert(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxUniformComponents
<= 4 * MAX_UNIFORMS
);
73 assert(ctx
->Const
.Program
[MESA_SHADER_VERTEX
].MaxAddressOffset
<= (1 << INST_INDEX_BITS
));
74 assert(ctx
->Const
.Program
[MESA_SHADER_FRAGMENT
].MaxAddressOffset
<= (1 << INST_INDEX_BITS
));
76 /* If this fails, increase prog_instruction::TexSrcUnit size */
77 STATIC_ASSERT(MAX_TEXTURE_UNITS
<= (1 << 5));
79 /* If this fails, increase prog_instruction::TexSrcTarget size */
80 STATIC_ASSERT(NUM_TEXTURE_TARGETS
<= (1 << 4));
82 ctx
->Program
.ErrorPos
= -1;
83 ctx
->Program
.ErrorString
= strdup("");
85 ctx
->VertexProgram
.Enabled
= GL_FALSE
;
86 ctx
->VertexProgram
.PointSizeEnabled
=
87 (ctx
->API
== API_OPENGLES2
) ? GL_TRUE
: GL_FALSE
;
88 ctx
->VertexProgram
.TwoSideEnabled
= GL_FALSE
;
89 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
90 ctx
->Shared
->DefaultVertexProgram
);
91 assert(ctx
->VertexProgram
.Current
);
92 ctx
->VertexProgram
.Cache
= _mesa_new_program_cache();
94 ctx
->FragmentProgram
.Enabled
= GL_FALSE
;
95 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
96 ctx
->Shared
->DefaultFragmentProgram
);
97 assert(ctx
->FragmentProgram
.Current
);
98 ctx
->FragmentProgram
.Cache
= _mesa_new_program_cache();
100 /* XXX probably move this stuff */
101 ctx
->ATIFragmentShader
.Enabled
= GL_FALSE
;
102 ctx
->ATIFragmentShader
.Current
= ctx
->Shared
->DefaultFragmentShader
;
103 assert(ctx
->ATIFragmentShader
.Current
);
104 ctx
->ATIFragmentShader
.Current
->RefCount
++;
109 * Free a context's vertex/fragment program state
112 _mesa_free_program_data(struct gl_context
*ctx
)
114 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
, NULL
);
115 _mesa_delete_program_cache(ctx
, ctx
->VertexProgram
.Cache
);
116 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
, NULL
);
117 _mesa_delete_shader_cache(ctx
, ctx
->FragmentProgram
.Cache
);
119 /* XXX probably move this stuff */
120 if (ctx
->ATIFragmentShader
.Current
) {
121 ctx
->ATIFragmentShader
.Current
->RefCount
--;
122 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
123 free(ctx
->ATIFragmentShader
.Current
);
127 free((void *) ctx
->Program
.ErrorString
);
132 * Update the default program objects in the given context to reference those
133 * specified in the shared state and release those referencing the old
137 _mesa_update_default_objects_program(struct gl_context
*ctx
)
139 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
.Current
,
140 ctx
->Shared
->DefaultVertexProgram
);
141 assert(ctx
->VertexProgram
.Current
);
143 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
.Current
,
144 ctx
->Shared
->DefaultFragmentProgram
);
145 assert(ctx
->FragmentProgram
.Current
);
147 /* XXX probably move this stuff */
148 if (ctx
->ATIFragmentShader
.Current
) {
149 ctx
->ATIFragmentShader
.Current
->RefCount
--;
150 if (ctx
->ATIFragmentShader
.Current
->RefCount
<= 0) {
151 free(ctx
->ATIFragmentShader
.Current
);
154 ctx
->ATIFragmentShader
.Current
= (struct ati_fragment_shader
*) ctx
->Shared
->DefaultFragmentShader
;
155 assert(ctx
->ATIFragmentShader
.Current
);
156 ctx
->ATIFragmentShader
.Current
->RefCount
++;
161 * Set the vertex/fragment program error state (position and error string).
162 * This is generally called from within the parsers.
165 _mesa_set_program_error(struct gl_context
*ctx
, GLint pos
, const char *string
)
167 ctx
->Program
.ErrorPos
= pos
;
168 free((void *) ctx
->Program
.ErrorString
);
171 ctx
->Program
.ErrorString
= strdup(string
);
176 * Initialize a new gl_program object.
179 _mesa_init_gl_program(void *_prog
, GLenum target
, GLuint id
)
181 struct gl_program
*prog
= (struct gl_program
*)_prog
;
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
)
217 struct gl_program
*prog
;
219 case GL_VERTEX_PROGRAM_ARB
: /* == GL_VERTEX_PROGRAM_NV */
220 prog
= _mesa_init_gl_program(CALLOC_STRUCT(gl_vertex_program
),
223 case GL_FRAGMENT_PROGRAM_NV
:
224 case GL_FRAGMENT_PROGRAM_ARB
:
225 prog
=_mesa_init_gl_program(CALLOC_STRUCT(gl_fragment_program
),
228 case GL_GEOMETRY_PROGRAM_NV
:
229 prog
= _mesa_init_gl_program(CALLOC_STRUCT(gl_geometry_program
),
232 case GL_TESS_CONTROL_PROGRAM_NV
:
233 prog
= _mesa_init_gl_program(CALLOC_STRUCT(gl_tess_ctrl_program
),
236 case GL_TESS_EVALUATION_PROGRAM_NV
:
237 prog
= _mesa_init_gl_program(CALLOC_STRUCT(gl_tess_eval_program
),
240 case GL_COMPUTE_PROGRAM_NV
:
241 prog
= _mesa_init_gl_program(CALLOC_STRUCT(gl_compute_program
),
245 _mesa_problem(ctx
, "bad target in _mesa_new_program");
253 * Delete a program and remove it from the hash table, ignoring the
255 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
256 * by a device driver function.
259 _mesa_delete_program(struct gl_context
*ctx
, struct gl_program
*prog
)
263 assert(prog
->RefCount
==0);
265 if (prog
== &_mesa_DummyProgram
)
269 free(prog
->LocalParams
);
271 if (prog
->Instructions
) {
272 _mesa_free_instructions(prog
->Instructions
, prog
->NumInstructions
);
274 if (prog
->Parameters
) {
275 _mesa_free_parameter_list(prog
->Parameters
);
279 ralloc_free(prog
->nir
);
282 mtx_destroy(&prog
->Mutex
);
288 * Return the gl_program object for a given ID.
289 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
293 _mesa_lookup_program(struct gl_context
*ctx
, GLuint id
)
296 return (struct gl_program
*) _mesa_HashLookup(ctx
->Shared
->Programs
, id
);
303 * Reference counting for vertex/fragment programs
304 * This is normally only called from the _mesa_reference_program() macro
305 * when there's a real pointer change.
308 _mesa_reference_program_(struct gl_context
*ctx
,
309 struct gl_program
**ptr
,
310 struct gl_program
*prog
)
316 if ((*ptr
)->Target
== GL_VERTEX_PROGRAM_ARB
)
317 assert(prog
->Target
== GL_VERTEX_PROGRAM_ARB
);
318 else if ((*ptr
)->Target
== GL_FRAGMENT_PROGRAM_ARB
)
319 assert(prog
->Target
== GL_FRAGMENT_PROGRAM_ARB
||
320 prog
->Target
== GL_FRAGMENT_PROGRAM_NV
);
321 else if ((*ptr
)->Target
== GL_GEOMETRY_PROGRAM_NV
)
322 assert(prog
->Target
== GL_GEOMETRY_PROGRAM_NV
);
327 GLboolean deleteFlag
;
328 struct gl_program
*oldProg
= *ptr
;
330 mtx_lock(&oldProg
->Mutex
);
331 assert(oldProg
->RefCount
> 0);
334 deleteFlag
= (oldProg
->RefCount
== 0);
335 mtx_unlock(&oldProg
->Mutex
);
339 ctx
->Driver
.DeleteProgram(ctx
, oldProg
);
347 mtx_lock(&prog
->Mutex
);
349 mtx_unlock(&prog
->Mutex
);
357 * Insert 'count' NOP instructions at 'start' in the given program.
358 * Adjust branch targets accordingly.
361 _mesa_insert_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
363 const GLuint origLen
= prog
->NumInstructions
;
364 const GLuint newLen
= origLen
+ count
;
365 struct prog_instruction
*newInst
;
368 /* adjust branches */
369 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
370 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
371 if (inst
->BranchTarget
> 0) {
372 if ((GLuint
)inst
->BranchTarget
>= start
) {
373 inst
->BranchTarget
+= count
;
378 /* Alloc storage for new instructions */
379 newInst
= _mesa_alloc_instructions(newLen
);
384 /* Copy 'start' instructions into new instruction buffer */
385 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
387 /* init the new instructions */
388 _mesa_init_instructions(newInst
+ start
, count
);
390 /* Copy the remaining/tail instructions to new inst buffer */
391 _mesa_copy_instructions(newInst
+ start
+ count
,
392 prog
->Instructions
+ start
,
395 /* free old instructions */
396 _mesa_free_instructions(prog
->Instructions
, origLen
);
398 /* install new instructions */
399 prog
->Instructions
= newInst
;
400 prog
->NumInstructions
= newLen
;
406 * Delete 'count' instructions at 'start' in the given program.
407 * Adjust branch targets accordingly.
410 _mesa_delete_instructions(struct gl_program
*prog
, GLuint start
, GLuint count
)
412 const GLuint origLen
= prog
->NumInstructions
;
413 const GLuint newLen
= origLen
- count
;
414 struct prog_instruction
*newInst
;
417 /* adjust branches */
418 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
419 struct prog_instruction
*inst
= prog
->Instructions
+ i
;
420 if (inst
->BranchTarget
> 0) {
421 if (inst
->BranchTarget
> (GLint
) start
) {
422 inst
->BranchTarget
-= count
;
427 /* Alloc storage for new instructions */
428 newInst
= _mesa_alloc_instructions(newLen
);
433 /* Copy 'start' instructions into new instruction buffer */
434 _mesa_copy_instructions(newInst
, prog
->Instructions
, start
);
436 /* Copy the remaining/tail instructions to new inst buffer */
437 _mesa_copy_instructions(newInst
+ start
,
438 prog
->Instructions
+ start
+ count
,
441 /* free old instructions */
442 _mesa_free_instructions(prog
->Instructions
, origLen
);
444 /* install new instructions */
445 prog
->Instructions
= newInst
;
446 prog
->NumInstructions
= newLen
;
453 * Search instructions for registers that match (oldFile, oldIndex),
454 * replacing them with (newFile, newIndex).
457 replace_registers(struct prog_instruction
*inst
, GLuint numInst
,
458 GLuint oldFile
, GLuint oldIndex
,
459 GLuint newFile
, GLuint newIndex
)
462 for (i
= 0; i
< numInst
; i
++) {
464 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
[i
].Opcode
); j
++) {
465 if (inst
[i
].SrcReg
[j
].File
== oldFile
&&
466 inst
[i
].SrcReg
[j
].Index
== oldIndex
) {
467 inst
[i
].SrcReg
[j
].File
= newFile
;
468 inst
[i
].SrcReg
[j
].Index
= newIndex
;
472 if (inst
[i
].DstReg
.File
== oldFile
&& inst
[i
].DstReg
.Index
== oldIndex
) {
473 inst
[i
].DstReg
.File
= newFile
;
474 inst
[i
].DstReg
.Index
= newIndex
;
481 * Search instructions for references to program parameters. When found,
482 * increment the parameter index by 'offset'.
483 * Used when combining programs.
486 adjust_param_indexes(struct prog_instruction
*inst
, GLuint numInst
,
490 for (i
= 0; i
< numInst
; i
++) {
491 for (j
= 0; j
< _mesa_num_inst_src_regs(inst
[i
].Opcode
); j
++) {
492 GLuint f
= inst
[i
].SrcReg
[j
].File
;
493 if (f
== PROGRAM_CONSTANT
||
494 f
== PROGRAM_UNIFORM
||
495 f
== PROGRAM_STATE_VAR
) {
496 inst
[i
].SrcReg
[j
].Index
+= offset
;
504 * Populate the 'used' array with flags indicating which registers (TEMPs,
505 * INPUTs, OUTPUTs, etc, are used by the given program.
506 * \param file type of register to scan for
507 * \param used returns true/false flags for in use / free
508 * \param usedSize size of the 'used' array
511 _mesa_find_used_registers(const struct gl_program
*prog
,
512 gl_register_file file
,
513 GLboolean used
[], GLuint usedSize
)
517 memset(used
, 0, usedSize
);
519 for (i
= 0; i
< prog
->NumInstructions
; i
++) {
520 const struct prog_instruction
*inst
= prog
->Instructions
+ i
;
521 const GLuint n
= _mesa_num_inst_src_regs(inst
->Opcode
);
523 if (inst
->DstReg
.File
== file
) {
524 assert(inst
->DstReg
.Index
< usedSize
);
525 if(inst
->DstReg
.Index
< usedSize
)
526 used
[inst
->DstReg
.Index
] = GL_TRUE
;
529 for (j
= 0; j
< n
; j
++) {
530 if (inst
->SrcReg
[j
].File
== file
) {
531 assert(inst
->SrcReg
[j
].Index
< (GLint
) usedSize
);
532 if (inst
->SrcReg
[j
].Index
< (GLint
) usedSize
)
533 used
[inst
->SrcReg
[j
].Index
] = GL_TRUE
;
541 * Scan the given 'used' register flag array for the first entry
542 * that's >= firstReg.
543 * \param used vector of flags indicating registers in use (as returned
544 * by _mesa_find_used_registers())
545 * \param usedSize size of the 'used' array
546 * \param firstReg first register to start searching at
547 * \return index of unused register, or -1 if none.
550 _mesa_find_free_register(const GLboolean used
[],
551 GLuint usedSize
, GLuint firstReg
)
555 assert(firstReg
< usedSize
);
557 for (i
= firstReg
; i
< usedSize
; i
++)
565 /* Gets the minimum number of shader invocations per fragment.
566 * This function is useful to determine if we need to do per
567 * sample shading or per fragment shading.
570 _mesa_get_min_invocations_per_fragment(struct gl_context
*ctx
,
571 const struct gl_fragment_program
*prog
,
572 bool ignore_sample_qualifier
)
574 /* From ARB_sample_shading specification:
575 * "Using gl_SampleID in a fragment shader causes the entire shader
576 * to be evaluated per-sample."
578 * "Using gl_SamplePosition in a fragment shader causes the entire
579 * shader to be evaluated per-sample."
581 * "If MULTISAMPLE or SAMPLE_SHADING_ARB is disabled, sample shading
584 if (ctx
->Multisample
.Enabled
) {
585 /* The ARB_gpu_shader5 specification says:
587 * "Use of the "sample" qualifier on a fragment shader input
588 * forces per-sample shading"
590 if (prog
->IsSample
&& !ignore_sample_qualifier
)
591 return MAX2(ctx
->DrawBuffer
->Visual
.samples
, 1);
593 if (prog
->Base
.SystemValuesRead
& (SYSTEM_BIT_SAMPLE_ID
|
594 SYSTEM_BIT_SAMPLE_POS
))
595 return MAX2(ctx
->DrawBuffer
->Visual
.samples
, 1);
596 else if (ctx
->Multisample
.SampleShading
)
597 return MAX2(ceil(ctx
->Multisample
.MinSampleShadingValue
*
598 ctx
->DrawBuffer
->Visual
.samples
), 1);