1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
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.
23  */
24 
25 /**
26  * \file program.c
27  * Vertex and fragment program support functions.
28  * \author Brian Paul
29  */
30 
31 
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"
38 #include "program.h"
39 #include "prog_cache.h"
40 #include "prog_parameter.h"
41 #include "prog_instruction.h"
42 #include "util/bitscan.h"
43 #include "util/ralloc.h"
44 #include "util/u_atomic.h"
45 
46 
47 /**
48  * A pointer to this dummy program is put into the hash table when
49  * glGenPrograms is called.
50  */
51 struct gl_program _mesa_DummyProgram;
52 
53 
54 /**
55  * Init context's vertex/fragment program state
56  */
57 void
_mesa_init_program(struct gl_context * ctx)58 _mesa_init_program(struct gl_context *ctx)
59 {
60    /*
61     * If this assertion fails, we need to increase the field
62     * size for register indexes (see INST_INDEX_BITS).
63     */
64    assert(ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents / 4
65           <= (1 << INST_INDEX_BITS));
66    assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents / 4
67           <= (1 << INST_INDEX_BITS));
68 
69    assert(ctx->Const.Program[MESA_SHADER_VERTEX].MaxTemps <= (1 << INST_INDEX_BITS));
70    assert(ctx->Const.Program[MESA_SHADER_VERTEX].MaxLocalParams <= (1 << INST_INDEX_BITS));
71    assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTemps <= (1 << INST_INDEX_BITS));
72    assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxLocalParams <= (1 << INST_INDEX_BITS));
73 
74    assert(ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents <= 4 * MAX_UNIFORMS);
75    assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents <= 4 * MAX_UNIFORMS);
76 
77    assert(ctx->Const.Program[MESA_SHADER_VERTEX].MaxAddressOffset <= (1 << INST_INDEX_BITS));
78    assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxAddressOffset <= (1 << INST_INDEX_BITS));
79 
80    /* If this fails, increase prog_instruction::TexSrcUnit size */
81    STATIC_ASSERT(MAX_TEXTURE_UNITS <= (1 << 5));
82 
83    /* If this fails, increase prog_instruction::TexSrcTarget size */
84    STATIC_ASSERT(NUM_TEXTURE_TARGETS <= (1 << 4));
85 
86    ctx->Program.ErrorPos = -1;
87    ctx->Program.ErrorString = strdup("");
88 
89    ctx->VertexProgram.Enabled = GL_FALSE;
90    ctx->VertexProgram.PointSizeEnabled =
91       (ctx->API == API_OPENGLES2) ? GL_TRUE : GL_FALSE;
92    ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
93    _mesa_reference_program(ctx, &ctx->VertexProgram.Current,
94                            ctx->Shared->DefaultVertexProgram);
95    assert(ctx->VertexProgram.Current);
96    ctx->VertexProgram.Cache = _mesa_new_program_cache();
97 
98    ctx->FragmentProgram.Enabled = GL_FALSE;
99    _mesa_reference_program(ctx, &ctx->FragmentProgram.Current,
100                            ctx->Shared->DefaultFragmentProgram);
101    assert(ctx->FragmentProgram.Current);
102    ctx->FragmentProgram.Cache = _mesa_new_program_cache();
103    ctx->VertexProgram._VPMode = VP_MODE_FF;
104 
105    /* XXX probably move this stuff */
106    ctx->ATIFragmentShader.Enabled = GL_FALSE;
107    ctx->ATIFragmentShader.Current = ctx->Shared->DefaultFragmentShader;
108    assert(ctx->ATIFragmentShader.Current);
109    ctx->ATIFragmentShader.Current->RefCount++;
110 }
111 
112 
113 /**
114  * Free a context's vertex/fragment program state
115  */
116 void
_mesa_free_program_data(struct gl_context * ctx)117 _mesa_free_program_data(struct gl_context *ctx)
118 {
119    _mesa_reference_program(ctx, &ctx->VertexProgram.Current, NULL);
120    _mesa_delete_program_cache(ctx, ctx->VertexProgram.Cache);
121    _mesa_reference_program(ctx, &ctx->FragmentProgram.Current, NULL);
122    _mesa_delete_shader_cache(ctx, ctx->FragmentProgram.Cache);
123 
124    /* XXX probably move this stuff */
125    if (ctx->ATIFragmentShader.Current) {
126       ctx->ATIFragmentShader.Current->RefCount--;
127       if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
128          free(ctx->ATIFragmentShader.Current);
129       }
130    }
131 
132    free((void *) ctx->Program.ErrorString);
133 }
134 
135 
136 /**
137  * Update the default program objects in the given context to reference those
138  * specified in the shared state and release those referencing the old
139  * shared state.
140  */
141 void
_mesa_update_default_objects_program(struct gl_context * ctx)142 _mesa_update_default_objects_program(struct gl_context *ctx)
143 {
144    _mesa_reference_program(ctx, &ctx->VertexProgram.Current,
145                            ctx->Shared->DefaultVertexProgram);
146    assert(ctx->VertexProgram.Current);
147 
148    _mesa_reference_program(ctx, &ctx->FragmentProgram.Current,
149                             ctx->Shared->DefaultFragmentProgram);
150    assert(ctx->FragmentProgram.Current);
151 
152    /* XXX probably move this stuff */
153    if (ctx->ATIFragmentShader.Current) {
154       ctx->ATIFragmentShader.Current->RefCount--;
155       if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
156          free(ctx->ATIFragmentShader.Current);
157       }
158    }
159    ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
160    assert(ctx->ATIFragmentShader.Current);
161    ctx->ATIFragmentShader.Current->RefCount++;
162 }
163 
164 
165 /**
166  * Set the vertex/fragment program error state (position and error string).
167  * This is generally called from within the parsers.
168  */
169 void
_mesa_set_program_error(struct gl_context * ctx,GLint pos,const char * string)170 _mesa_set_program_error(struct gl_context *ctx, GLint pos, const char *string)
171 {
172    ctx->Program.ErrorPos = pos;
173    free((void *) ctx->Program.ErrorString);
174    if (!string)
175       string = "";
176    ctx->Program.ErrorString = strdup(string);
177 }
178 
179 
180 /**
181  * Initialize a new gl_program object.
182  */
183 struct gl_program *
_mesa_init_gl_program(struct gl_program * prog,gl_shader_stage stage,GLuint id,bool is_arb_asm)184 _mesa_init_gl_program(struct gl_program *prog, gl_shader_stage stage,
185                       GLuint id, bool is_arb_asm)
186 {
187    if (!prog)
188       return NULL;
189 
190    memset(prog, 0, sizeof(*prog));
191    prog->Id = id;
192    prog->Target = _mesa_shader_stage_to_program(stage);
193    prog->RefCount = 1;
194    prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
195    prog->info.stage = stage;
196    prog->is_arb_asm = is_arb_asm;
197 
198    /* Uniforms that lack an initializer in the shader code have an initial
199     * value of zero.  This includes sampler uniforms.
200     *
201     * Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
202     *
203     *     "The link time initial value is either the value of the variable's
204     *     initializer, if present, or 0 if no initializer is present. Sampler
205     *     types cannot have initializers."
206     *
207     * So we only initialise ARB assembly style programs.
208     */
209    if (is_arb_asm) {
210       /* default mapping from samplers to texture units */
211       for (unsigned i = 0; i < MAX_SAMPLERS; i++)
212          prog->SamplerUnits[i] = i;
213    }
214 
215    return prog;
216 }
217 
218 
219 /**
220  * Allocate and initialize a new fragment/vertex program object but
221  * don't put it into the program hash table.  Called via
222  * ctx->Driver.NewProgram.  May be overridden (ie. replaced) by a
223  * device driver function to implement OO deriviation with additional
224  * types not understood by this function.
225  *
226  * \param ctx  context
227  * \param id   program id/number
228  * \param stage  shader stage
229  * \return  pointer to new program object
230  */
231 struct gl_program *
_mesa_new_program(struct gl_context * ctx,gl_shader_stage stage,GLuint id,bool is_arb_asm)232 _mesa_new_program(struct gl_context *ctx, gl_shader_stage stage, GLuint id,
233                   bool is_arb_asm)
234 {
235    struct gl_program *prog = rzalloc(NULL, struct gl_program);
236 
237    return _mesa_init_gl_program(prog, stage, id, is_arb_asm);
238 }
239 
240 
241 /**
242  * Delete a program and remove it from the hash table, ignoring the
243  * reference count.
244  * Called via ctx->Driver.DeleteProgram.  May be wrapped (OO deriviation)
245  * by a device driver function.
246  */
247 void
_mesa_delete_program(struct gl_context * ctx,struct gl_program * prog)248 _mesa_delete_program(struct gl_context *ctx, struct gl_program *prog)
249 {
250    (void) ctx;
251    assert(prog);
252    assert(prog->RefCount==0);
253 
254    if (prog == &_mesa_DummyProgram)
255       return;
256 
257    if (prog->Parameters) {
258       _mesa_free_parameter_list(prog->Parameters);
259    }
260 
261    if (prog->nir) {
262       ralloc_free(prog->nir);
263    }
264 
265    if (prog->sh.BindlessSamplers) {
266       ralloc_free(prog->sh.BindlessSamplers);
267    }
268 
269    if (prog->sh.BindlessImages) {
270       ralloc_free(prog->sh.BindlessImages);
271    }
272 
273    if (prog->driver_cache_blob) {
274       ralloc_free(prog->driver_cache_blob);
275    }
276 
277    ralloc_free(prog);
278 }
279 
280 
281 /**
282  * Return the gl_program object for a given ID.
283  * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
284  * casts elsewhere.
285  */
286 struct gl_program *
_mesa_lookup_program(struct gl_context * ctx,GLuint id)287 _mesa_lookup_program(struct gl_context *ctx, GLuint id)
288 {
289    if (id)
290       return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
291    else
292       return NULL;
293 }
294 
295 
296 /**
297  * Reference counting for vertex/fragment programs
298  * This is normally only called from the _mesa_reference_program() macro
299  * when there's a real pointer change.
300  */
301 void
_mesa_reference_program_(struct gl_context * ctx,struct gl_program ** ptr,struct gl_program * prog)302 _mesa_reference_program_(struct gl_context *ctx,
303                          struct gl_program **ptr,
304                          struct gl_program *prog)
305 {
306 #ifndef NDEBUG
307    assert(ptr);
308    if (*ptr && prog) {
309       /* sanity check */
310       if ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB)
311          assert(prog->Target == GL_VERTEX_PROGRAM_ARB);
312       else if ((*ptr)->Target == GL_FRAGMENT_PROGRAM_ARB)
313          assert(prog->Target == GL_FRAGMENT_PROGRAM_ARB ||
314                 prog->Target == GL_FRAGMENT_PROGRAM_NV);
315       else if ((*ptr)->Target == GL_GEOMETRY_PROGRAM_NV)
316          assert(prog->Target == GL_GEOMETRY_PROGRAM_NV);
317    }
318 #endif
319 
320    if (*ptr) {
321       struct gl_program *oldProg = *ptr;
322 
323       assert(oldProg->RefCount > 0);
324 
325       if (p_atomic_dec_zero(&oldProg->RefCount)) {
326          assert(ctx);
327          _mesa_reference_shader_program_data(ctx, &oldProg->sh.data, NULL);
328          ctx->Driver.DeleteProgram(ctx, oldProg);
329       }
330 
331       *ptr = NULL;
332    }
333 
334    assert(!*ptr);
335    if (prog) {
336       p_atomic_inc(&prog->RefCount);
337    }
338 
339    *ptr = prog;
340 }
341 
342 
343 /**
344  * Insert 'count' NOP instructions at 'start' in the given program.
345  * Adjust branch targets accordingly.
346  */
347 GLboolean
_mesa_insert_instructions(struct gl_program * prog,GLuint start,GLuint count)348 _mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
349 {
350    const GLuint origLen = prog->arb.NumInstructions;
351    const GLuint newLen = origLen + count;
352    struct prog_instruction *newInst;
353    GLuint i;
354 
355    /* adjust branches */
356    for (i = 0; i < prog->arb.NumInstructions; i++) {
357       struct prog_instruction *inst = prog->arb.Instructions + i;
358       if (inst->BranchTarget > 0) {
359          if ((GLuint)inst->BranchTarget >= start) {
360             inst->BranchTarget += count;
361          }
362       }
363    }
364 
365    /* Alloc storage for new instructions */
366    newInst = rzalloc_array(prog, struct prog_instruction, newLen);
367    if (!newInst) {
368       return GL_FALSE;
369    }
370 
371    /* Copy 'start' instructions into new instruction buffer */
372    _mesa_copy_instructions(newInst, prog->arb.Instructions, start);
373 
374    /* init the new instructions */
375    _mesa_init_instructions(newInst + start, count);
376 
377    /* Copy the remaining/tail instructions to new inst buffer */
378    _mesa_copy_instructions(newInst + start + count,
379                            prog->arb.Instructions + start,
380                            origLen - start);
381 
382    /* free old instructions */
383    ralloc_free(prog->arb.Instructions);
384 
385    /* install new instructions */
386    prog->arb.Instructions = newInst;
387    prog->arb.NumInstructions = newLen;
388 
389    return GL_TRUE;
390 }
391 
392 /**
393  * Delete 'count' instructions at 'start' in the given program.
394  * Adjust branch targets accordingly.
395  */
396 GLboolean
_mesa_delete_instructions(struct gl_program * prog,GLuint start,GLuint count,void * mem_ctx)397 _mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count,
398                           void *mem_ctx)
399 {
400    const GLuint origLen = prog->arb.NumInstructions;
401    const GLuint newLen = origLen - count;
402    struct prog_instruction *newInst;
403    GLuint i;
404 
405    /* adjust branches */
406    for (i = 0; i < prog->arb.NumInstructions; i++) {
407       struct prog_instruction *inst = prog->arb.Instructions + i;
408       if (inst->BranchTarget > 0) {
409          if (inst->BranchTarget > (GLint) start) {
410             inst->BranchTarget -= count;
411          }
412       }
413    }
414 
415    /* Alloc storage for new instructions */
416    newInst = rzalloc_array(mem_ctx, struct prog_instruction, newLen);
417    if (!newInst) {
418       return GL_FALSE;
419    }
420 
421    /* Copy 'start' instructions into new instruction buffer */
422    _mesa_copy_instructions(newInst, prog->arb.Instructions, start);
423 
424    /* Copy the remaining/tail instructions to new inst buffer */
425    _mesa_copy_instructions(newInst + start,
426                            prog->arb.Instructions + start + count,
427                            newLen - start);
428 
429    /* free old instructions */
430    ralloc_free(prog->arb.Instructions);
431 
432    /* install new instructions */
433    prog->arb.Instructions = newInst;
434    prog->arb.NumInstructions = newLen;
435 
436    return GL_TRUE;
437 }
438 
439 
440 /**
441  * Populate the 'used' array with flags indicating which registers (TEMPs,
442  * INPUTs, OUTPUTs, etc, are used by the given program.
443  * \param file  type of register to scan for
444  * \param used  returns true/false flags for in use / free
445  * \param usedSize  size of the 'used' array
446  */
447 void
_mesa_find_used_registers(const struct gl_program * prog,gl_register_file file,GLboolean used[],GLuint usedSize)448 _mesa_find_used_registers(const struct gl_program *prog,
449                           gl_register_file file,
450                           GLboolean used[], GLuint usedSize)
451 {
452    GLuint i, j;
453 
454    memset(used, 0, usedSize);
455 
456    for (i = 0; i < prog->arb.NumInstructions; i++) {
457       const struct prog_instruction *inst = prog->arb.Instructions + i;
458       const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
459 
460       if (inst->DstReg.File == file) {
461          assert(inst->DstReg.Index < usedSize);
462          if(inst->DstReg.Index < usedSize)
463             used[inst->DstReg.Index] = GL_TRUE;
464       }
465 
466       for (j = 0; j < n; j++) {
467          if (inst->SrcReg[j].File == file) {
468             assert(inst->SrcReg[j].Index < (GLint) usedSize);
469             if (inst->SrcReg[j].Index < (GLint) usedSize)
470                used[inst->SrcReg[j].Index] = GL_TRUE;
471          }
472       }
473    }
474 }
475 
476 
477 /**
478  * Scan the given 'used' register flag array for the first entry
479  * that's >= firstReg.
480  * \param used  vector of flags indicating registers in use (as returned
481  *              by _mesa_find_used_registers())
482  * \param usedSize  size of the 'used' array
483  * \param firstReg  first register to start searching at
484  * \return index of unused register, or -1 if none.
485  */
486 GLint
_mesa_find_free_register(const GLboolean used[],GLuint usedSize,GLuint firstReg)487 _mesa_find_free_register(const GLboolean used[],
488                          GLuint usedSize, GLuint firstReg)
489 {
490    GLuint i;
491 
492    assert(firstReg < usedSize);
493 
494    for (i = firstReg; i < usedSize; i++)
495       if (!used[i])
496          return i;
497 
498    return -1;
499 }
500 
501 
502 /* Gets the minimum number of shader invocations per fragment.
503  * This function is useful to determine if we need to do per
504  * sample shading or per fragment shading.
505  */
506 GLint
_mesa_get_min_invocations_per_fragment(struct gl_context * ctx,const struct gl_program * prog)507 _mesa_get_min_invocations_per_fragment(struct gl_context *ctx,
508                                        const struct gl_program *prog)
509 {
510    /* From ARB_sample_shading specification:
511     * "Using gl_SampleID in a fragment shader causes the entire shader
512     *  to be evaluated per-sample."
513     *
514     * "Using gl_SamplePosition in a fragment shader causes the entire
515     *  shader to be evaluated per-sample."
516     *
517     * "If MULTISAMPLE or SAMPLE_SHADING_ARB is disabled, sample shading
518     *  has no effect."
519     */
520    if (ctx->Multisample.Enabled) {
521       /* The ARB_gpu_shader5 specification says:
522        *
523        * "Use of the "sample" qualifier on a fragment shader input
524        *  forces per-sample shading"
525        */
526       if (prog->info.fs.uses_sample_qualifier ||
527           BITSET_TEST(prog->info.system_values_read, SYSTEM_VALUE_SAMPLE_ID) ||
528           BITSET_TEST(prog->info.system_values_read, SYSTEM_VALUE_SAMPLE_POS))
529          return MAX2(_mesa_geometric_samples(ctx->DrawBuffer), 1);
530       else if (ctx->Multisample.SampleShading)
531          return MAX2(ceilf(ctx->Multisample.MinSampleShadingValue *
532                           _mesa_geometric_samples(ctx->DrawBuffer)), 1);
533       else
534          return 1;
535    }
536    return 1;
537 }
538 
539 
540 GLbitfield
gl_external_samplers(const struct gl_program * prog)541 gl_external_samplers(const struct gl_program *prog)
542 {
543    GLbitfield external_samplers = 0;
544    GLbitfield mask = prog->SamplersUsed;
545 
546    while (mask) {
547       int idx = u_bit_scan(&mask);
548       if (prog->sh.SamplerTargets[idx] == TEXTURE_EXTERNAL_INDEX)
549          external_samplers |= (1 << idx);
550    }
551 
552    return external_samplers;
553 }
554