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