program: remove _mesa_init_*_program wrappers
[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/hash.h"
35 #include "main/macros.h"
36 #include "program.h"
37 #include "prog_cache.h"
38 #include "prog_parameter.h"
39 #include "prog_instruction.h"
40 #include "util/ralloc.h"
41
42
43 /**
44 * A pointer to this dummy program is put into the hash table when
45 * glGenPrograms is called.
46 */
47 struct gl_program _mesa_DummyProgram;
48
49
50 /**
51 * Init context's vertex/fragment program state
52 */
53 void
54 _mesa_init_program(struct gl_context *ctx)
55 {
56 /*
57 * If this assertion fails, we need to increase the field
58 * size for register indexes (see INST_INDEX_BITS).
59 */
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));
64
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));
69
70 assert(ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents <= 4 * MAX_UNIFORMS);
71 assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents <= 4 * MAX_UNIFORMS);
72
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));
75
76 /* If this fails, increase prog_instruction::TexSrcUnit size */
77 STATIC_ASSERT(MAX_TEXTURE_UNITS <= (1 << 5));
78
79 /* If this fails, increase prog_instruction::TexSrcTarget size */
80 STATIC_ASSERT(NUM_TEXTURE_TARGETS <= (1 << 4));
81
82 ctx->Program.ErrorPos = -1;
83 ctx->Program.ErrorString = strdup("");
84
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();
93
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();
99
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++;
105 }
106
107
108 /**
109 * Free a context's vertex/fragment program state
110 */
111 void
112 _mesa_free_program_data(struct gl_context *ctx)
113 {
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);
118
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);
124 }
125 }
126
127 free((void *) ctx->Program.ErrorString);
128 }
129
130
131 /**
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
134 * shared state.
135 */
136 void
137 _mesa_update_default_objects_program(struct gl_context *ctx)
138 {
139 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
140 ctx->Shared->DefaultVertexProgram);
141 assert(ctx->VertexProgram.Current);
142
143 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
144 ctx->Shared->DefaultFragmentProgram);
145 assert(ctx->FragmentProgram.Current);
146
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);
152 }
153 }
154 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
155 assert(ctx->ATIFragmentShader.Current);
156 ctx->ATIFragmentShader.Current->RefCount++;
157 }
158
159
160 /**
161 * Set the vertex/fragment program error state (position and error string).
162 * This is generally called from within the parsers.
163 */
164 void
165 _mesa_set_program_error(struct gl_context *ctx, GLint pos, const char *string)
166 {
167 ctx->Program.ErrorPos = pos;
168 free((void *) ctx->Program.ErrorString);
169 if (!string)
170 string = "";
171 ctx->Program.ErrorString = strdup(string);
172 }
173
174
175 /**
176 * Initialize a new gl_program object.
177 */
178 struct gl_program *
179 _mesa_init_gl_program(void *_prog, GLenum target, GLuint id)
180 {
181 struct gl_program *prog = (struct gl_program*)_prog;
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 struct gl_program *prog;
218 switch (target) {
219 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
220 prog = _mesa_init_gl_program(CALLOC_STRUCT(gl_vertex_program),
221 target, id);
222 break;
223 case GL_FRAGMENT_PROGRAM_NV:
224 case GL_FRAGMENT_PROGRAM_ARB:
225 prog =_mesa_init_gl_program(CALLOC_STRUCT(gl_fragment_program),
226 target, id);
227 break;
228 case GL_GEOMETRY_PROGRAM_NV:
229 prog = _mesa_init_gl_program(CALLOC_STRUCT(gl_geometry_program),
230 target, id);
231 break;
232 case GL_TESS_CONTROL_PROGRAM_NV:
233 prog = _mesa_init_gl_program(CALLOC_STRUCT(gl_tess_ctrl_program),
234 target, id);
235 break;
236 case GL_TESS_EVALUATION_PROGRAM_NV:
237 prog = _mesa_init_gl_program(CALLOC_STRUCT(gl_tess_eval_program),
238 target, id);
239 break;
240 case GL_COMPUTE_PROGRAM_NV:
241 prog = _mesa_init_gl_program(CALLOC_STRUCT(gl_compute_program),
242 target, id);
243 break;
244 default:
245 _mesa_problem(ctx, "bad target in _mesa_new_program");
246 prog = NULL;
247 }
248 return prog;
249 }
250
251
252 /**
253 * Delete a program and remove it from the hash table, ignoring the
254 * reference count.
255 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
256 * by a device driver function.
257 */
258 void
259 _mesa_delete_program(struct gl_context *ctx, struct gl_program *prog)
260 {
261 (void) ctx;
262 assert(prog);
263 assert(prog->RefCount==0);
264
265 if (prog == &_mesa_DummyProgram)
266 return;
267
268 free(prog->String);
269 free(prog->LocalParams);
270
271 if (prog->Instructions) {
272 _mesa_free_instructions(prog->Instructions, prog->NumInstructions);
273 }
274 if (prog->Parameters) {
275 _mesa_free_parameter_list(prog->Parameters);
276 }
277
278 if (prog->nir) {
279 ralloc_free(prog->nir);
280 }
281
282 mtx_destroy(&prog->Mutex);
283 free(prog);
284 }
285
286
287 /**
288 * Return the gl_program object for a given ID.
289 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
290 * casts elsewhere.
291 */
292 struct gl_program *
293 _mesa_lookup_program(struct gl_context *ctx, GLuint id)
294 {
295 if (id)
296 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
297 else
298 return NULL;
299 }
300
301
302 /**
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.
306 */
307 void
308 _mesa_reference_program_(struct gl_context *ctx,
309 struct gl_program **ptr,
310 struct gl_program *prog)
311 {
312 #ifndef NDEBUG
313 assert(ptr);
314 if (*ptr && prog) {
315 /* sanity check */
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);
323 }
324 #endif
325
326 if (*ptr) {
327 GLboolean deleteFlag;
328 struct gl_program *oldProg = *ptr;
329
330 mtx_lock(&oldProg->Mutex);
331 assert(oldProg->RefCount > 0);
332 oldProg->RefCount--;
333
334 deleteFlag = (oldProg->RefCount == 0);
335 mtx_unlock(&oldProg->Mutex);
336
337 if (deleteFlag) {
338 assert(ctx);
339 ctx->Driver.DeleteProgram(ctx, oldProg);
340 }
341
342 *ptr = NULL;
343 }
344
345 assert(!*ptr);
346 if (prog) {
347 mtx_lock(&prog->Mutex);
348 prog->RefCount++;
349 mtx_unlock(&prog->Mutex);
350 }
351
352 *ptr = prog;
353 }
354
355
356 /**
357 * Insert 'count' NOP instructions at 'start' in the given program.
358 * Adjust branch targets accordingly.
359 */
360 GLboolean
361 _mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
362 {
363 const GLuint origLen = prog->NumInstructions;
364 const GLuint newLen = origLen + count;
365 struct prog_instruction *newInst;
366 GLuint i;
367
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;
374 }
375 }
376 }
377
378 /* Alloc storage for new instructions */
379 newInst = _mesa_alloc_instructions(newLen);
380 if (!newInst) {
381 return GL_FALSE;
382 }
383
384 /* Copy 'start' instructions into new instruction buffer */
385 _mesa_copy_instructions(newInst, prog->Instructions, start);
386
387 /* init the new instructions */
388 _mesa_init_instructions(newInst + start, count);
389
390 /* Copy the remaining/tail instructions to new inst buffer */
391 _mesa_copy_instructions(newInst + start + count,
392 prog->Instructions + start,
393 origLen - start);
394
395 /* free old instructions */
396 _mesa_free_instructions(prog->Instructions, origLen);
397
398 /* install new instructions */
399 prog->Instructions = newInst;
400 prog->NumInstructions = newLen;
401
402 return GL_TRUE;
403 }
404
405 /**
406 * Delete 'count' instructions at 'start' in the given program.
407 * Adjust branch targets accordingly.
408 */
409 GLboolean
410 _mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count)
411 {
412 const GLuint origLen = prog->NumInstructions;
413 const GLuint newLen = origLen - count;
414 struct prog_instruction *newInst;
415 GLuint i;
416
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;
423 }
424 }
425 }
426
427 /* Alloc storage for new instructions */
428 newInst = _mesa_alloc_instructions(newLen);
429 if (!newInst) {
430 return GL_FALSE;
431 }
432
433 /* Copy 'start' instructions into new instruction buffer */
434 _mesa_copy_instructions(newInst, prog->Instructions, start);
435
436 /* Copy the remaining/tail instructions to new inst buffer */
437 _mesa_copy_instructions(newInst + start,
438 prog->Instructions + start + count,
439 newLen - start);
440
441 /* free old instructions */
442 _mesa_free_instructions(prog->Instructions, origLen);
443
444 /* install new instructions */
445 prog->Instructions = newInst;
446 prog->NumInstructions = newLen;
447
448 return GL_TRUE;
449 }
450
451
452 /**
453 * Search instructions for registers that match (oldFile, oldIndex),
454 * replacing them with (newFile, newIndex).
455 */
456 static void
457 replace_registers(struct prog_instruction *inst, GLuint numInst,
458 GLuint oldFile, GLuint oldIndex,
459 GLuint newFile, GLuint newIndex)
460 {
461 GLuint i, j;
462 for (i = 0; i < numInst; i++) {
463 /* src regs */
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;
469 }
470 }
471 /* dst reg */
472 if (inst[i].DstReg.File == oldFile && inst[i].DstReg.Index == oldIndex) {
473 inst[i].DstReg.File = newFile;
474 inst[i].DstReg.Index = newIndex;
475 }
476 }
477 }
478
479
480 /**
481 * Search instructions for references to program parameters. When found,
482 * increment the parameter index by 'offset'.
483 * Used when combining programs.
484 */
485 static void
486 adjust_param_indexes(struct prog_instruction *inst, GLuint numInst,
487 GLuint offset)
488 {
489 GLuint i, j;
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;
497 }
498 }
499 }
500 }
501
502
503 /**
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
509 */
510 void
511 _mesa_find_used_registers(const struct gl_program *prog,
512 gl_register_file file,
513 GLboolean used[], GLuint usedSize)
514 {
515 GLuint i, j;
516
517 memset(used, 0, usedSize);
518
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);
522
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;
527 }
528
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;
534 }
535 }
536 }
537 }
538
539
540 /**
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.
548 */
549 GLint
550 _mesa_find_free_register(const GLboolean used[],
551 GLuint usedSize, GLuint firstReg)
552 {
553 GLuint i;
554
555 assert(firstReg < usedSize);
556
557 for (i = firstReg; i < usedSize; i++)
558 if (!used[i])
559 return i;
560
561 return -1;
562 }
563
564
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.
568 */
569 GLint
570 _mesa_get_min_invocations_per_fragment(struct gl_context *ctx,
571 const struct gl_fragment_program *prog,
572 bool ignore_sample_qualifier)
573 {
574 /* From ARB_sample_shading specification:
575 * "Using gl_SampleID in a fragment shader causes the entire shader
576 * to be evaluated per-sample."
577 *
578 * "Using gl_SamplePosition in a fragment shader causes the entire
579 * shader to be evaluated per-sample."
580 *
581 * "If MULTISAMPLE or SAMPLE_SHADING_ARB is disabled, sample shading
582 * has no effect."
583 */
584 if (ctx->Multisample.Enabled) {
585 /* The ARB_gpu_shader5 specification says:
586 *
587 * "Use of the "sample" qualifier on a fragment shader input
588 * forces per-sample shading"
589 */
590 if (prog->IsSample && !ignore_sample_qualifier)
591 return MAX2(ctx->DrawBuffer->Visual.samples, 1);
592
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);
599 else
600 return 1;
601 }
602 return 1;
603 }