1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
/*
* Mesa 3-D graphics library
*
* Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* \file program.c
* Vertex and fragment program support functions.
* \author Brian Paul
*/
#include "util/glheader.h"
#include "main/context.h"
#include "main/framebuffer.h"
#include "main/hash.h"
#include "main/macros.h"
#include "main/shaderobj.h"
#include "main/state.h"
#include "program.h"
#include "prog_cache.h"
#include "prog_parameter.h"
#include "prog_instruction.h"
#include "util/bitscan.h"
#include "util/ralloc.h"
#include "util/u_atomic.h"
#include "state_tracker/st_program.h"
#include "state_tracker/st_context.h"
/**
* A pointer to this dummy program is put into the hash table when
* glGenPrograms is called.
*/
struct gl_program _mesa_DummyProgram;
/**
* Init context's vertex/fragment program state
*/
void
_mesa_init_program(struct gl_context *ctx)
{
/*
* If this assertion fails, we need to increase the field
* size for register indexes (see INST_INDEX_BITS).
*/
assert(ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents / 4
<= (1 << INST_INDEX_BITS));
assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents / 4
<= (1 << INST_INDEX_BITS));
assert(ctx->Const.Program[MESA_SHADER_VERTEX].MaxTemps <= (1 << INST_INDEX_BITS));
assert(ctx->Const.Program[MESA_SHADER_VERTEX].MaxLocalParams <= (1 << INST_INDEX_BITS));
assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTemps <= (1 << INST_INDEX_BITS));
assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxLocalParams <= (1 << INST_INDEX_BITS));
assert(ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents <= 4 * MAX_UNIFORMS);
assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents <= 4 * MAX_UNIFORMS);
assert(ctx->Const.Program[MESA_SHADER_VERTEX].MaxAddressOffset <= (1 << INST_INDEX_BITS));
assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxAddressOffset <= (1 << INST_INDEX_BITS));
/* If this fails, increase prog_instruction::TexSrcUnit size */
STATIC_ASSERT(MAX_TEXTURE_UNITS <= (1 << 5));
/* If this fails, increase prog_instruction::TexSrcTarget size */
STATIC_ASSERT(NUM_TEXTURE_TARGETS <= (1 << 4));
ctx->Program.ErrorPos = -1;
ctx->Program.ErrorString = strdup("");
ctx->VertexProgram._VaryingInputs = VERT_BIT_ALL;
ctx->VertexProgram.Enabled = GL_FALSE;
ctx->VertexProgram.PointSizeEnabled =
_mesa_is_gles2(ctx) ? GL_TRUE : GL_FALSE;
ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
_mesa_reference_program(ctx, &ctx->VertexProgram.Current,
ctx->Shared->DefaultVertexProgram);
assert(ctx->VertexProgram.Current);
ctx->VertexProgram.Cache = _mesa_new_program_cache();
ctx->FragmentProgram.Enabled = GL_FALSE;
_mesa_reference_program(ctx, &ctx->FragmentProgram.Current,
ctx->Shared->DefaultFragmentProgram);
assert(ctx->FragmentProgram.Current);
ctx->FragmentProgram.Cache = _mesa_new_program_cache();
_mesa_reset_vertex_processing_mode(ctx);
/* XXX probably move this stuff */
ctx->ATIFragmentShader.Enabled = GL_FALSE;
ctx->ATIFragmentShader.Current = ctx->Shared->DefaultFragmentShader;
assert(ctx->ATIFragmentShader.Current);
ctx->ATIFragmentShader.Current->RefCount++;
}
/**
* Free a context's vertex/fragment program state
*/
void
_mesa_free_program_data(struct gl_context *ctx)
{
_mesa_reference_program(ctx, &ctx->VertexProgram.Current, NULL);
_mesa_delete_program_cache(ctx, ctx->VertexProgram.Cache);
_mesa_reference_program(ctx, &ctx->FragmentProgram.Current, NULL);
_mesa_delete_shader_cache(ctx, ctx->FragmentProgram.Cache);
/* XXX probably move this stuff */
if (ctx->ATIFragmentShader.Current) {
ctx->ATIFragmentShader.Current->RefCount--;
if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
free(ctx->ATIFragmentShader.Current);
}
}
free((void *) ctx->Program.ErrorString);
}
/**
* Update the default program objects in the given context to reference those
* specified in the shared state and release those referencing the old
* shared state.
*/
void
_mesa_update_default_objects_program(struct gl_context *ctx)
{
_mesa_reference_program(ctx, &ctx->VertexProgram.Current,
ctx->Shared->DefaultVertexProgram);
assert(ctx->VertexProgram.Current);
_mesa_reference_program(ctx, &ctx->FragmentProgram.Current,
ctx->Shared->DefaultFragmentProgram);
assert(ctx->FragmentProgram.Current);
/* XXX probably move this stuff */
if (ctx->ATIFragmentShader.Current) {
ctx->ATIFragmentShader.Current->RefCount--;
if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
free(ctx->ATIFragmentShader.Current);
}
}
ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
assert(ctx->ATIFragmentShader.Current);
ctx->ATIFragmentShader.Current->RefCount++;
}
/**
* Set the vertex/fragment program error state (position and error string).
* This is generally called from within the parsers.
*/
void
_mesa_set_program_error(struct gl_context *ctx, GLint pos, const char *string)
{
ctx->Program.ErrorPos = pos;
free((void *) ctx->Program.ErrorString);
if (!string)
string = "";
ctx->Program.ErrorString = strdup(string);
}
/**
* Initialize a new gl_program object.
*/
struct gl_program *
_mesa_init_gl_program(struct gl_program *prog, gl_shader_stage stage,
GLuint id, bool is_arb_asm)
{
if (!prog)
return NULL;
memset(prog, 0, sizeof(*prog));
prog->Id = id;
prog->Target = _mesa_shader_stage_to_program(stage);
prog->RefCount = 1;
prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
prog->info.stage = stage;
prog->info.use_legacy_math_rules = is_arb_asm;
/* Uniforms that lack an initializer in the shader code have an initial
* value of zero. This includes sampler uniforms.
*
* Page 24 (page 30 of the PDF) of the GLSL 1.20 spec says:
*
* "The link time initial value is either the value of the variable's
* initializer, if present, or 0 if no initializer is present. Sampler
* types cannot have initializers."
*
* So we only initialise ARB assembly style programs.
*/
if (is_arb_asm) {
/* default mapping from samplers to texture units */
for (unsigned i = 0; i < MAX_SAMPLERS; i++)
prog->SamplerUnits[i] = i;
}
return prog;
}
struct gl_program *
_mesa_new_program(struct gl_context *ctx, gl_shader_stage stage, GLuint id,
bool is_arb_asm)
{
struct gl_program *prog;
switch (stage) {
case MESA_SHADER_VERTEX:
prog = (struct gl_program*)rzalloc(NULL, struct gl_vertex_program);
break;
default:
prog = rzalloc(NULL, struct gl_program);
break;
}
return _mesa_init_gl_program(prog, stage, id, is_arb_asm);
}
/**
* Delete a program and remove it from the hash table, ignoring the
* reference count.
*/
void
_mesa_delete_program(struct gl_context *ctx, struct gl_program *prog)
{
struct st_context *st = st_context(ctx);
assert(prog);
assert(prog->RefCount==0);
st_release_variants(st, prog);
free(prog->serialized_nir);
if (prog == &_mesa_DummyProgram)
return;
if (prog->Parameters) {
_mesa_free_parameter_list(prog->Parameters);
}
if (prog->nir) {
ralloc_free(prog->nir);
}
if (prog->sh.BindlessSamplers) {
ralloc_free(prog->sh.BindlessSamplers);
}
if (prog->sh.BindlessImages) {
ralloc_free(prog->sh.BindlessImages);
}
if (prog->driver_cache_blob) {
ralloc_free(prog->driver_cache_blob);
}
ralloc_free(prog);
}
/**
* Return the gl_program object for a given ID.
* Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
* casts elsewhere.
*/
struct gl_program *
_mesa_lookup_program(struct gl_context *ctx, GLuint id)
{
if (id)
return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
else
return NULL;
}
/**
* Reference counting for vertex/fragment programs
* This is normally only called from the _mesa_reference_program() macro
* when there's a real pointer change.
*/
void
_mesa_reference_program_(struct gl_context *ctx,
struct gl_program **ptr,
struct gl_program *prog)
{
#ifndef NDEBUG
assert(ptr);
if (*ptr && prog) {
/* sanity check */
if ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB)
assert(prog->Target == GL_VERTEX_PROGRAM_ARB);
else if ((*ptr)->Target == GL_FRAGMENT_PROGRAM_ARB)
assert(prog->Target == GL_FRAGMENT_PROGRAM_ARB ||
prog->Target == GL_FRAGMENT_PROGRAM_NV);
else if ((*ptr)->Target == GL_GEOMETRY_PROGRAM_NV)
assert(prog->Target == GL_GEOMETRY_PROGRAM_NV);
}
#endif
if (*ptr) {
struct gl_program *oldProg = *ptr;
assert(oldProg->RefCount > 0);
if (p_atomic_dec_zero(&oldProg->RefCount)) {
assert(ctx);
_mesa_reference_shader_program_data(&oldProg->sh.data, NULL);
_mesa_delete_program(ctx, oldProg);
}
*ptr = NULL;
}
assert(!*ptr);
if (prog) {
p_atomic_inc(&prog->RefCount);
}
*ptr = prog;
}
/* Gets the minimum number of shader invocations per fragment.
* This function is useful to determine if we need to do per
* sample shading or per fragment shading.
*/
GLint
_mesa_get_min_invocations_per_fragment(struct gl_context *ctx,
const struct gl_program *prog)
{
/* From ARB_sample_shading specification:
* "Using gl_SampleID in a fragment shader causes the entire shader
* to be evaluated per-sample."
*
* "Using gl_SamplePosition in a fragment shader causes the entire
* shader to be evaluated per-sample."
*
* "If MULTISAMPLE or SAMPLE_SHADING_ARB is disabled, sample shading
* has no effect."
*/
if (ctx->Multisample.Enabled) {
/* The ARB_gpu_shader5 specification says:
*
* "Use of the "sample" qualifier on a fragment shader input
* forces per-sample shading"
*/
if (prog->info.fs.uses_sample_qualifier ||
BITSET_TEST(prog->info.system_values_read, SYSTEM_VALUE_SAMPLE_ID) ||
BITSET_TEST(prog->info.system_values_read, SYSTEM_VALUE_SAMPLE_POS))
return MAX2(_mesa_geometric_samples(ctx->DrawBuffer), 1);
else if (ctx->Multisample.SampleShading)
return MAX2(ceilf(ctx->Multisample.MinSampleShadingValue *
_mesa_geometric_samples(ctx->DrawBuffer)), 1);
else
return 1;
}
return 1;
}
GLbitfield
gl_external_samplers(const struct gl_program *prog)
{
GLbitfield external_samplers = 0;
GLbitfield mask = prog->SamplersUsed;
while (mask) {
int idx = u_bit_scan(&mask);
if (prog->sh.SamplerTargets[idx] == TEXTURE_EXTERNAL_INDEX)
external_samplers |= (1 << idx);
}
return external_samplers;
}
static int compare_state_var(const void *a1, const void *a2)
{
const struct gl_program_parameter *p1 =
(const struct gl_program_parameter *)a1;
const struct gl_program_parameter *p2 =
(const struct gl_program_parameter *)a2;
for (unsigned i = 0; i < STATE_LENGTH; i++) {
if (p1->StateIndexes[i] != p2->StateIndexes[i])
return p1->StateIndexes[i] - p2->StateIndexes[i];
}
return 0;
}
void
_mesa_add_separate_state_parameters(struct gl_program *prog,
struct gl_program_parameter_list *state_params)
{
unsigned num_state_params = state_params->NumParameters;
/* All state parameters should be vec4s. */
for (unsigned i = 0; i < num_state_params; i++) {
assert(state_params->Parameters[i].Type == PROGRAM_STATE_VAR);
assert(state_params->Parameters[i].Size == 4);
assert(state_params->Parameters[i].ValueOffset == i * 4);
}
/* Sort state parameters to facilitate better parameter merging. */
qsort(state_params->Parameters, num_state_params,
sizeof(state_params->Parameters[0]), compare_state_var);
unsigned *remap = malloc(num_state_params * sizeof(unsigned));
/* Add state parameters to the end of the parameter list. */
for (unsigned i = 0; i < num_state_params; i++) {
unsigned old_index = state_params->Parameters[i].ValueOffset / 4;
remap[old_index] =
_mesa_add_parameter(prog->Parameters, PROGRAM_STATE_VAR,
state_params->Parameters[i].Name,
state_params->Parameters[i].Size,
GL_NONE, NULL,
state_params->Parameters[i].StateIndexes,
state_params->Parameters[i].Padded);
prog->Parameters->StateFlags |=
_mesa_program_state_flags(state_params->Parameters[i].StateIndexes);
}
/* Fix up state parameter offsets in instructions. */
int num_instr = prog->arb.NumInstructions;
struct prog_instruction *instrs = prog->arb.Instructions;
/* Fix src indices after sorting. */
for (unsigned i = 0; i < num_instr; i++) {
struct prog_instruction *inst = instrs + i;
unsigned num_src = _mesa_num_inst_src_regs(inst->Opcode);
for (unsigned j = 0; j < num_src; j++) {
if (inst->SrcReg[j].File == PROGRAM_STATE_VAR)
inst->SrcReg[j].Index = remap[inst->SrcReg[j].Index];
}
}
free(remap);
}
|