e5eb06cb4b735076d3868288328c1d20088e8ca1
[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 "program.h"
38 #include "prog_cache.h"
39 #include "prog_parameter.h"
40 #include "prog_instruction.h"
41 #include "util/ralloc.h"
42
43
44 /**
45 * A pointer to this dummy program is put into the hash table when
46 * glGenPrograms is called.
47 */
48 struct gl_program _mesa_DummyProgram;
49
50
51 /**
52 * Init context's vertex/fragment program state
53 */
54 void
55 _mesa_init_program(struct gl_context *ctx)
56 {
57 /*
58 * If this assertion fails, we need to increase the field
59 * size for register indexes (see INST_INDEX_BITS).
60 */
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));
65
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));
70
71 assert(ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents <= 4 * MAX_UNIFORMS);
72 assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents <= 4 * MAX_UNIFORMS);
73
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));
76
77 /* If this fails, increase prog_instruction::TexSrcUnit size */
78 STATIC_ASSERT(MAX_TEXTURE_UNITS <= (1 << 5));
79
80 /* If this fails, increase prog_instruction::TexSrcTarget size */
81 STATIC_ASSERT(NUM_TEXTURE_TARGETS <= (1 << 4));
82
83 ctx->Program.ErrorPos = -1;
84 ctx->Program.ErrorString = strdup("");
85
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();
94
95 ctx->FragmentProgram.Enabled = GL_FALSE;
96 _mesa_reference_program(ctx, &ctx->FragmentProgram.Current,
97 ctx->Shared->DefaultFragmentProgram);
98 assert(ctx->FragmentProgram.Current);
99 ctx->FragmentProgram.Cache = _mesa_new_program_cache();
100
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++;
106 }
107
108
109 /**
110 * Free a context's vertex/fragment program state
111 */
112 void
113 _mesa_free_program_data(struct gl_context *ctx)
114 {
115 _mesa_reference_program(ctx, &ctx->VertexProgram.Current, NULL);
116 _mesa_delete_program_cache(ctx, ctx->VertexProgram.Cache);
117 _mesa_reference_program(ctx, &ctx->FragmentProgram.Current, NULL);
118 _mesa_delete_shader_cache(ctx, ctx->FragmentProgram.Cache);
119
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);
125 }
126 }
127
128 free((void *) ctx->Program.ErrorString);
129 }
130
131
132 /**
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
135 * shared state.
136 */
137 void
138 _mesa_update_default_objects_program(struct gl_context *ctx)
139 {
140 _mesa_reference_program(ctx, &ctx->VertexProgram.Current,
141 ctx->Shared->DefaultVertexProgram);
142 assert(ctx->VertexProgram.Current);
143
144 _mesa_reference_program(ctx, &ctx->FragmentProgram.Current,
145 ctx->Shared->DefaultFragmentProgram);
146 assert(ctx->FragmentProgram.Current);
147
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);
153 }
154 }
155 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
156 assert(ctx->ATIFragmentShader.Current);
157 ctx->ATIFragmentShader.Current->RefCount++;
158 }
159
160
161 /**
162 * Set the vertex/fragment program error state (position and error string).
163 * This is generally called from within the parsers.
164 */
165 void
166 _mesa_set_program_error(struct gl_context *ctx, GLint pos, const char *string)
167 {
168 ctx->Program.ErrorPos = pos;
169 free((void *) ctx->Program.ErrorString);
170 if (!string)
171 string = "";
172 ctx->Program.ErrorString = strdup(string);
173 }
174
175
176 /**
177 * Initialize a new gl_program object.
178 */
179 struct gl_program *
180 _mesa_init_gl_program(struct gl_program *prog, GLenum target, GLuint id)
181 {
182 GLuint i;
183
184 if (!prog)
185 return NULL;
186
187 memset(prog, 0, sizeof(*prog));
188 mtx_init(&prog->Mutex, mtx_plain);
189 prog->Id = id;
190 prog->Target = target;
191 prog->RefCount = 1;
192 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
193
194 /* default mapping from samplers to texture units */
195 for (i = 0; i < MAX_SAMPLERS; i++)
196 prog->SamplerUnits[i] = i;
197
198 return prog;
199 }
200
201
202 /**
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.
208 *
209 * \param ctx context
210 * \param id program id/number
211 * \param target program target/type
212 * \return pointer to new program object
213 */
214 struct gl_program *
215 _mesa_new_program(struct gl_context *ctx, GLenum target, GLuint id)
216 {
217 switch (target) {
218 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
219 case GL_GEOMETRY_PROGRAM_NV:
220 case GL_TESS_CONTROL_PROGRAM_NV:
221 case GL_TESS_EVALUATION_PROGRAM_NV:
222 case GL_FRAGMENT_PROGRAM_ARB:
223 case GL_COMPUTE_PROGRAM_NV: {
224 struct gl_program *prog = rzalloc(NULL, struct gl_program);
225 return _mesa_init_gl_program(prog, target, id);
226 }
227 default:
228 _mesa_problem(ctx, "bad target in _mesa_new_program");
229 return NULL;
230 }
231 }
232
233
234 /**
235 * Delete a program and remove it from the hash table, ignoring the
236 * reference count.
237 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
238 * by a device driver function.
239 */
240 void
241 _mesa_delete_program(struct gl_context *ctx, struct gl_program *prog)
242 {
243 (void) ctx;
244 assert(prog);
245 assert(prog->RefCount==0);
246
247 if (prog == &_mesa_DummyProgram)
248 return;
249
250 if (prog->Parameters) {
251 _mesa_free_parameter_list(prog->Parameters);
252 }
253
254 if (prog->nir) {
255 ralloc_free(prog->nir);
256 }
257
258 mtx_destroy(&prog->Mutex);
259 ralloc_free(prog);
260 }
261
262
263 /**
264 * Return the gl_program object for a given ID.
265 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
266 * casts elsewhere.
267 */
268 struct gl_program *
269 _mesa_lookup_program(struct gl_context *ctx, GLuint id)
270 {
271 if (id)
272 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
273 else
274 return NULL;
275 }
276
277
278 /**
279 * Reference counting for vertex/fragment programs
280 * This is normally only called from the _mesa_reference_program() macro
281 * when there's a real pointer change.
282 */
283 void
284 _mesa_reference_program_(struct gl_context *ctx,
285 struct gl_program **ptr,
286 struct gl_program *prog)
287 {
288 #ifndef NDEBUG
289 assert(ptr);
290 if (*ptr && prog) {
291 /* sanity check */
292 if ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB)
293 assert(prog->Target == GL_VERTEX_PROGRAM_ARB);
294 else if ((*ptr)->Target == GL_FRAGMENT_PROGRAM_ARB)
295 assert(prog->Target == GL_FRAGMENT_PROGRAM_ARB ||
296 prog->Target == GL_FRAGMENT_PROGRAM_NV);
297 else if ((*ptr)->Target == GL_GEOMETRY_PROGRAM_NV)
298 assert(prog->Target == GL_GEOMETRY_PROGRAM_NV);
299 }
300 #endif
301
302 if (*ptr) {
303 GLboolean deleteFlag;
304 struct gl_program *oldProg = *ptr;
305
306 mtx_lock(&oldProg->Mutex);
307 assert(oldProg->RefCount > 0);
308 oldProg->RefCount--;
309
310 deleteFlag = (oldProg->RefCount == 0);
311 mtx_unlock(&oldProg->Mutex);
312
313 if (deleteFlag) {
314 assert(ctx);
315 ctx->Driver.DeleteProgram(ctx, oldProg);
316 }
317
318 *ptr = NULL;
319 }
320
321 assert(!*ptr);
322 if (prog) {
323 mtx_lock(&prog->Mutex);
324 prog->RefCount++;
325 mtx_unlock(&prog->Mutex);
326 }
327
328 *ptr = prog;
329 }
330
331
332 /**
333 * Insert 'count' NOP instructions at 'start' in the given program.
334 * Adjust branch targets accordingly.
335 */
336 GLboolean
337 _mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
338 {
339 const GLuint origLen = prog->arb.NumInstructions;
340 const GLuint newLen = origLen + count;
341 struct prog_instruction *newInst;
342 GLuint i;
343
344 /* adjust branches */
345 for (i = 0; i < prog->arb.NumInstructions; i++) {
346 struct prog_instruction *inst = prog->arb.Instructions + i;
347 if (inst->BranchTarget > 0) {
348 if ((GLuint)inst->BranchTarget >= start) {
349 inst->BranchTarget += count;
350 }
351 }
352 }
353
354 /* Alloc storage for new instructions */
355 newInst = rzalloc_array(prog, struct prog_instruction, newLen);
356 if (!newInst) {
357 return GL_FALSE;
358 }
359
360 /* Copy 'start' instructions into new instruction buffer */
361 _mesa_copy_instructions(newInst, prog->arb.Instructions, start);
362
363 /* init the new instructions */
364 _mesa_init_instructions(newInst + start, count);
365
366 /* Copy the remaining/tail instructions to new inst buffer */
367 _mesa_copy_instructions(newInst + start + count,
368 prog->arb.Instructions + start,
369 origLen - start);
370
371 /* free old instructions */
372 ralloc_free(prog->arb.Instructions);
373
374 /* install new instructions */
375 prog->arb.Instructions = newInst;
376 prog->arb.NumInstructions = newLen;
377
378 return GL_TRUE;
379 }
380
381 /**
382 * Delete 'count' instructions at 'start' in the given program.
383 * Adjust branch targets accordingly.
384 */
385 GLboolean
386 _mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count,
387 void *mem_ctx)
388 {
389 const GLuint origLen = prog->arb.NumInstructions;
390 const GLuint newLen = origLen - count;
391 struct prog_instruction *newInst;
392 GLuint i;
393
394 /* adjust branches */
395 for (i = 0; i < prog->arb.NumInstructions; i++) {
396 struct prog_instruction *inst = prog->arb.Instructions + i;
397 if (inst->BranchTarget > 0) {
398 if (inst->BranchTarget > (GLint) start) {
399 inst->BranchTarget -= count;
400 }
401 }
402 }
403
404 /* Alloc storage for new instructions */
405 newInst = rzalloc_array(mem_ctx, struct prog_instruction, newLen);
406 if (!newInst) {
407 return GL_FALSE;
408 }
409
410 /* Copy 'start' instructions into new instruction buffer */
411 _mesa_copy_instructions(newInst, prog->arb.Instructions, start);
412
413 /* Copy the remaining/tail instructions to new inst buffer */
414 _mesa_copy_instructions(newInst + start,
415 prog->arb.Instructions + start + count,
416 newLen - start);
417
418 /* free old instructions */
419 ralloc_free(prog->arb.Instructions);
420
421 /* install new instructions */
422 prog->arb.Instructions = newInst;
423 prog->arb.NumInstructions = newLen;
424
425 return GL_TRUE;
426 }
427
428
429 /**
430 * Populate the 'used' array with flags indicating which registers (TEMPs,
431 * INPUTs, OUTPUTs, etc, are used by the given program.
432 * \param file type of register to scan for
433 * \param used returns true/false flags for in use / free
434 * \param usedSize size of the 'used' array
435 */
436 void
437 _mesa_find_used_registers(const struct gl_program *prog,
438 gl_register_file file,
439 GLboolean used[], GLuint usedSize)
440 {
441 GLuint i, j;
442
443 memset(used, 0, usedSize);
444
445 for (i = 0; i < prog->arb.NumInstructions; i++) {
446 const struct prog_instruction *inst = prog->arb.Instructions + i;
447 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
448
449 if (inst->DstReg.File == file) {
450 assert(inst->DstReg.Index < usedSize);
451 if(inst->DstReg.Index < usedSize)
452 used[inst->DstReg.Index] = GL_TRUE;
453 }
454
455 for (j = 0; j < n; j++) {
456 if (inst->SrcReg[j].File == file) {
457 assert(inst->SrcReg[j].Index < (GLint) usedSize);
458 if (inst->SrcReg[j].Index < (GLint) usedSize)
459 used[inst->SrcReg[j].Index] = GL_TRUE;
460 }
461 }
462 }
463 }
464
465
466 /**
467 * Scan the given 'used' register flag array for the first entry
468 * that's >= firstReg.
469 * \param used vector of flags indicating registers in use (as returned
470 * by _mesa_find_used_registers())
471 * \param usedSize size of the 'used' array
472 * \param firstReg first register to start searching at
473 * \return index of unused register, or -1 if none.
474 */
475 GLint
476 _mesa_find_free_register(const GLboolean used[],
477 GLuint usedSize, GLuint firstReg)
478 {
479 GLuint i;
480
481 assert(firstReg < usedSize);
482
483 for (i = firstReg; i < usedSize; i++)
484 if (!used[i])
485 return i;
486
487 return -1;
488 }
489
490
491 /* Gets the minimum number of shader invocations per fragment.
492 * This function is useful to determine if we need to do per
493 * sample shading or per fragment shading.
494 */
495 GLint
496 _mesa_get_min_invocations_per_fragment(struct gl_context *ctx,
497 const struct gl_program *prog,
498 bool ignore_sample_qualifier)
499 {
500 /* From ARB_sample_shading specification:
501 * "Using gl_SampleID in a fragment shader causes the entire shader
502 * to be evaluated per-sample."
503 *
504 * "Using gl_SamplePosition in a fragment shader causes the entire
505 * shader to be evaluated per-sample."
506 *
507 * "If MULTISAMPLE or SAMPLE_SHADING_ARB is disabled, sample shading
508 * has no effect."
509 */
510 if (ctx->Multisample.Enabled) {
511 /* The ARB_gpu_shader5 specification says:
512 *
513 * "Use of the "sample" qualifier on a fragment shader input
514 * forces per-sample shading"
515 */
516 if (prog->info.fs.uses_sample_qualifier && !ignore_sample_qualifier)
517 return MAX2(_mesa_geometric_samples(ctx->DrawBuffer), 1);
518
519 if (prog->info.system_values_read & (SYSTEM_BIT_SAMPLE_ID |
520 SYSTEM_BIT_SAMPLE_POS))
521 return MAX2(_mesa_geometric_samples(ctx->DrawBuffer), 1);
522 else if (ctx->Multisample.SampleShading)
523 return MAX2(ceil(ctx->Multisample.MinSampleShadingValue *
524 _mesa_geometric_samples(ctx->DrawBuffer)), 1);
525 else
526 return 1;
527 }
528 return 1;
529 }