blob: 52254e9365f9bb249303e4b857e2d3d957a509ea [file] [log] [blame]
Michal Krol2861e732004-03-29 11:09:34 +00001/*
2 * Mesa 3-D graphics library
Brian942ee022007-02-09 15:40:00 -07003 * Version: 6.5.3
Michal Krol2861e732004-03-29 11:09:34 +00004 *
Brian942ee022007-02-09 15:40:00 -07005 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
Michal Krol2861e732004-03-29 11:09:34 +00006 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR 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
José Fonseca101d1a62008-07-23 21:06:01 +090032#include "main/glheader.h"
33#include "main/context.h"
34#include "main/hash.h"
Vinson Lee21750a22011-01-09 00:47:33 -080035#include "main/mfeatures.h"
Brian0560d812006-12-14 15:01:28 -070036#include "program.h"
Brianb26aae62007-10-31 12:00:38 -060037#include "prog_cache.h"
Brian0560d812006-12-14 15:01:28 -070038#include "prog_parameter.h"
39#include "prog_instruction.h"
Michal Krol2861e732004-03-29 11:09:34 +000040
41
Brian942ee022007-02-09 15:40:00 -070042/**
43 * A pointer to this dummy program is put into the hash table when
Brian Paul765f1a12004-09-14 22:28:27 +000044 * glGenPrograms is called.
45 */
Brian Paul122629f2006-07-20 16:49:57 +000046struct gl_program _mesa_DummyProgram;
Brian Paul765f1a12004-09-14 22:28:27 +000047
48
Michal Krol2861e732004-03-29 11:09:34 +000049/**
Brian Paul21841f02004-08-14 14:28:11 +000050 * Init context's vertex/fragment program state
Michal Krol2861e732004-03-29 11:09:34 +000051 */
52void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -040053_mesa_init_program(struct gl_context *ctx)
Michal Krol2861e732004-03-29 11:09:34 +000054{
55 GLuint i;
56
Brian Paul5b2f8dc2009-02-18 11:47:40 -070057 /*
58 * If this assertion fails, we need to increase the field
Brian Paul4567b472010-08-10 08:38:12 -060059 * size for register indexes (see INST_INDEX_BITS).
Brian Paul5b2f8dc2009-02-18 11:47:40 -070060 */
61 ASSERT(ctx->Const.VertexProgram.MaxUniformComponents / 4
62 <= (1 << INST_INDEX_BITS));
63 ASSERT(ctx->Const.FragmentProgram.MaxUniformComponents / 4
64 <= (1 << INST_INDEX_BITS));
65
Brian Paul4567b472010-08-10 08:38:12 -060066 ASSERT(ctx->Const.VertexProgram.MaxTemps <= (1 << INST_INDEX_BITS));
67 ASSERT(ctx->Const.VertexProgram.MaxLocalParams <= (1 << INST_INDEX_BITS));
68 ASSERT(ctx->Const.FragmentProgram.MaxTemps <= (1 << INST_INDEX_BITS));
69 ASSERT(ctx->Const.FragmentProgram.MaxLocalParams <= (1 << INST_INDEX_BITS));
70
71 ASSERT(ctx->Const.VertexProgram.MaxUniformComponents <= 4 * MAX_UNIFORMS);
72 ASSERT(ctx->Const.FragmentProgram.MaxUniformComponents <= 4 * MAX_UNIFORMS);
73
Brian Pauldd528f02009-08-26 10:57:18 -060074 /* If this fails, increase prog_instruction::TexSrcUnit size */
75 ASSERT(MAX_TEXTURE_UNITS < (1 << 5));
76
77 /* If this fails, increase prog_instruction::TexSrcTarget size */
78 ASSERT(NUM_TEXTURE_TARGETS < (1 << 3));
79
Michal Krol2861e732004-03-29 11:09:34 +000080 ctx->Program.ErrorPos = -1;
81 ctx->Program.ErrorString = _mesa_strdup("");
82
83#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
84 ctx->VertexProgram.Enabled = GL_FALSE;
Brian Paulaf3d9db2008-08-12 10:00:36 -060085#if FEATURE_es2_glsl
Kristian Høgsbergf67b0202010-05-24 10:01:38 -040086 ctx->VertexProgram.PointSizeEnabled =
87 (ctx->API == API_OPENGLES2) ? GL_TRUE : GL_FALSE;
Brian Paulaf3d9db2008-08-12 10:00:36 -060088#else
Michal Krol2861e732004-03-29 11:09:34 +000089 ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
Brian Paulaf3d9db2008-08-12 10:00:36 -060090#endif
Michal Krol2861e732004-03-29 11:09:34 +000091 ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
Briandf43fb62008-05-06 23:08:51 -060092 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
93 ctx->Shared->DefaultVertexProgram);
Michal Krol2861e732004-03-29 11:09:34 +000094 assert(ctx->VertexProgram.Current);
Michal Krol2861e732004-03-29 11:09:34 +000095 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
96 ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
97 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
98 }
Brianb26aae62007-10-31 12:00:38 -060099 ctx->VertexProgram.Cache = _mesa_new_program_cache();
Michal Krol2861e732004-03-29 11:09:34 +0000100#endif
101
102#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
103 ctx->FragmentProgram.Enabled = GL_FALSE;
Briandf43fb62008-05-06 23:08:51 -0600104 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
105 ctx->Shared->DefaultFragmentProgram);
Michal Krol2861e732004-03-29 11:09:34 +0000106 assert(ctx->FragmentProgram.Current);
Brianb26aae62007-10-31 12:00:38 -0600107 ctx->FragmentProgram.Cache = _mesa_new_program_cache();
Michal Krol2861e732004-03-29 11:09:34 +0000108#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +0000109
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400110#if FEATURE_ARB_geometry_shader4
111 ctx->GeometryProgram.Enabled = GL_FALSE;
112 /* right now by default we don't have a geometry program */
113 _mesa_reference_geomprog(ctx, &ctx->GeometryProgram.Current,
114 NULL);
115 ctx->GeometryProgram.Cache = _mesa_new_program_cache();
116#endif
Brianb26aae62007-10-31 12:00:38 -0600117
Brian Paul63d68302005-11-19 16:43:04 +0000118 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +0000119#if FEATURE_ATI_fragment_shader
120 ctx->ATIFragmentShader.Enabled = GL_FALSE;
Brian Paulb7eea9a2008-07-29 17:19:25 -0600121 ctx->ATIFragmentShader.Current = ctx->Shared->DefaultFragmentShader;
Dave Airlie7f752fe2004-12-19 03:06:59 +0000122 assert(ctx->ATIFragmentShader.Current);
Brian Paul63d68302005-11-19 16:43:04 +0000123 ctx->ATIFragmentShader.Current->RefCount++;
Dave Airlie7f752fe2004-12-19 03:06:59 +0000124#endif
Michal Krol2861e732004-03-29 11:09:34 +0000125}
126
127
128/**
Brian Paul21841f02004-08-14 14:28:11 +0000129 * Free a context's vertex/fragment program state
130 */
131void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400132_mesa_free_program_data(struct gl_context *ctx)
Brian Paul21841f02004-08-14 14:28:11 +0000133{
Roland Scheidegger93da6732006-03-01 23:11:14 +0000134#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Briandf43fb62008-05-06 23:08:51 -0600135 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL);
Brian1631a952007-12-26 06:57:24 -0700136 _mesa_delete_program_cache(ctx, ctx->VertexProgram.Cache);
Brian Paul21841f02004-08-14 14:28:11 +0000137#endif
Roland Scheidegger93da6732006-03-01 23:11:14 +0000138#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Briandf43fb62008-05-06 23:08:51 -0600139 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL);
Brian1631a952007-12-26 06:57:24 -0700140 _mesa_delete_program_cache(ctx, ctx->FragmentProgram.Cache);
Brian Paul21841f02004-08-14 14:28:11 +0000141#endif
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400142#if FEATURE_ARB_geometry_shader4
143 _mesa_reference_geomprog(ctx, &ctx->GeometryProgram.Current, NULL);
144 _mesa_delete_program_cache(ctx, ctx->GeometryProgram.Cache);
145#endif
Brian Paul63d68302005-11-19 16:43:04 +0000146 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +0000147#if FEATURE_ATI_fragment_shader
148 if (ctx->ATIFragmentShader.Current) {
Brian Paul63d68302005-11-19 16:43:04 +0000149 ctx->ATIFragmentShader.Current->RefCount--;
150 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500151 free(ctx->ATIFragmentShader.Current);
Brian Paul63d68302005-11-19 16:43:04 +0000152 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000153 }
154#endif
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500155 free((void *) ctx->Program.ErrorString);
Brian Paul21841f02004-08-14 14:28:11 +0000156}
157
158
Brian4b654d42007-08-23 08:53:43 +0100159/**
160 * Update the default program objects in the given context to reference those
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200161 * specified in the shared state and release those referencing the old
Brian4b654d42007-08-23 08:53:43 +0100162 * shared state.
163 */
164void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400165_mesa_update_default_objects_program(struct gl_context *ctx)
Brian4b654d42007-08-23 08:53:43 +0100166{
167#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Brian Paul57e222d2008-05-14 12:10:45 -0600168 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
169 (struct gl_vertex_program *)
170 ctx->Shared->DefaultVertexProgram);
Brian4b654d42007-08-23 08:53:43 +0100171 assert(ctx->VertexProgram.Current);
Brian4b654d42007-08-23 08:53:43 +0100172#endif
173
174#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Brian Paul57e222d2008-05-14 12:10:45 -0600175 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
176 (struct gl_fragment_program *)
177 ctx->Shared->DefaultFragmentProgram);
Brian4b654d42007-08-23 08:53:43 +0100178 assert(ctx->FragmentProgram.Current);
Brian4b654d42007-08-23 08:53:43 +0100179#endif
180
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400181#if FEATURE_ARB_geometry_shader4
182 _mesa_reference_geomprog(ctx, &ctx->GeometryProgram.Current,
183 (struct gl_geometry_program *)
184 ctx->Shared->DefaultGeometryProgram);
185#endif
186
Brian4b654d42007-08-23 08:53:43 +0100187 /* XXX probably move this stuff */
188#if FEATURE_ATI_fragment_shader
189 if (ctx->ATIFragmentShader.Current) {
190 ctx->ATIFragmentShader.Current->RefCount--;
191 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500192 free(ctx->ATIFragmentShader.Current);
Brian4b654d42007-08-23 08:53:43 +0100193 }
194 }
195 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
196 assert(ctx->ATIFragmentShader.Current);
197 ctx->ATIFragmentShader.Current->RefCount++;
198#endif
199}
Brian Paul21841f02004-08-14 14:28:11 +0000200
201
202/**
Michal Krol2861e732004-03-29 11:09:34 +0000203 * Set the vertex/fragment program error state (position and error string).
204 * This is generally called from within the parsers.
205 */
206void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400207_mesa_set_program_error(struct gl_context *ctx, GLint pos, const char *string)
Michal Krol2861e732004-03-29 11:09:34 +0000208{
209 ctx->Program.ErrorPos = pos;
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500210 free((void *) ctx->Program.ErrorString);
Michal Krol2861e732004-03-29 11:09:34 +0000211 if (!string)
212 string = "";
213 ctx->Program.ErrorString = _mesa_strdup(string);
214}
215
216
217/**
218 * Find the line number and column for 'pos' within 'string'.
219 * Return a copy of the line which contains 'pos'. Free the line with
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500220 * free().
Michal Krol2861e732004-03-29 11:09:34 +0000221 * \param string the program string
222 * \param pos the position within the string
223 * \param line returns the line number corresponding to 'pos'.
224 * \param col returns the column number corresponding to 'pos'.
225 * \return copy of the line containing 'pos'.
226 */
227const GLubyte *
228_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
229 GLint *line, GLint *col)
230{
231 const GLubyte *lineStart = string;
232 const GLubyte *p = string;
233 GLubyte *s;
234 int len;
235
236 *line = 1;
237
238 while (p != pos) {
239 if (*p == (GLubyte) '\n') {
240 (*line)++;
241 lineStart = p + 1;
242 }
243 p++;
244 }
245
246 *col = (pos - lineStart) + 1;
247
248 /* return copy of this line */
249 while (*p != 0 && *p != '\n')
250 p++;
251 len = p - lineStart;
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500252 s = (GLubyte *) malloc(len + 1);
Kenneth Graunkec7ac48622010-02-18 23:50:59 -0800253 memcpy(s, lineStart, len);
Michal Krol2861e732004-03-29 11:09:34 +0000254 s[len] = 0;
255
256 return s;
257}
258
259
Brian Paul765f1a12004-09-14 22:28:27 +0000260/**
261 * Initialize a new vertex/fragment program object.
262 */
Brian Paul122629f2006-07-20 16:49:57 +0000263static struct gl_program *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400264_mesa_init_program_struct( struct gl_context *ctx, struct gl_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000265 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000266{
Brian Paula6c423d2004-08-25 15:59:48 +0000267 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000268 if (prog) {
Brian Paul0c78c762008-05-18 15:46:26 -0600269 GLuint i;
Brian Paul6bf1ea82010-02-19 08:32:36 -0700270 memset(prog, 0, sizeof(*prog));
Michal Krol2861e732004-03-29 11:09:34 +0000271 prog->Id = id;
272 prog->Target = target;
273 prog->Resident = GL_TRUE;
274 prog->RefCount = 1;
Brian Paul308b85f2006-11-23 15:58:30 +0000275 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
Brian Paul0c78c762008-05-18 15:46:26 -0600276
277 /* default mapping from samplers to texture units */
278 for (i = 0; i < MAX_SAMPLERS; i++)
279 prog->SamplerUnits[i] = i;
Michal Krol2861e732004-03-29 11:09:34 +0000280 }
281
282 return prog;
283}
284
Brian Paul765f1a12004-09-14 22:28:27 +0000285
286/**
287 * Initialize a new fragment program object.
288 */
Brian Paul122629f2006-07-20 16:49:57 +0000289struct gl_program *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400290_mesa_init_fragment_program( struct gl_context *ctx, struct gl_fragment_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000291 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000292{
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200293 if (prog)
Michal Krol2861e732004-03-29 11:09:34 +0000294 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
295 else
296 return NULL;
297}
298
Brian Paul765f1a12004-09-14 22:28:27 +0000299
300/**
301 * Initialize a new vertex program object.
302 */
Brian Paul122629f2006-07-20 16:49:57 +0000303struct gl_program *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400304_mesa_init_vertex_program( struct gl_context *ctx, struct gl_vertex_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000305 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000306{
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200307 if (prog)
Michal Krol2861e732004-03-29 11:09:34 +0000308 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
309 else
310 return NULL;
311}
312
313
314/**
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400315 * Initialize a new geometry program object.
316 */
317struct gl_program *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400318_mesa_init_geometry_program( struct gl_context *ctx, struct gl_geometry_program *prog,
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400319 GLenum target, GLuint id)
320{
321 if (prog)
322 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
323 else
324 return NULL;
325}
326
327
328/**
Michal Krol2861e732004-03-29 11:09:34 +0000329 * Allocate and initialize a new fragment/vertex program object but
330 * don't put it into the program hash table. Called via
331 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
332 * device driver function to implement OO deriviation with additional
333 * types not understood by this function.
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200334 *
Michal Krol2861e732004-03-29 11:09:34 +0000335 * \param ctx context
336 * \param id program id/number
337 * \param target program target/type
338 * \return pointer to new program object
339 */
Brian Paul122629f2006-07-20 16:49:57 +0000340struct gl_program *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400341_mesa_new_program(struct gl_context *ctx, GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000342{
Brian Paula3e86d42008-05-16 09:56:11 -0600343 struct gl_program *prog;
Michal Krol2861e732004-03-29 11:09:34 +0000344 switch (target) {
345 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
Brian Paulc5af2ed2009-04-18 10:08:54 -0600346 case GL_VERTEX_STATE_PROGRAM_NV:
Brian Paula3e86d42008-05-16 09:56:11 -0600347 prog = _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
Brian Paul122629f2006-07-20 16:49:57 +0000348 target, id );
Brian Paula3e86d42008-05-16 09:56:11 -0600349 break;
Michal Krol2861e732004-03-29 11:09:34 +0000350 case GL_FRAGMENT_PROGRAM_NV:
351 case GL_FRAGMENT_PROGRAM_ARB:
Brian Paula3e86d42008-05-16 09:56:11 -0600352 prog =_mesa_init_fragment_program(ctx,
Brian Paul122629f2006-07-20 16:49:57 +0000353 CALLOC_STRUCT(gl_fragment_program),
354 target, id );
Brian Paula3e86d42008-05-16 09:56:11 -0600355 break;
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400356 case MESA_GEOMETRY_PROGRAM:
357 prog = _mesa_init_geometry_program(ctx,
358 CALLOC_STRUCT(gl_geometry_program),
359 target, id);
360 break;
Michal Krol2861e732004-03-29 11:09:34 +0000361 default:
362 _mesa_problem(ctx, "bad target in _mesa_new_program");
Brian Paula3e86d42008-05-16 09:56:11 -0600363 prog = NULL;
Michal Krol2861e732004-03-29 11:09:34 +0000364 }
Brian Paula3e86d42008-05-16 09:56:11 -0600365 return prog;
Michal Krol2861e732004-03-29 11:09:34 +0000366}
367
368
369/**
370 * Delete a program and remove it from the hash table, ignoring the
371 * reference count.
372 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
373 * by a device driver function.
374 */
375void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400376_mesa_delete_program(struct gl_context *ctx, struct gl_program *prog)
Michal Krol2861e732004-03-29 11:09:34 +0000377{
Brian Paula6c423d2004-08-25 15:59:48 +0000378 (void) ctx;
Brian Paul57e222d2008-05-14 12:10:45 -0600379 ASSERT(prog);
380 ASSERT(prog->RefCount==0);
Michal Krol2861e732004-03-29 11:09:34 +0000381
Brian Paul308b85f2006-11-23 15:58:30 +0000382 if (prog == &_mesa_DummyProgram)
383 return;
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200384
Michal Krol2861e732004-03-29 11:09:34 +0000385 if (prog->String)
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500386 free(prog->String);
Brian Paulde997602005-11-12 17:53:14 +0000387
Brian Paul19ad9cf2008-05-14 12:39:41 -0600388 _mesa_free_instructions(prog->Instructions, prog->NumInstructions);
Brian Paulde997602005-11-12 17:53:14 +0000389
Brian Paul65a51c02006-05-24 03:30:31 +0000390 if (prog->Parameters) {
Brian Paulde997602005-11-12 17:53:14 +0000391 _mesa_free_parameter_list(prog->Parameters);
Brian Paul65a51c02006-05-24 03:30:31 +0000392 }
Brianfe1d01c2006-12-13 14:54:47 -0700393 if (prog->Varying) {
394 _mesa_free_parameter_list(prog->Varying);
395 }
Brian3493e862007-03-24 16:18:13 -0600396 if (prog->Attributes) {
397 _mesa_free_parameter_list(prog->Attributes);
398 }
Brianfe1d01c2006-12-13 14:54:47 -0700399
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500400 free(prog);
Michal Krol2861e732004-03-29 11:09:34 +0000401}
402
403
Brian Paul4d12a052006-08-23 23:10:14 +0000404/**
405 * Return the gl_program object for a given ID.
406 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
407 * casts elsewhere.
408 */
409struct gl_program *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400410_mesa_lookup_program(struct gl_context *ctx, GLuint id)
Brian Paul4d12a052006-08-23 23:10:14 +0000411{
412 if (id)
413 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
414 else
415 return NULL;
416}
417
Michal Krol2861e732004-03-29 11:09:34 +0000418
Brian Paul3b9b8de2006-08-24 21:57:36 +0000419/**
Briandf43fb62008-05-06 23:08:51 -0600420 * Reference counting for vertex/fragment programs
421 */
422void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400423_mesa_reference_program(struct gl_context *ctx,
Briandf43fb62008-05-06 23:08:51 -0600424 struct gl_program **ptr,
425 struct gl_program *prog)
426{
427 assert(ptr);
428 if (*ptr && prog) {
429 /* sanity check */
Brian Paul4bc39c52008-09-26 07:40:45 -0600430 if ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB)
431 ASSERT(prog->Target == GL_VERTEX_PROGRAM_ARB);
432 else if ((*ptr)->Target == GL_FRAGMENT_PROGRAM_ARB)
433 ASSERT(prog->Target == GL_FRAGMENT_PROGRAM_ARB ||
434 prog->Target == GL_FRAGMENT_PROGRAM_NV);
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400435 else if ((*ptr)->Target == MESA_GEOMETRY_PROGRAM)
436 ASSERT(prog->Target == MESA_GEOMETRY_PROGRAM);
Briandf43fb62008-05-06 23:08:51 -0600437 }
438 if (*ptr == prog) {
439 return; /* no change */
440 }
441 if (*ptr) {
442 GLboolean deleteFlag;
443
444 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
Brian Paulb4e75d62008-05-08 10:59:31 -0600445#if 0
Brian Paula3e86d42008-05-16 09:56:11 -0600446 printf("Program %p ID=%u Target=%s Refcount-- to %d\n",
447 *ptr, (*ptr)->Id,
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400448 ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB ? "VP" :
449 ((*ptr)->Target == MESA_GEOMETRY_PROGRAM ? "GP" : "FP")),
Brian Paula3e86d42008-05-16 09:56:11 -0600450 (*ptr)->RefCount - 1);
Briandf43fb62008-05-06 23:08:51 -0600451#endif
452 ASSERT((*ptr)->RefCount > 0);
453 (*ptr)->RefCount--;
454
455 deleteFlag = ((*ptr)->RefCount == 0);
456 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200457
Briandf43fb62008-05-06 23:08:51 -0600458 if (deleteFlag) {
459 ASSERT(ctx);
460 ctx->Driver.DeleteProgram(ctx, *ptr);
461 }
462
463 *ptr = NULL;
464 }
465
466 assert(!*ptr);
467 if (prog) {
468 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
469 prog->RefCount++;
Brian Paulb4e75d62008-05-08 10:59:31 -0600470#if 0
Brian Paula3e86d42008-05-16 09:56:11 -0600471 printf("Program %p ID=%u Target=%s Refcount++ to %d\n",
472 prog, prog->Id,
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400473 (prog->Target == GL_VERTEX_PROGRAM_ARB ? "VP" :
474 (prog->Target == MESA_GEOMETRY_PROGRAM ? "GP" : "FP")),
Brian Paula3e86d42008-05-16 09:56:11 -0600475 prog->RefCount);
Briandf43fb62008-05-06 23:08:51 -0600476#endif
477 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
478 }
479
480 *ptr = prog;
481}
482
483
484/**
Brianb2a3a852006-12-14 13:56:58 -0700485 * Return a copy of a program.
486 * XXX Problem here if the program object is actually OO-derivation
487 * made by a device driver.
488 */
489struct gl_program *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400490_mesa_clone_program(struct gl_context *ctx, const struct gl_program *prog)
Brianb2a3a852006-12-14 13:56:58 -0700491{
492 struct gl_program *clone;
493
Brian5b6858c2007-07-24 09:56:44 -0600494 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
Brianb2a3a852006-12-14 13:56:58 -0700495 if (!clone)
496 return NULL;
497
498 assert(clone->Target == prog->Target);
Briandf43fb62008-05-06 23:08:51 -0600499 assert(clone->RefCount == 1);
500
Brianb2a3a852006-12-14 13:56:58 -0700501 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
Brianb2a3a852006-12-14 13:56:58 -0700502 clone->Format = prog->Format;
503 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
504 if (!clone->Instructions) {
Brian Paul57e222d2008-05-14 12:10:45 -0600505 _mesa_reference_program(ctx, &clone, NULL);
Brianb2a3a852006-12-14 13:56:58 -0700506 return NULL;
507 }
Brian12229f12007-03-22 09:11:26 -0600508 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
509 prog->NumInstructions);
Brianb2a3a852006-12-14 13:56:58 -0700510 clone->InputsRead = prog->InputsRead;
511 clone->OutputsWritten = prog->OutputsWritten;
Brian Paul0c78c762008-05-18 15:46:26 -0600512 clone->SamplersUsed = prog->SamplersUsed;
Nicolai Haehnle82635aa2008-07-05 18:03:44 +0200513 clone->ShadowSamplers = prog->ShadowSamplers;
Brianc9db2232007-01-04 17:22:19 -0700514 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
515
Brian2e76f0a2006-12-19 09:52:07 -0700516 if (prog->Parameters)
517 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
Brianb2a3a852006-12-14 13:56:58 -0700518 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian2e76f0a2006-12-19 09:52:07 -0700519 if (prog->Varying)
520 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
Brian3209c3e2007-01-09 17:49:24 -0700521 if (prog->Attributes)
522 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
Brianb2a3a852006-12-14 13:56:58 -0700523 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian Paul56643092010-07-30 14:24:23 -0600524 clone->IndirectRegisterFiles = prog->IndirectRegisterFiles;
Brianb2a3a852006-12-14 13:56:58 -0700525 clone->NumInstructions = prog->NumInstructions;
526 clone->NumTemporaries = prog->NumTemporaries;
527 clone->NumParameters = prog->NumParameters;
528 clone->NumAttributes = prog->NumAttributes;
529 clone->NumAddressRegs = prog->NumAddressRegs;
530 clone->NumNativeInstructions = prog->NumNativeInstructions;
531 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
532 clone->NumNativeParameters = prog->NumNativeParameters;
533 clone->NumNativeAttributes = prog->NumNativeAttributes;
534 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -0700535 clone->NumAluInstructions = prog->NumAluInstructions;
536 clone->NumTexInstructions = prog->NumTexInstructions;
537 clone->NumTexIndirections = prog->NumTexIndirections;
538 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
539 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
540 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
Brianb2a3a852006-12-14 13:56:58 -0700541
542 switch (prog->Target) {
543 case GL_VERTEX_PROGRAM_ARB:
544 {
545 const struct gl_vertex_program *vp
546 = (const struct gl_vertex_program *) prog;
547 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
548 vpc->IsPositionInvariant = vp->IsPositionInvariant;
Nicolai Hähnle736e1ae2009-09-09 19:56:57 +0200549 vpc->IsNVProgram = vp->IsNVProgram;
Brianb2a3a852006-12-14 13:56:58 -0700550 }
551 break;
552 case GL_FRAGMENT_PROGRAM_ARB:
553 {
554 const struct gl_fragment_program *fp
555 = (const struct gl_fragment_program *) prog;
556 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
Brianb2a3a852006-12-14 13:56:58 -0700557 fpc->FogOption = fp->FogOption;
558 fpc->UsesKill = fp->UsesKill;
Brian Paulb947b1d2010-02-13 13:51:38 -0700559 fpc->OriginUpperLeft = fp->OriginUpperLeft;
560 fpc->PixelCenterInteger = fp->PixelCenterInteger;
Brianb2a3a852006-12-14 13:56:58 -0700561 }
562 break;
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400563 case MESA_GEOMETRY_PROGRAM:
564 {
565 const struct gl_geometry_program *gp
566 = (const struct gl_geometry_program *) prog;
567 struct gl_geometry_program *gpc = (struct gl_geometry_program *) clone;
568 gpc->VerticesOut = gp->VerticesOut;
569 gpc->InputType = gp->InputType;
570 gpc->OutputType = gp->OutputType;
571 }
572 break;
Brianb2a3a852006-12-14 13:56:58 -0700573 default:
574 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
575 }
576
577 return clone;
578}
579
580
Brian Paul19ad9cf2008-05-14 12:39:41 -0600581/**
582 * Insert 'count' NOP instructions at 'start' in the given program.
583 * Adjust branch targets accordingly.
584 */
585GLboolean
586_mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
587{
588 const GLuint origLen = prog->NumInstructions;
589 const GLuint newLen = origLen + count;
590 struct prog_instruction *newInst;
591 GLuint i;
592
593 /* adjust branches */
594 for (i = 0; i < prog->NumInstructions; i++) {
595 struct prog_instruction *inst = prog->Instructions + i;
596 if (inst->BranchTarget > 0) {
José Fonseca457d7212008-06-24 11:34:46 +0900597 if ((GLuint)inst->BranchTarget >= start) {
Brian Paul19ad9cf2008-05-14 12:39:41 -0600598 inst->BranchTarget += count;
599 }
600 }
601 }
602
603 /* Alloc storage for new instructions */
604 newInst = _mesa_alloc_instructions(newLen);
605 if (!newInst) {
606 return GL_FALSE;
607 }
608
609 /* Copy 'start' instructions into new instruction buffer */
610 _mesa_copy_instructions(newInst, prog->Instructions, start);
611
612 /* init the new instructions */
613 _mesa_init_instructions(newInst + start, count);
614
615 /* Copy the remaining/tail instructions to new inst buffer */
616 _mesa_copy_instructions(newInst + start + count,
617 prog->Instructions + start,
618 origLen - start);
619
620 /* free old instructions */
621 _mesa_free_instructions(prog->Instructions, origLen);
622
623 /* install new instructions */
624 prog->Instructions = newInst;
625 prog->NumInstructions = newLen;
626
627 return GL_TRUE;
628}
629
Brianb2a3a852006-12-14 13:56:58 -0700630/**
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200631 * Delete 'count' instructions at 'start' in the given program.
632 * Adjust branch targets accordingly.
633 */
634GLboolean
635_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count)
636{
637 const GLuint origLen = prog->NumInstructions;
638 const GLuint newLen = origLen - count;
639 struct prog_instruction *newInst;
640 GLuint i;
641
642 /* adjust branches */
643 for (i = 0; i < prog->NumInstructions; i++) {
644 struct prog_instruction *inst = prog->Instructions + i;
645 if (inst->BranchTarget > 0) {
Brian Paul20fbb242010-01-27 17:03:04 -0700646 if (inst->BranchTarget > (GLint) start) {
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200647 inst->BranchTarget -= count;
648 }
649 }
650 }
651
652 /* Alloc storage for new instructions */
653 newInst = _mesa_alloc_instructions(newLen);
654 if (!newInst) {
655 return GL_FALSE;
656 }
657
658 /* Copy 'start' instructions into new instruction buffer */
659 _mesa_copy_instructions(newInst, prog->Instructions, start);
660
661 /* Copy the remaining/tail instructions to new inst buffer */
662 _mesa_copy_instructions(newInst + start,
663 prog->Instructions + start + count,
664 newLen - start);
665
666 /* free old instructions */
667 _mesa_free_instructions(prog->Instructions, origLen);
668
669 /* install new instructions */
670 prog->Instructions = newInst;
671 prog->NumInstructions = newLen;
672
673 return GL_TRUE;
674}
675
676
677/**
Brian Paul6ca948a2008-05-14 12:53:03 -0600678 * Search instructions for registers that match (oldFile, oldIndex),
679 * replacing them with (newFile, newIndex).
680 */
681static void
682replace_registers(struct prog_instruction *inst, GLuint numInst,
683 GLuint oldFile, GLuint oldIndex,
684 GLuint newFile, GLuint newIndex)
685{
686 GLuint i, j;
687 for (i = 0; i < numInst; i++) {
Brian Paul0c78c762008-05-18 15:46:26 -0600688 /* src regs */
Brian Paulb22a00b2010-04-09 10:03:31 -0600689 for (j = 0; j < _mesa_num_inst_src_regs(inst[i].Opcode); j++) {
Brian Paul6ca948a2008-05-14 12:53:03 -0600690 if (inst[i].SrcReg[j].File == oldFile &&
691 inst[i].SrcReg[j].Index == oldIndex) {
692 inst[i].SrcReg[j].File = newFile;
693 inst[i].SrcReg[j].Index = newIndex;
694 }
695 }
Brian Paul0c78c762008-05-18 15:46:26 -0600696 /* dst reg */
697 if (inst[i].DstReg.File == oldFile && inst[i].DstReg.Index == oldIndex) {
698 inst[i].DstReg.File = newFile;
699 inst[i].DstReg.Index = newIndex;
700 }
Brian Paul6ca948a2008-05-14 12:53:03 -0600701 }
702}
703
704
705/**
706 * Search instructions for references to program parameters. When found,
707 * increment the parameter index by 'offset'.
708 * Used when combining programs.
709 */
710static void
711adjust_param_indexes(struct prog_instruction *inst, GLuint numInst,
712 GLuint offset)
713{
714 GLuint i, j;
715 for (i = 0; i < numInst; i++) {
Brian Paulb22a00b2010-04-09 10:03:31 -0600716 for (j = 0; j < _mesa_num_inst_src_regs(inst[i].Opcode); j++) {
Brian Paul6ca948a2008-05-14 12:53:03 -0600717 GLuint f = inst[i].SrcReg[j].File;
718 if (f == PROGRAM_CONSTANT ||
719 f == PROGRAM_UNIFORM ||
720 f == PROGRAM_STATE_VAR) {
721 inst[i].SrcReg[j].Index += offset;
722 }
723 }
724 }
725}
726
727
728/**
729 * Combine two programs into one. Fix instructions so the outputs of
730 * the first program go to the inputs of the second program.
731 */
732struct gl_program *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400733_mesa_combine_programs(struct gl_context *ctx,
Brian Paul0c78c762008-05-18 15:46:26 -0600734 const struct gl_program *progA,
735 const struct gl_program *progB)
Brian Paul6ca948a2008-05-14 12:53:03 -0600736{
737 struct prog_instruction *newInst;
738 struct gl_program *newProg;
739 const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */
740 const GLuint lenB = progB->NumInstructions;
741 const GLuint numParamsA = _mesa_num_parameters(progA->Parameters);
742 const GLuint newLength = lenA + lenB;
Brian Paula2ddb3d2010-02-01 18:00:12 -0700743 GLboolean usedTemps[MAX_PROGRAM_TEMPS];
744 GLuint firstTemp = 0;
Brian Paul0c78c762008-05-18 15:46:26 -0600745 GLbitfield inputsB;
Brian Paul6ca948a2008-05-14 12:53:03 -0600746 GLuint i;
747
748 ASSERT(progA->Target == progB->Target);
749
750 newInst = _mesa_alloc_instructions(newLength);
751 if (!newInst)
752 return GL_FALSE;
753
754 _mesa_copy_instructions(newInst, progA->Instructions, lenA);
755 _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB);
756
757 /* adjust branch / instruction addresses for B's instructions */
758 for (i = 0; i < lenB; i++) {
759 newInst[lenA + i].BranchTarget += lenA;
760 }
761
762 newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0);
763 newProg->Instructions = newInst;
764 newProg->NumInstructions = newLength;
765
Brian Paula2ddb3d2010-02-01 18:00:12 -0700766 /* find used temp regs (we may need new temps below) */
767 _mesa_find_used_registers(newProg, PROGRAM_TEMPORARY,
768 usedTemps, MAX_PROGRAM_TEMPS);
769
Brian Paul6ca948a2008-05-14 12:53:03 -0600770 if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul0c78c762008-05-18 15:46:26 -0600771 struct gl_fragment_program *fprogA, *fprogB, *newFprog;
Brian Paul5c4bd762008-10-08 14:02:24 -0600772 GLbitfield progB_inputsRead = progB->InputsRead;
773 GLint progB_colorFile, progB_colorIndex;
774
Brian Paul0c78c762008-05-18 15:46:26 -0600775 fprogA = (struct gl_fragment_program *) progA;
776 fprogB = (struct gl_fragment_program *) progB;
777 newFprog = (struct gl_fragment_program *) newProg;
778
779 newFprog->UsesKill = fprogA->UsesKill || fprogB->UsesKill;
780
Brian Paul5c4bd762008-10-08 14:02:24 -0600781 /* We'll do a search and replace for instances
782 * of progB_colorFile/progB_colorIndex below...
783 */
784 progB_colorFile = PROGRAM_INPUT;
785 progB_colorIndex = FRAG_ATTRIB_COL0;
786
787 /*
788 * The fragment program may get color from a state var rather than
789 * a fragment input (vertex output) if it's constant.
790 * See the texenvprogram.c code.
791 * So, search the program's parameter list now to see if the program
792 * gets color from a state var instead of a conventional fragment
793 * input register.
794 */
795 for (i = 0; i < progB->Parameters->NumParameters; i++) {
796 struct gl_program_parameter *p = &progB->Parameters->Parameters[i];
797 if (p->Type == PROGRAM_STATE_VAR &&
798 p->StateIndexes[0] == STATE_INTERNAL &&
799 p->StateIndexes[1] == STATE_CURRENT_ATTRIB &&
Brian Paulf72e4b32010-10-25 09:10:32 -0600800 (int) p->StateIndexes[2] == (int) VERT_ATTRIB_COLOR0) {
Brian Paul5c4bd762008-10-08 14:02:24 -0600801 progB_inputsRead |= FRAG_BIT_COL0;
802 progB_colorFile = PROGRAM_STATE_VAR;
803 progB_colorIndex = i;
804 break;
805 }
806 }
807
Brian Paul0c78c762008-05-18 15:46:26 -0600808 /* Connect color outputs of fprogA to color inputs of fprogB, via a
809 * new temporary register.
810 */
Brian Paul8d475822009-02-28 11:49:46 -0700811 if ((progA->OutputsWritten & (1 << FRAG_RESULT_COLOR)) &&
Brian Paul5c4bd762008-10-08 14:02:24 -0600812 (progB_inputsRead & FRAG_BIT_COL0)) {
Brian Paula2ddb3d2010-02-01 18:00:12 -0700813 GLint tempReg = _mesa_find_free_register(usedTemps, MAX_PROGRAM_TEMPS,
814 firstTemp);
Brian Paule469d782008-05-19 16:03:43 -0600815 if (tempReg < 0) {
Brian Paul0c78c762008-05-18 15:46:26 -0600816 _mesa_problem(ctx, "No free temp regs found in "
817 "_mesa_combine_programs(), using 31");
818 tempReg = 31;
819 }
Brian Paula2ddb3d2010-02-01 18:00:12 -0700820 firstTemp = tempReg + 1;
821
Brian Paul0c78c762008-05-18 15:46:26 -0600822 /* replace writes to result.color[0] with tempReg */
823 replace_registers(newInst, lenA,
Brian Paul8d475822009-02-28 11:49:46 -0700824 PROGRAM_OUTPUT, FRAG_RESULT_COLOR,
Brian Paul0c78c762008-05-18 15:46:26 -0600825 PROGRAM_TEMPORARY, tempReg);
Brian Paul5c4bd762008-10-08 14:02:24 -0600826 /* replace reads from the input color with tempReg */
Brian Paul6ca948a2008-05-14 12:53:03 -0600827 replace_registers(newInst + lenA, lenB,
Brian Paul5c4bd762008-10-08 14:02:24 -0600828 progB_colorFile, progB_colorIndex, /* search for */
829 PROGRAM_TEMPORARY, tempReg /* replace with */ );
Brian Paul6ca948a2008-05-14 12:53:03 -0600830 }
831
Brian Paul5c4bd762008-10-08 14:02:24 -0600832 /* compute combined program's InputsRead */
833 inputsB = progB_inputsRead;
Brian Paul8d475822009-02-28 11:49:46 -0700834 if (progA->OutputsWritten & (1 << FRAG_RESULT_COLOR)) {
Brian Paul0c78c762008-05-18 15:46:26 -0600835 inputsB &= ~(1 << FRAG_ATTRIB_COL0);
836 }
837 newProg->InputsRead = progA->InputsRead | inputsB;
Brian Paul6ca948a2008-05-14 12:53:03 -0600838 newProg->OutputsWritten = progB->OutputsWritten;
Brian Paul0c78c762008-05-18 15:46:26 -0600839 newProg->SamplersUsed = progA->SamplersUsed | progB->SamplersUsed;
Brian Paul6ca948a2008-05-14 12:53:03 -0600840 }
841 else {
842 /* vertex program */
843 assert(0); /* XXX todo */
844 }
845
846 /*
847 * Merge parameters (uniforms, constants, etc)
848 */
849 newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters,
850 progB->Parameters);
851
852 adjust_param_indexes(newInst + lenA, lenB, numParamsA);
853
854
855 return newProg;
856}
857
858
Brian Paul6ca948a2008-05-14 12:53:03 -0600859/**
Brian Paula2ddb3d2010-02-01 18:00:12 -0700860 * Populate the 'used' array with flags indicating which registers (TEMPs,
861 * INPUTs, OUTPUTs, etc, are used by the given program.
862 * \param file type of register to scan for
863 * \param used returns true/false flags for in use / free
864 * \param usedSize size of the 'used' array
Brian Paul6ca948a2008-05-14 12:53:03 -0600865 */
Brian Paula2ddb3d2010-02-01 18:00:12 -0700866void
867_mesa_find_used_registers(const struct gl_program *prog,
868 gl_register_file file,
869 GLboolean used[], GLuint usedSize)
Brian Paul6ca948a2008-05-14 12:53:03 -0600870{
Brian Paula2ddb3d2010-02-01 18:00:12 -0700871 GLuint i, j;
Brian Paul6ca948a2008-05-14 12:53:03 -0600872
Kenneth Graunke26f8fad2010-02-18 23:51:00 -0800873 memset(used, 0, usedSize);
Brian Paul6ca948a2008-05-14 12:53:03 -0600874
875 for (i = 0; i < prog->NumInstructions; i++) {
876 const struct prog_instruction *inst = prog->Instructions + i;
877 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
878
Brian Paula2ddb3d2010-02-01 18:00:12 -0700879 if (inst->DstReg.File == file) {
Luca Barbieri029c1812010-09-05 17:57:12 +0200880 ASSERT(inst->DstReg.Index < usedSize);
881 if(inst->DstReg.Index < usedSize)
882 used[inst->DstReg.Index] = GL_TRUE;
Brian Paul5076a4f2009-12-11 09:16:25 -0700883 }
Brian Paula2ddb3d2010-02-01 18:00:12 -0700884
885 for (j = 0; j < n; j++) {
886 if (inst->SrcReg[j].File == file) {
Luca Barbieri029c1812010-09-05 17:57:12 +0200887 ASSERT(inst->SrcReg[j].Index < usedSize);
888 if(inst->SrcReg[j].Index < usedSize)
889 used[inst->SrcReg[j].Index] = GL_TRUE;
Brian Paul6ca948a2008-05-14 12:53:03 -0600890 }
891 }
892 }
Brian Paula2ddb3d2010-02-01 18:00:12 -0700893}
Brian Paul6ca948a2008-05-14 12:53:03 -0600894
Brian Paula2ddb3d2010-02-01 18:00:12 -0700895
896/**
897 * Scan the given 'used' register flag array for the first entry
898 * that's >= firstReg.
899 * \param used vector of flags indicating registers in use (as returned
900 * by _mesa_find_used_registers())
901 * \param usedSize size of the 'used' array
902 * \param firstReg first register to start searching at
903 * \return index of unused register, or -1 if none.
904 */
905GLint
906_mesa_find_free_register(const GLboolean used[],
907 GLuint usedSize, GLuint firstReg)
908{
909 GLuint i;
910
911 assert(firstReg < usedSize);
912
913 for (i = firstReg; i < usedSize; i++)
Brian Paul6ca948a2008-05-14 12:53:03 -0600914 if (!used[i])
915 return i;
Brian Paul6ca948a2008-05-14 12:53:03 -0600916
917 return -1;
918}
Brian Paulec6ad7b2009-06-17 07:42:49 -0600919
920
Brian Paul512f8402010-11-23 10:12:55 -0700921
922/**
923 * Check if the given register index is valid (doesn't exceed implementation-
924 * dependent limits).
925 * \return GL_TRUE if OK, GL_FALSE if bad index
926 */
927GLboolean
928_mesa_valid_register_index(const struct gl_context *ctx,
Brian Paulc628fd72010-11-23 10:28:43 -0700929 gl_shader_type shaderType,
Brian Paul512f8402010-11-23 10:12:55 -0700930 gl_register_file file, GLint index)
931{
932 const struct gl_program_constants *c;
933
934 switch (shaderType) {
935 case MESA_SHADER_VERTEX:
936 c = &ctx->Const.VertexProgram;
937 break;
938 case MESA_SHADER_FRAGMENT:
939 c = &ctx->Const.FragmentProgram;
940 break;
941 case MESA_SHADER_GEOMETRY:
942 c = &ctx->Const.GeometryProgram;
943 break;
944 default:
945 _mesa_problem(ctx,
946 "unexpected shader type in _mesa_valid_register_index()");
947 return GL_FALSE;
948 }
949
950 switch (file) {
951 case PROGRAM_UNDEFINED:
952 return GL_TRUE; /* XXX or maybe false? */
953
954 case PROGRAM_TEMPORARY:
955 return index >= 0 && index < c->MaxTemps;
956
957 case PROGRAM_ENV_PARAM:
958 return index >= 0 && index < c->MaxEnvParams;
959
960 case PROGRAM_LOCAL_PARAM:
961 return index >= 0 && index < c->MaxLocalParams;
962
963 case PROGRAM_NAMED_PARAM:
964 return index >= 0 && index < c->MaxParameters;
965
966 case PROGRAM_UNIFORM:
967 case PROGRAM_STATE_VAR:
968 /* aka constant buffer */
969 return index >= 0 && index < c->MaxUniformComponents / 4;
970
971 case PROGRAM_CONSTANT:
972 /* constant buffer w/ possible relative negative addressing */
973 return (index > (int) c->MaxUniformComponents / -4 &&
974 index < c->MaxUniformComponents / 4);
975
976 case PROGRAM_INPUT:
977 if (index < 0)
978 return GL_FALSE;
979
980 switch (shaderType) {
981 case MESA_SHADER_VERTEX:
982 return index < VERT_ATTRIB_GENERIC0 + c->MaxAttribs;
983 case MESA_SHADER_FRAGMENT:
984 return index < FRAG_ATTRIB_VAR0 + ctx->Const.MaxVarying;
985 case MESA_SHADER_GEOMETRY:
986 return index < GEOM_ATTRIB_VAR0 + ctx->Const.MaxVarying;
987 default:
988 return GL_FALSE;
989 }
990
991 case PROGRAM_OUTPUT:
992 if (index < 0)
993 return GL_FALSE;
994
995 switch (shaderType) {
996 case MESA_SHADER_VERTEX:
997 return index < VERT_RESULT_VAR0 + ctx->Const.MaxVarying;
998 case MESA_SHADER_FRAGMENT:
999 return index < FRAG_RESULT_DATA0 + ctx->Const.MaxDrawBuffers;
1000 case MESA_SHADER_GEOMETRY:
1001 return index < GEOM_RESULT_VAR0 + ctx->Const.MaxVarying;
1002 default:
1003 return GL_FALSE;
1004 }
1005
1006 case PROGRAM_ADDRESS:
1007 return index >= 0 && index < c->MaxAddressRegs;
1008
1009 default:
1010 _mesa_problem(ctx,
1011 "unexpected register file in _mesa_valid_register_index()");
1012 return GL_FALSE;
1013 }
1014}
1015
1016
1017
Brian Paulec6ad7b2009-06-17 07:42:49 -06001018/**
1019 * "Post-process" a GPU program. This is intended to be used for debugging.
1020 * Example actions include no-op'ing instructions or changing instruction
1021 * behaviour.
1022 */
1023void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001024_mesa_postprocess_program(struct gl_context *ctx, struct gl_program *prog)
Brian Paulec6ad7b2009-06-17 07:42:49 -06001025{
1026 static const GLfloat white[4] = { 0.5, 0.5, 0.5, 0.5 };
1027 GLuint i;
1028 GLuint whiteSwizzle;
1029 GLint whiteIndex = _mesa_add_unnamed_constant(prog->Parameters,
1030 white, 4, &whiteSwizzle);
1031
Brian Paul516d20f2009-06-17 07:43:44 -06001032 (void) whiteIndex;
1033
Brian Paulec6ad7b2009-06-17 07:42:49 -06001034 for (i = 0; i < prog->NumInstructions; i++) {
1035 struct prog_instruction *inst = prog->Instructions + i;
1036 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
1037
1038 (void) n;
1039
1040 if (_mesa_is_tex_instruction(inst->Opcode)) {
1041#if 0
1042 /* replace TEX/TXP/TXB with MOV */
1043 inst->Opcode = OPCODE_MOV;
1044 inst->DstReg.WriteMask = WRITEMASK_XYZW;
1045 inst->SrcReg[0].Swizzle = SWIZZLE_XYZW;
1046 inst->SrcReg[0].Negate = NEGATE_NONE;
1047#endif
1048
1049#if 0
1050 /* disable shadow texture mode */
1051 inst->TexShadow = 0;
1052#endif
1053 }
1054
1055 if (inst->Opcode == OPCODE_TXP) {
1056#if 0
1057 inst->Opcode = OPCODE_MOV;
1058 inst->DstReg.WriteMask = WRITEMASK_XYZW;
1059 inst->SrcReg[0].File = PROGRAM_CONSTANT;
1060 inst->SrcReg[0].Index = whiteIndex;
1061 inst->SrcReg[0].Swizzle = SWIZZLE_XYZW;
1062 inst->SrcReg[0].Negate = NEGATE_NONE;
1063#endif
1064#if 0
1065 inst->TexShadow = 0;
1066#endif
1067#if 0
1068 inst->Opcode = OPCODE_TEX;
1069 inst->TexShadow = 0;
1070#endif
1071 }
1072
1073 }
1074}