glsl/mesa: add reference to gl_shader_program_data from gl_program
[mesa.git] / src / mesa / program / program.c
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/ralloc.h"
43
44
45 /**
46 * A pointer to this dummy program is put into the hash table when
47 * glGenPrograms is called.
48 */
49 struct gl_program _mesa_DummyProgram;
50
51
52 /**
53 * Init context's vertex/fragment program state
54 */
55 void
56 _mesa_init_program(struct gl_context *ctx)
57 {
58 /*
59 * If this assertion fails, we need to increase the field
60 * size for register indexes (see INST_INDEX_BITS).
61 */
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));
66
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));
71
72 assert(ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents <= 4 * MAX_UNIFORMS);
73 assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents <= 4 * MAX_UNIFORMS);
74
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));
77
78 /* If this fails, increase prog_instruction::TexSrcUnit size */
79 STATIC_ASSERT(MAX_TEXTURE_UNITS <= (1 << 5));
80
81 /* If this fails, increase prog_instruction::TexSrcTarget size */
82 STATIC_ASSERT(NUM_TEXTURE_TARGETS <= (1 << 4));
83
84 ctx->Program.ErrorPos = -1;
85 ctx->Program.ErrorString = strdup("");
86
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();
95
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();
101
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++;
107 }
108
109
110 /**
111 * Free a context's vertex/fragment program state
112 */
113 void
114 _mesa_free_program_data(struct gl_context *ctx)
115 {
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);
120
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);
126 }
127 }
128
129 free((void *) ctx->Program.ErrorString);
130 }
131
132
133 /**
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
136 * shared state.
137 */
138 void
139 _mesa_update_default_objects_program(struct gl_context *ctx)
140 {
141 _mesa_reference_program(ctx, &ctx->VertexProgram.Current,
142 ctx->Shared->DefaultVertexProgram);
143 assert(ctx->VertexProgram.Current);
144
145 _mesa_reference_program(ctx, &ctx->FragmentProgram.Current,
146 ctx->Shared->DefaultFragmentProgram);
147 assert(ctx->FragmentProgram.Current);
148
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);
154 }
155 }
156 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
157 assert(ctx->ATIFragmentShader.Current);
158 ctx->ATIFragmentShader.Current->RefCount++;
159 }
160
161
162 /**
163 * Set the vertex/fragment program error state (position and error string).
164 * This is generally called from within the parsers.
165 */
166 void
167 _mesa_set_program_error(struct gl_context *ctx, GLint pos, const char *string)
168 {
169 ctx->Program.ErrorPos = pos;
170 free((void *) ctx->Program.ErrorString);
171 if (!string)
172 string = "";
173 ctx->Program.ErrorString = strdup(string);
174 }
175
176
177 /**
178 * Initialize a new gl_program object.
179 */
180 struct gl_program *
181 _mesa_init_gl_program(struct gl_program *prog, GLenum target, GLuint id)
182 {
183 GLuint i;
184
185 if (!prog)
186 return NULL;
187
188 memset(prog, 0, sizeof(*prog));
189 mtx_init(&prog->Mutex, mtx_plain);
190 prog->Id = id;
191 prog->Target = target;
192 prog->RefCount = 1;
193 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
194 prog->info.stage = _mesa_program_enum_to_shader_stage(target);
195
196 /* default mapping from samplers to texture units */
197 for (i = 0; i < MAX_SAMPLERS; i++)
198 prog->SamplerUnits[i] = i;
199
200 return prog;
201 }
202
203
204 /**
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.
210 *
211 * \param ctx context
212 * \param id program id/number
213 * \param target program target/type
214 * \return pointer to new program object
215 */
216 struct gl_program *
217 _mesa_new_program(struct gl_context *ctx, GLenum target, GLuint id)
218 {
219 switch (target) {
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);
228 }
229 default:
230 _mesa_problem(ctx, "bad target in _mesa_new_program");
231 return NULL;
232 }
233 }
234
235
236 /**
237 * Delete a program and remove it from the hash table, ignoring the
238 * reference count.
239 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
240 * by a device driver function.
241 */
242 void
243 _mesa_delete_program(struct gl_context *ctx, struct gl_program *prog)
244 {
245 (void) ctx;
246 assert(prog);
247 assert(prog->RefCount==0);
248
249 if (prog == &_mesa_DummyProgram)
250 return;
251
252 if (prog->Parameters) {
253 _mesa_free_parameter_list(prog->Parameters);
254 }
255
256 if (prog->nir) {
257 ralloc_free(prog->nir);
258 }
259
260 mtx_destroy(&prog->Mutex);
261 ralloc_free(prog);
262 }
263
264
265 /**
266 * Return the gl_program object for a given ID.
267 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
268 * casts elsewhere.
269 */
270 struct gl_program *
271 _mesa_lookup_program(struct gl_context *ctx, GLuint id)
272 {
273 if (id)
274 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
275 else
276 return NULL;
277 }
278
279
280 /**
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.
284 */
285 void
286 _mesa_reference_program_(struct gl_context *ctx,
287 struct gl_program **ptr,
288 struct gl_program *prog)
289 {
290 #ifndef NDEBUG
291 assert(ptr);
292 if (*ptr && prog) {
293 /* sanity check */
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);
301 }
302 #endif
303
304 if (*ptr) {
305 GLboolean deleteFlag;
306 struct gl_program *oldProg = *ptr;
307
308 mtx_lock(&oldProg->Mutex);
309 assert(oldProg->RefCount > 0);
310 oldProg->RefCount--;
311
312 deleteFlag = (oldProg->RefCount == 0);
313 mtx_unlock(&oldProg->Mutex);
314
315 if (deleteFlag) {
316 assert(ctx);
317 _mesa_reference_shader_program_data(ctx, &oldProg->sh.data, NULL);
318 ctx->Driver.DeleteProgram(ctx, oldProg);
319 }
320
321 *ptr = NULL;
322 }
323
324 assert(!*ptr);
325 if (prog) {
326 mtx_lock(&prog->Mutex);
327 prog->RefCount++;
328 mtx_unlock(&prog->Mutex);
329 }
330
331 *ptr = prog;
332 }
333
334
335 /**
336 * Insert 'count' NOP instructions at 'start' in the given program.
337 * Adjust branch targets accordingly.
338 */
339 GLboolean
340 _mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
341 {
342 const GLuint origLen = prog->arb.NumInstructions;
343 const GLuint newLen = origLen + count;
344 struct prog_instruction *newInst;
345 GLuint i;
346
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;
353 }
354 }
355 }
356
357 /* Alloc storage for new instructions */
358 newInst = rzalloc_array(prog, struct prog_instruction, newLen);
359 if (!newInst) {
360 return GL_FALSE;
361 }
362
363 /* Copy 'start' instructions into new instruction buffer */
364 _mesa_copy_instructions(newInst, prog->arb.Instructions, start);
365
366 /* init the new instructions */
367 _mesa_init_instructions(newInst + start, count);
368
369 /* Copy the remaining/tail instructions to new inst buffer */
370 _mesa_copy_instructions(newInst + start + count,
371 prog->arb.Instructions + start,
372 origLen - start);
373
374 /* free old instructions */
375 ralloc_free(prog->arb.Instructions);
376
377 /* install new instructions */
378 prog->arb.Instructions = newInst;
379 prog->arb.NumInstructions = newLen;
380
381 return GL_TRUE;
382 }
383
384 /**
385 * Delete 'count' instructions at 'start' in the given program.
386 * Adjust branch targets accordingly.
387 */
388 GLboolean
389 _mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count,
390 void *mem_ctx)
391 {
392 const GLuint origLen = prog->arb.NumInstructions;
393 const GLuint newLen = origLen - count;
394 struct prog_instruction *newInst;
395 GLuint i;
396
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;
403 }
404 }
405 }
406
407 /* Alloc storage for new instructions */
408 newInst = rzalloc_array(mem_ctx, struct prog_instruction, newLen);
409 if (!newInst) {
410 return GL_FALSE;
411 }
412
413 /* Copy 'start' instructions into new instruction buffer */
414 _mesa_copy_instructions(newInst, prog->arb.Instructions, start);
415
416 /* Copy the remaining/tail instructions to new inst buffer */
417 _mesa_copy_instructions(newInst + start,
418 prog->arb.Instructions + start + count,
419 newLen - start);
420
421 /* free old instructions */
422 ralloc_free(prog->arb.Instructions);
423
424 /* install new instructions */
425 prog->arb.Instructions = newInst;
426 prog->arb.NumInstructions = newLen;
427
428 return GL_TRUE;
429 }
430
431
432 /**
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
438 */
439 void
440 _mesa_find_used_registers(const struct gl_program *prog,
441 gl_register_file file,
442 GLboolean used[], GLuint usedSize)
443 {
444 GLuint i, j;
445
446 memset(used, 0, usedSize);
447
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);
451
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;
456 }
457
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;
463 }
464 }
465 }
466 }
467
468
469 /**
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.
477 */
478 GLint
479 _mesa_find_free_register(const GLboolean used[],
480 GLuint usedSize, GLuint firstReg)
481 {
482 GLuint i;
483
484 assert(firstReg < usedSize);
485
486 for (i = firstReg; i < usedSize; i++)
487 if (!used[i])
488 return i;
489
490 return -1;
491 }
492
493
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.
497 */
498 GLint
499 _mesa_get_min_invocations_per_fragment(struct gl_context *ctx,
500 const struct gl_program *prog,
501 bool ignore_sample_qualifier)
502 {
503 /* From ARB_sample_shading specification:
504 * "Using gl_SampleID in a fragment shader causes the entire shader
505 * to be evaluated per-sample."
506 *
507 * "Using gl_SamplePosition in a fragment shader causes the entire
508 * shader to be evaluated per-sample."
509 *
510 * "If MULTISAMPLE or SAMPLE_SHADING_ARB is disabled, sample shading
511 * has no effect."
512 */
513 if (ctx->Multisample.Enabled) {
514 /* The ARB_gpu_shader5 specification says:
515 *
516 * "Use of the "sample" qualifier on a fragment shader input
517 * forces per-sample shading"
518 */
519 if (prog->info.fs.uses_sample_qualifier && !ignore_sample_qualifier)
520 return MAX2(_mesa_geometric_samples(ctx->DrawBuffer), 1);
521
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);
528 else
529 return 1;
530 }
531 return 1;
532 }