blob: a6ada8a048b859c14c86c47a69a1347a9b4ff1db [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"
Brian0560d812006-12-14 15:01:28 -070035#include "program.h"
Brianb26aae62007-10-31 12:00:38 -060036#include "prog_cache.h"
Brian0560d812006-12-14 15:01:28 -070037#include "prog_parameter.h"
38#include "prog_instruction.h"
Michal Krol2861e732004-03-29 11:09:34 +000039
40
Brian942ee022007-02-09 15:40:00 -070041/**
42 * A pointer to this dummy program is put into the hash table when
Brian Paul765f1a12004-09-14 22:28:27 +000043 * glGenPrograms is called.
44 */
Brian Paul122629f2006-07-20 16:49:57 +000045struct gl_program _mesa_DummyProgram;
Brian Paul765f1a12004-09-14 22:28:27 +000046
47
Michal Krol2861e732004-03-29 11:09:34 +000048/**
Brian Paul21841f02004-08-14 14:28:11 +000049 * Init context's vertex/fragment program state
Michal Krol2861e732004-03-29 11:09:34 +000050 */
51void
52_mesa_init_program(GLcontext *ctx)
53{
54 GLuint i;
55
Brian Paul5b2f8dc2009-02-18 11:47:40 -070056 /*
57 * If this assertion fails, we need to increase the field
58 * size for register indexes.
59 */
60 ASSERT(ctx->Const.VertexProgram.MaxUniformComponents / 4
61 <= (1 << INST_INDEX_BITS));
62 ASSERT(ctx->Const.FragmentProgram.MaxUniformComponents / 4
63 <= (1 << INST_INDEX_BITS));
64
Brian Pauldd528f02009-08-26 10:57:18 -060065 /* If this fails, increase prog_instruction::TexSrcUnit size */
66 ASSERT(MAX_TEXTURE_UNITS < (1 << 5));
67
68 /* If this fails, increase prog_instruction::TexSrcTarget size */
69 ASSERT(NUM_TEXTURE_TARGETS < (1 << 3));
70
Michal Krol2861e732004-03-29 11:09:34 +000071 ctx->Program.ErrorPos = -1;
72 ctx->Program.ErrorString = _mesa_strdup("");
73
74#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
75 ctx->VertexProgram.Enabled = GL_FALSE;
Brian Paulaf3d9db2008-08-12 10:00:36 -060076#if FEATURE_es2_glsl
Kristian Høgsbergf67b0202010-05-24 10:01:38 -040077 ctx->VertexProgram.PointSizeEnabled =
78 (ctx->API == API_OPENGLES2) ? GL_TRUE : GL_FALSE;
Brian Paulaf3d9db2008-08-12 10:00:36 -060079#else
Michal Krol2861e732004-03-29 11:09:34 +000080 ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
Brian Paulaf3d9db2008-08-12 10:00:36 -060081#endif
Michal Krol2861e732004-03-29 11:09:34 +000082 ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
Briandf43fb62008-05-06 23:08:51 -060083 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
84 ctx->Shared->DefaultVertexProgram);
Michal Krol2861e732004-03-29 11:09:34 +000085 assert(ctx->VertexProgram.Current);
Michal Krol2861e732004-03-29 11:09:34 +000086 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
87 ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
88 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
89 }
Brianb26aae62007-10-31 12:00:38 -060090 ctx->VertexProgram.Cache = _mesa_new_program_cache();
Michal Krol2861e732004-03-29 11:09:34 +000091#endif
92
93#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
94 ctx->FragmentProgram.Enabled = GL_FALSE;
Briandf43fb62008-05-06 23:08:51 -060095 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
96 ctx->Shared->DefaultFragmentProgram);
Michal Krol2861e732004-03-29 11:09:34 +000097 assert(ctx->FragmentProgram.Current);
Brianb26aae62007-10-31 12:00:38 -060098 ctx->FragmentProgram.Cache = _mesa_new_program_cache();
Michal Krol2861e732004-03-29 11:09:34 +000099#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +0000100
Brianb26aae62007-10-31 12:00:38 -0600101
Brian Paul63d68302005-11-19 16:43:04 +0000102 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +0000103#if FEATURE_ATI_fragment_shader
104 ctx->ATIFragmentShader.Enabled = GL_FALSE;
Brian Paulb7eea9a2008-07-29 17:19:25 -0600105 ctx->ATIFragmentShader.Current = ctx->Shared->DefaultFragmentShader;
Dave Airlie7f752fe2004-12-19 03:06:59 +0000106 assert(ctx->ATIFragmentShader.Current);
Brian Paul63d68302005-11-19 16:43:04 +0000107 ctx->ATIFragmentShader.Current->RefCount++;
Dave Airlie7f752fe2004-12-19 03:06:59 +0000108#endif
Michal Krol2861e732004-03-29 11:09:34 +0000109}
110
111
112/**
Brian Paul21841f02004-08-14 14:28:11 +0000113 * Free a context's vertex/fragment program state
114 */
115void
116_mesa_free_program_data(GLcontext *ctx)
117{
Roland Scheidegger93da6732006-03-01 23:11:14 +0000118#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Briandf43fb62008-05-06 23:08:51 -0600119 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL);
Brian1631a952007-12-26 06:57:24 -0700120 _mesa_delete_program_cache(ctx, ctx->VertexProgram.Cache);
Brian Paul21841f02004-08-14 14:28:11 +0000121#endif
Roland Scheidegger93da6732006-03-01 23:11:14 +0000122#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Briandf43fb62008-05-06 23:08:51 -0600123 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL);
Brian1631a952007-12-26 06:57:24 -0700124 _mesa_delete_program_cache(ctx, ctx->FragmentProgram.Cache);
Brian Paul21841f02004-08-14 14:28:11 +0000125#endif
Brian Paul63d68302005-11-19 16:43:04 +0000126 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +0000127#if FEATURE_ATI_fragment_shader
128 if (ctx->ATIFragmentShader.Current) {
Brian Paul63d68302005-11-19 16:43:04 +0000129 ctx->ATIFragmentShader.Current->RefCount--;
130 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500131 free(ctx->ATIFragmentShader.Current);
Brian Paul63d68302005-11-19 16:43:04 +0000132 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000133 }
134#endif
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500135 free((void *) ctx->Program.ErrorString);
Brian Paul21841f02004-08-14 14:28:11 +0000136}
137
138
Brian4b654d42007-08-23 08:53:43 +0100139/**
140 * Update the default program objects in the given context to reference those
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200141 * specified in the shared state and release those referencing the old
Brian4b654d42007-08-23 08:53:43 +0100142 * shared state.
143 */
144void
145_mesa_update_default_objects_program(GLcontext *ctx)
146{
147#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Brian Paul57e222d2008-05-14 12:10:45 -0600148 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
149 (struct gl_vertex_program *)
150 ctx->Shared->DefaultVertexProgram);
Brian4b654d42007-08-23 08:53:43 +0100151 assert(ctx->VertexProgram.Current);
Brian4b654d42007-08-23 08:53:43 +0100152#endif
153
154#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Brian Paul57e222d2008-05-14 12:10:45 -0600155 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
156 (struct gl_fragment_program *)
157 ctx->Shared->DefaultFragmentProgram);
Brian4b654d42007-08-23 08:53:43 +0100158 assert(ctx->FragmentProgram.Current);
Brian4b654d42007-08-23 08:53:43 +0100159#endif
160
161 /* XXX probably move this stuff */
162#if FEATURE_ATI_fragment_shader
163 if (ctx->ATIFragmentShader.Current) {
164 ctx->ATIFragmentShader.Current->RefCount--;
165 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500166 free(ctx->ATIFragmentShader.Current);
Brian4b654d42007-08-23 08:53:43 +0100167 }
168 }
169 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
170 assert(ctx->ATIFragmentShader.Current);
171 ctx->ATIFragmentShader.Current->RefCount++;
172#endif
173}
Brian Paul21841f02004-08-14 14:28:11 +0000174
175
176/**
Michal Krol2861e732004-03-29 11:09:34 +0000177 * Set the vertex/fragment program error state (position and error string).
178 * This is generally called from within the parsers.
179 */
180void
181_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
182{
183 ctx->Program.ErrorPos = pos;
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500184 free((void *) ctx->Program.ErrorString);
Michal Krol2861e732004-03-29 11:09:34 +0000185 if (!string)
186 string = "";
187 ctx->Program.ErrorString = _mesa_strdup(string);
188}
189
190
191/**
192 * Find the line number and column for 'pos' within 'string'.
193 * Return a copy of the line which contains 'pos'. Free the line with
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500194 * free().
Michal Krol2861e732004-03-29 11:09:34 +0000195 * \param string the program string
196 * \param pos the position within the string
197 * \param line returns the line number corresponding to 'pos'.
198 * \param col returns the column number corresponding to 'pos'.
199 * \return copy of the line containing 'pos'.
200 */
201const GLubyte *
202_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
203 GLint *line, GLint *col)
204{
205 const GLubyte *lineStart = string;
206 const GLubyte *p = string;
207 GLubyte *s;
208 int len;
209
210 *line = 1;
211
212 while (p != pos) {
213 if (*p == (GLubyte) '\n') {
214 (*line)++;
215 lineStart = p + 1;
216 }
217 p++;
218 }
219
220 *col = (pos - lineStart) + 1;
221
222 /* return copy of this line */
223 while (*p != 0 && *p != '\n')
224 p++;
225 len = p - lineStart;
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500226 s = (GLubyte *) malloc(len + 1);
Kenneth Graunkec7ac48622010-02-18 23:50:59 -0800227 memcpy(s, lineStart, len);
Michal Krol2861e732004-03-29 11:09:34 +0000228 s[len] = 0;
229
230 return s;
231}
232
233
Brian Paul765f1a12004-09-14 22:28:27 +0000234/**
235 * Initialize a new vertex/fragment program object.
236 */
Brian Paul122629f2006-07-20 16:49:57 +0000237static struct gl_program *
238_mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000239 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000240{
Brian Paula6c423d2004-08-25 15:59:48 +0000241 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000242 if (prog) {
Brian Paul0c78c762008-05-18 15:46:26 -0600243 GLuint i;
Brian Paul6bf1ea82010-02-19 08:32:36 -0700244 memset(prog, 0, sizeof(*prog));
Michal Krol2861e732004-03-29 11:09:34 +0000245 prog->Id = id;
246 prog->Target = target;
247 prog->Resident = GL_TRUE;
248 prog->RefCount = 1;
Brian Paul308b85f2006-11-23 15:58:30 +0000249 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
Brian Paul0c78c762008-05-18 15:46:26 -0600250
251 /* default mapping from samplers to texture units */
252 for (i = 0; i < MAX_SAMPLERS; i++)
253 prog->SamplerUnits[i] = i;
Michal Krol2861e732004-03-29 11:09:34 +0000254 }
255
256 return prog;
257}
258
Brian Paul765f1a12004-09-14 22:28:27 +0000259
260/**
261 * Initialize a new fragment program object.
262 */
Brian Paul122629f2006-07-20 16:49:57 +0000263struct gl_program *
264_mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000265 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000266{
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200267 if (prog)
Michal Krol2861e732004-03-29 11:09:34 +0000268 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
269 else
270 return NULL;
271}
272
Brian Paul765f1a12004-09-14 22:28:27 +0000273
274/**
275 * Initialize a new vertex program object.
276 */
Brian Paul122629f2006-07-20 16:49:57 +0000277struct gl_program *
278_mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000279 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000280{
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200281 if (prog)
Michal Krol2861e732004-03-29 11:09:34 +0000282 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
283 else
284 return NULL;
285}
286
287
288/**
289 * Allocate and initialize a new fragment/vertex program object but
290 * don't put it into the program hash table. Called via
291 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
292 * device driver function to implement OO deriviation with additional
293 * types not understood by this function.
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200294 *
Michal Krol2861e732004-03-29 11:09:34 +0000295 * \param ctx context
296 * \param id program id/number
297 * \param target program target/type
298 * \return pointer to new program object
299 */
Brian Paul122629f2006-07-20 16:49:57 +0000300struct gl_program *
Michal Krol2861e732004-03-29 11:09:34 +0000301_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
302{
Brian Paula3e86d42008-05-16 09:56:11 -0600303 struct gl_program *prog;
Michal Krol2861e732004-03-29 11:09:34 +0000304 switch (target) {
305 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
Brian Paulc5af2ed2009-04-18 10:08:54 -0600306 case GL_VERTEX_STATE_PROGRAM_NV:
Brian Paula3e86d42008-05-16 09:56:11 -0600307 prog = _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
Brian Paul122629f2006-07-20 16:49:57 +0000308 target, id );
Brian Paula3e86d42008-05-16 09:56:11 -0600309 break;
Michal Krol2861e732004-03-29 11:09:34 +0000310 case GL_FRAGMENT_PROGRAM_NV:
311 case GL_FRAGMENT_PROGRAM_ARB:
Brian Paula3e86d42008-05-16 09:56:11 -0600312 prog =_mesa_init_fragment_program(ctx,
Brian Paul122629f2006-07-20 16:49:57 +0000313 CALLOC_STRUCT(gl_fragment_program),
314 target, id );
Brian Paula3e86d42008-05-16 09:56:11 -0600315 break;
Michal Krol2861e732004-03-29 11:09:34 +0000316 default:
317 _mesa_problem(ctx, "bad target in _mesa_new_program");
Brian Paula3e86d42008-05-16 09:56:11 -0600318 prog = NULL;
Michal Krol2861e732004-03-29 11:09:34 +0000319 }
Brian Paula3e86d42008-05-16 09:56:11 -0600320 return prog;
Michal Krol2861e732004-03-29 11:09:34 +0000321}
322
323
324/**
325 * Delete a program and remove it from the hash table, ignoring the
326 * reference count.
327 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
328 * by a device driver function.
329 */
330void
Brian Paul122629f2006-07-20 16:49:57 +0000331_mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
Michal Krol2861e732004-03-29 11:09:34 +0000332{
Brian Paula6c423d2004-08-25 15:59:48 +0000333 (void) ctx;
Brian Paul57e222d2008-05-14 12:10:45 -0600334 ASSERT(prog);
335 ASSERT(prog->RefCount==0);
Michal Krol2861e732004-03-29 11:09:34 +0000336
Brian Paul308b85f2006-11-23 15:58:30 +0000337 if (prog == &_mesa_DummyProgram)
338 return;
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200339
Michal Krol2861e732004-03-29 11:09:34 +0000340 if (prog->String)
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500341 free(prog->String);
Brian Paulde997602005-11-12 17:53:14 +0000342
Brian Paul19ad9cf2008-05-14 12:39:41 -0600343 _mesa_free_instructions(prog->Instructions, prog->NumInstructions);
Brian Paulde997602005-11-12 17:53:14 +0000344
Brian Paul65a51c02006-05-24 03:30:31 +0000345 if (prog->Parameters) {
Brian Paulde997602005-11-12 17:53:14 +0000346 _mesa_free_parameter_list(prog->Parameters);
Brian Paul65a51c02006-05-24 03:30:31 +0000347 }
Brianfe1d01c2006-12-13 14:54:47 -0700348 if (prog->Varying) {
349 _mesa_free_parameter_list(prog->Varying);
350 }
Brian3493e862007-03-24 16:18:13 -0600351 if (prog->Attributes) {
352 _mesa_free_parameter_list(prog->Attributes);
353 }
Brianfe1d01c2006-12-13 14:54:47 -0700354
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500355 free(prog);
Michal Krol2861e732004-03-29 11:09:34 +0000356}
357
358
Brian Paul4d12a052006-08-23 23:10:14 +0000359/**
360 * Return the gl_program object for a given ID.
361 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
362 * casts elsewhere.
363 */
364struct gl_program *
365_mesa_lookup_program(GLcontext *ctx, GLuint id)
366{
367 if (id)
368 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
369 else
370 return NULL;
371}
372
Michal Krol2861e732004-03-29 11:09:34 +0000373
Brian Paul3b9b8de2006-08-24 21:57:36 +0000374/**
Briandf43fb62008-05-06 23:08:51 -0600375 * Reference counting for vertex/fragment programs
376 */
377void
378_mesa_reference_program(GLcontext *ctx,
379 struct gl_program **ptr,
380 struct gl_program *prog)
381{
382 assert(ptr);
383 if (*ptr && prog) {
384 /* sanity check */
Brian Paul4bc39c52008-09-26 07:40:45 -0600385 if ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB)
386 ASSERT(prog->Target == GL_VERTEX_PROGRAM_ARB);
387 else if ((*ptr)->Target == GL_FRAGMENT_PROGRAM_ARB)
388 ASSERT(prog->Target == GL_FRAGMENT_PROGRAM_ARB ||
389 prog->Target == GL_FRAGMENT_PROGRAM_NV);
Briandf43fb62008-05-06 23:08:51 -0600390 }
391 if (*ptr == prog) {
392 return; /* no change */
393 }
394 if (*ptr) {
395 GLboolean deleteFlag;
396
397 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
Brian Paulb4e75d62008-05-08 10:59:31 -0600398#if 0
Brian Paula3e86d42008-05-16 09:56:11 -0600399 printf("Program %p ID=%u Target=%s Refcount-- to %d\n",
400 *ptr, (*ptr)->Id,
401 ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB ? "VP" : "FP"),
402 (*ptr)->RefCount - 1);
Briandf43fb62008-05-06 23:08:51 -0600403#endif
404 ASSERT((*ptr)->RefCount > 0);
405 (*ptr)->RefCount--;
406
407 deleteFlag = ((*ptr)->RefCount == 0);
408 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200409
Briandf43fb62008-05-06 23:08:51 -0600410 if (deleteFlag) {
411 ASSERT(ctx);
412 ctx->Driver.DeleteProgram(ctx, *ptr);
413 }
414
415 *ptr = NULL;
416 }
417
418 assert(!*ptr);
419 if (prog) {
420 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
421 prog->RefCount++;
Brian Paulb4e75d62008-05-08 10:59:31 -0600422#if 0
Brian Paula3e86d42008-05-16 09:56:11 -0600423 printf("Program %p ID=%u Target=%s Refcount++ to %d\n",
424 prog, prog->Id,
425 (prog->Target == GL_VERTEX_PROGRAM_ARB ? "VP" : "FP"),
426 prog->RefCount);
Briandf43fb62008-05-06 23:08:51 -0600427#endif
428 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
429 }
430
431 *ptr = prog;
432}
433
434
435/**
Brianb2a3a852006-12-14 13:56:58 -0700436 * Return a copy of a program.
437 * XXX Problem here if the program object is actually OO-derivation
438 * made by a device driver.
439 */
440struct gl_program *
441_mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
442{
443 struct gl_program *clone;
444
Brian5b6858c2007-07-24 09:56:44 -0600445 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
Brianb2a3a852006-12-14 13:56:58 -0700446 if (!clone)
447 return NULL;
448
449 assert(clone->Target == prog->Target);
Briandf43fb62008-05-06 23:08:51 -0600450 assert(clone->RefCount == 1);
451
Brianb2a3a852006-12-14 13:56:58 -0700452 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
Brianb2a3a852006-12-14 13:56:58 -0700453 clone->Format = prog->Format;
454 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
455 if (!clone->Instructions) {
Brian Paul57e222d2008-05-14 12:10:45 -0600456 _mesa_reference_program(ctx, &clone, NULL);
Brianb2a3a852006-12-14 13:56:58 -0700457 return NULL;
458 }
Brian12229f12007-03-22 09:11:26 -0600459 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
460 prog->NumInstructions);
Brianb2a3a852006-12-14 13:56:58 -0700461 clone->InputsRead = prog->InputsRead;
462 clone->OutputsWritten = prog->OutputsWritten;
Brian Paul0c78c762008-05-18 15:46:26 -0600463 clone->SamplersUsed = prog->SamplersUsed;
Nicolai Haehnle82635aa2008-07-05 18:03:44 +0200464 clone->ShadowSamplers = prog->ShadowSamplers;
Brianc9db2232007-01-04 17:22:19 -0700465 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
466
Brian2e76f0a2006-12-19 09:52:07 -0700467 if (prog->Parameters)
468 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
Brianb2a3a852006-12-14 13:56:58 -0700469 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian2e76f0a2006-12-19 09:52:07 -0700470 if (prog->Varying)
471 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
Brian3209c3e2007-01-09 17:49:24 -0700472 if (prog->Attributes)
473 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
Brianb2a3a852006-12-14 13:56:58 -0700474 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
475 clone->NumInstructions = prog->NumInstructions;
476 clone->NumTemporaries = prog->NumTemporaries;
477 clone->NumParameters = prog->NumParameters;
478 clone->NumAttributes = prog->NumAttributes;
479 clone->NumAddressRegs = prog->NumAddressRegs;
480 clone->NumNativeInstructions = prog->NumNativeInstructions;
481 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
482 clone->NumNativeParameters = prog->NumNativeParameters;
483 clone->NumNativeAttributes = prog->NumNativeAttributes;
484 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -0700485 clone->NumAluInstructions = prog->NumAluInstructions;
486 clone->NumTexInstructions = prog->NumTexInstructions;
487 clone->NumTexIndirections = prog->NumTexIndirections;
488 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
489 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
490 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
Brianb2a3a852006-12-14 13:56:58 -0700491
492 switch (prog->Target) {
493 case GL_VERTEX_PROGRAM_ARB:
494 {
495 const struct gl_vertex_program *vp
496 = (const struct gl_vertex_program *) prog;
497 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
498 vpc->IsPositionInvariant = vp->IsPositionInvariant;
Nicolai Hähnle736e1ae2009-09-09 19:56:57 +0200499 vpc->IsNVProgram = vp->IsNVProgram;
Brianb2a3a852006-12-14 13:56:58 -0700500 }
501 break;
502 case GL_FRAGMENT_PROGRAM_ARB:
503 {
504 const struct gl_fragment_program *fp
505 = (const struct gl_fragment_program *) prog;
506 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
Brianb2a3a852006-12-14 13:56:58 -0700507 fpc->FogOption = fp->FogOption;
508 fpc->UsesKill = fp->UsesKill;
Brian Paulb947b1d2010-02-13 13:51:38 -0700509 fpc->OriginUpperLeft = fp->OriginUpperLeft;
510 fpc->PixelCenterInteger = fp->PixelCenterInteger;
Brianb2a3a852006-12-14 13:56:58 -0700511 }
512 break;
513 default:
514 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
515 }
516
517 return clone;
518}
519
520
Brian Paul19ad9cf2008-05-14 12:39:41 -0600521/**
522 * Insert 'count' NOP instructions at 'start' in the given program.
523 * Adjust branch targets accordingly.
524 */
525GLboolean
526_mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
527{
528 const GLuint origLen = prog->NumInstructions;
529 const GLuint newLen = origLen + count;
530 struct prog_instruction *newInst;
531 GLuint i;
532
533 /* adjust branches */
534 for (i = 0; i < prog->NumInstructions; i++) {
535 struct prog_instruction *inst = prog->Instructions + i;
536 if (inst->BranchTarget > 0) {
José Fonseca457d7212008-06-24 11:34:46 +0900537 if ((GLuint)inst->BranchTarget >= start) {
Brian Paul19ad9cf2008-05-14 12:39:41 -0600538 inst->BranchTarget += count;
539 }
540 }
541 }
542
543 /* Alloc storage for new instructions */
544 newInst = _mesa_alloc_instructions(newLen);
545 if (!newInst) {
546 return GL_FALSE;
547 }
548
549 /* Copy 'start' instructions into new instruction buffer */
550 _mesa_copy_instructions(newInst, prog->Instructions, start);
551
552 /* init the new instructions */
553 _mesa_init_instructions(newInst + start, count);
554
555 /* Copy the remaining/tail instructions to new inst buffer */
556 _mesa_copy_instructions(newInst + start + count,
557 prog->Instructions + start,
558 origLen - start);
559
560 /* free old instructions */
561 _mesa_free_instructions(prog->Instructions, origLen);
562
563 /* install new instructions */
564 prog->Instructions = newInst;
565 prog->NumInstructions = newLen;
566
567 return GL_TRUE;
568}
569
Brianb2a3a852006-12-14 13:56:58 -0700570/**
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200571 * Delete 'count' instructions at 'start' in the given program.
572 * Adjust branch targets accordingly.
573 */
574GLboolean
575_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count)
576{
577 const GLuint origLen = prog->NumInstructions;
578 const GLuint newLen = origLen - count;
579 struct prog_instruction *newInst;
580 GLuint i;
581
582 /* adjust branches */
583 for (i = 0; i < prog->NumInstructions; i++) {
584 struct prog_instruction *inst = prog->Instructions + i;
585 if (inst->BranchTarget > 0) {
Brian Paul20fbb242010-01-27 17:03:04 -0700586 if (inst->BranchTarget > (GLint) start) {
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200587 inst->BranchTarget -= count;
588 }
589 }
590 }
591
592 /* Alloc storage for new instructions */
593 newInst = _mesa_alloc_instructions(newLen);
594 if (!newInst) {
595 return GL_FALSE;
596 }
597
598 /* Copy 'start' instructions into new instruction buffer */
599 _mesa_copy_instructions(newInst, prog->Instructions, start);
600
601 /* Copy the remaining/tail instructions to new inst buffer */
602 _mesa_copy_instructions(newInst + start,
603 prog->Instructions + start + count,
604 newLen - start);
605
606 /* free old instructions */
607 _mesa_free_instructions(prog->Instructions, origLen);
608
609 /* install new instructions */
610 prog->Instructions = newInst;
611 prog->NumInstructions = newLen;
612
613 return GL_TRUE;
614}
615
616
617/**
Brian Paul6ca948a2008-05-14 12:53:03 -0600618 * Search instructions for registers that match (oldFile, oldIndex),
619 * replacing them with (newFile, newIndex).
620 */
621static void
622replace_registers(struct prog_instruction *inst, GLuint numInst,
623 GLuint oldFile, GLuint oldIndex,
624 GLuint newFile, GLuint newIndex)
625{
626 GLuint i, j;
627 for (i = 0; i < numInst; i++) {
Brian Paul0c78c762008-05-18 15:46:26 -0600628 /* src regs */
Brian Paulb22a00b2010-04-09 10:03:31 -0600629 for (j = 0; j < _mesa_num_inst_src_regs(inst[i].Opcode); j++) {
Brian Paul6ca948a2008-05-14 12:53:03 -0600630 if (inst[i].SrcReg[j].File == oldFile &&
631 inst[i].SrcReg[j].Index == oldIndex) {
632 inst[i].SrcReg[j].File = newFile;
633 inst[i].SrcReg[j].Index = newIndex;
634 }
635 }
Brian Paul0c78c762008-05-18 15:46:26 -0600636 /* dst reg */
637 if (inst[i].DstReg.File == oldFile && inst[i].DstReg.Index == oldIndex) {
638 inst[i].DstReg.File = newFile;
639 inst[i].DstReg.Index = newIndex;
640 }
Brian Paul6ca948a2008-05-14 12:53:03 -0600641 }
642}
643
644
645/**
646 * Search instructions for references to program parameters. When found,
647 * increment the parameter index by 'offset'.
648 * Used when combining programs.
649 */
650static void
651adjust_param_indexes(struct prog_instruction *inst, GLuint numInst,
652 GLuint offset)
653{
654 GLuint i, j;
655 for (i = 0; i < numInst; i++) {
Brian Paulb22a00b2010-04-09 10:03:31 -0600656 for (j = 0; j < _mesa_num_inst_src_regs(inst[i].Opcode); j++) {
Brian Paul6ca948a2008-05-14 12:53:03 -0600657 GLuint f = inst[i].SrcReg[j].File;
658 if (f == PROGRAM_CONSTANT ||
659 f == PROGRAM_UNIFORM ||
660 f == PROGRAM_STATE_VAR) {
661 inst[i].SrcReg[j].Index += offset;
662 }
663 }
664 }
665}
666
667
668/**
669 * Combine two programs into one. Fix instructions so the outputs of
670 * the first program go to the inputs of the second program.
671 */
672struct gl_program *
673_mesa_combine_programs(GLcontext *ctx,
Brian Paul0c78c762008-05-18 15:46:26 -0600674 const struct gl_program *progA,
675 const struct gl_program *progB)
Brian Paul6ca948a2008-05-14 12:53:03 -0600676{
677 struct prog_instruction *newInst;
678 struct gl_program *newProg;
679 const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */
680 const GLuint lenB = progB->NumInstructions;
681 const GLuint numParamsA = _mesa_num_parameters(progA->Parameters);
682 const GLuint newLength = lenA + lenB;
Brian Paula2ddb3d2010-02-01 18:00:12 -0700683 GLboolean usedTemps[MAX_PROGRAM_TEMPS];
684 GLuint firstTemp = 0;
Brian Paul0c78c762008-05-18 15:46:26 -0600685 GLbitfield inputsB;
Brian Paul6ca948a2008-05-14 12:53:03 -0600686 GLuint i;
687
688 ASSERT(progA->Target == progB->Target);
689
690 newInst = _mesa_alloc_instructions(newLength);
691 if (!newInst)
692 return GL_FALSE;
693
694 _mesa_copy_instructions(newInst, progA->Instructions, lenA);
695 _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB);
696
697 /* adjust branch / instruction addresses for B's instructions */
698 for (i = 0; i < lenB; i++) {
699 newInst[lenA + i].BranchTarget += lenA;
700 }
701
702 newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0);
703 newProg->Instructions = newInst;
704 newProg->NumInstructions = newLength;
705
Brian Paula2ddb3d2010-02-01 18:00:12 -0700706 /* find used temp regs (we may need new temps below) */
707 _mesa_find_used_registers(newProg, PROGRAM_TEMPORARY,
708 usedTemps, MAX_PROGRAM_TEMPS);
709
Brian Paul6ca948a2008-05-14 12:53:03 -0600710 if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul0c78c762008-05-18 15:46:26 -0600711 struct gl_fragment_program *fprogA, *fprogB, *newFprog;
Brian Paul5c4bd762008-10-08 14:02:24 -0600712 GLbitfield progB_inputsRead = progB->InputsRead;
713 GLint progB_colorFile, progB_colorIndex;
714
Brian Paul0c78c762008-05-18 15:46:26 -0600715 fprogA = (struct gl_fragment_program *) progA;
716 fprogB = (struct gl_fragment_program *) progB;
717 newFprog = (struct gl_fragment_program *) newProg;
718
719 newFprog->UsesKill = fprogA->UsesKill || fprogB->UsesKill;
720
Brian Paul5c4bd762008-10-08 14:02:24 -0600721 /* We'll do a search and replace for instances
722 * of progB_colorFile/progB_colorIndex below...
723 */
724 progB_colorFile = PROGRAM_INPUT;
725 progB_colorIndex = FRAG_ATTRIB_COL0;
726
727 /*
728 * The fragment program may get color from a state var rather than
729 * a fragment input (vertex output) if it's constant.
730 * See the texenvprogram.c code.
731 * So, search the program's parameter list now to see if the program
732 * gets color from a state var instead of a conventional fragment
733 * input register.
734 */
735 for (i = 0; i < progB->Parameters->NumParameters; i++) {
736 struct gl_program_parameter *p = &progB->Parameters->Parameters[i];
737 if (p->Type == PROGRAM_STATE_VAR &&
738 p->StateIndexes[0] == STATE_INTERNAL &&
739 p->StateIndexes[1] == STATE_CURRENT_ATTRIB &&
740 p->StateIndexes[2] == VERT_ATTRIB_COLOR0) {
741 progB_inputsRead |= FRAG_BIT_COL0;
742 progB_colorFile = PROGRAM_STATE_VAR;
743 progB_colorIndex = i;
744 break;
745 }
746 }
747
Brian Paul0c78c762008-05-18 15:46:26 -0600748 /* Connect color outputs of fprogA to color inputs of fprogB, via a
749 * new temporary register.
750 */
Brian Paul8d475822009-02-28 11:49:46 -0700751 if ((progA->OutputsWritten & (1 << FRAG_RESULT_COLOR)) &&
Brian Paul5c4bd762008-10-08 14:02:24 -0600752 (progB_inputsRead & FRAG_BIT_COL0)) {
Brian Paula2ddb3d2010-02-01 18:00:12 -0700753 GLint tempReg = _mesa_find_free_register(usedTemps, MAX_PROGRAM_TEMPS,
754 firstTemp);
Brian Paule469d782008-05-19 16:03:43 -0600755 if (tempReg < 0) {
Brian Paul0c78c762008-05-18 15:46:26 -0600756 _mesa_problem(ctx, "No free temp regs found in "
757 "_mesa_combine_programs(), using 31");
758 tempReg = 31;
759 }
Brian Paula2ddb3d2010-02-01 18:00:12 -0700760 firstTemp = tempReg + 1;
761
Brian Paul0c78c762008-05-18 15:46:26 -0600762 /* replace writes to result.color[0] with tempReg */
763 replace_registers(newInst, lenA,
Brian Paul8d475822009-02-28 11:49:46 -0700764 PROGRAM_OUTPUT, FRAG_RESULT_COLOR,
Brian Paul0c78c762008-05-18 15:46:26 -0600765 PROGRAM_TEMPORARY, tempReg);
Brian Paul5c4bd762008-10-08 14:02:24 -0600766 /* replace reads from the input color with tempReg */
Brian Paul6ca948a2008-05-14 12:53:03 -0600767 replace_registers(newInst + lenA, lenB,
Brian Paul5c4bd762008-10-08 14:02:24 -0600768 progB_colorFile, progB_colorIndex, /* search for */
769 PROGRAM_TEMPORARY, tempReg /* replace with */ );
Brian Paul6ca948a2008-05-14 12:53:03 -0600770 }
771
Brian Paul5c4bd762008-10-08 14:02:24 -0600772 /* compute combined program's InputsRead */
773 inputsB = progB_inputsRead;
Brian Paul8d475822009-02-28 11:49:46 -0700774 if (progA->OutputsWritten & (1 << FRAG_RESULT_COLOR)) {
Brian Paul0c78c762008-05-18 15:46:26 -0600775 inputsB &= ~(1 << FRAG_ATTRIB_COL0);
776 }
777 newProg->InputsRead = progA->InputsRead | inputsB;
Brian Paul6ca948a2008-05-14 12:53:03 -0600778 newProg->OutputsWritten = progB->OutputsWritten;
Brian Paul0c78c762008-05-18 15:46:26 -0600779 newProg->SamplersUsed = progA->SamplersUsed | progB->SamplersUsed;
Brian Paul6ca948a2008-05-14 12:53:03 -0600780 }
781 else {
782 /* vertex program */
783 assert(0); /* XXX todo */
784 }
785
786 /*
787 * Merge parameters (uniforms, constants, etc)
788 */
789 newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters,
790 progB->Parameters);
791
792 adjust_param_indexes(newInst + lenA, lenB, numParamsA);
793
794
795 return newProg;
796}
797
798
Brian Paul6ca948a2008-05-14 12:53:03 -0600799/**
Brian Paula2ddb3d2010-02-01 18:00:12 -0700800 * Populate the 'used' array with flags indicating which registers (TEMPs,
801 * INPUTs, OUTPUTs, etc, are used by the given program.
802 * \param file type of register to scan for
803 * \param used returns true/false flags for in use / free
804 * \param usedSize size of the 'used' array
Brian Paul6ca948a2008-05-14 12:53:03 -0600805 */
Brian Paula2ddb3d2010-02-01 18:00:12 -0700806void
807_mesa_find_used_registers(const struct gl_program *prog,
808 gl_register_file file,
809 GLboolean used[], GLuint usedSize)
Brian Paul6ca948a2008-05-14 12:53:03 -0600810{
Brian Paula2ddb3d2010-02-01 18:00:12 -0700811 GLuint i, j;
Brian Paul6ca948a2008-05-14 12:53:03 -0600812
Kenneth Graunke26f8fad2010-02-18 23:51:00 -0800813 memset(used, 0, usedSize);
Brian Paul6ca948a2008-05-14 12:53:03 -0600814
815 for (i = 0; i < prog->NumInstructions; i++) {
816 const struct prog_instruction *inst = prog->Instructions + i;
817 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
818
Brian Paula2ddb3d2010-02-01 18:00:12 -0700819 if (inst->DstReg.File == file) {
Brian Paul5076a4f2009-12-11 09:16:25 -0700820 used[inst->DstReg.Index] = GL_TRUE;
821 }
Brian Paula2ddb3d2010-02-01 18:00:12 -0700822
823 for (j = 0; j < n; j++) {
824 if (inst->SrcReg[j].File == file) {
825 used[inst->SrcReg[j].Index] = GL_TRUE;
Brian Paul6ca948a2008-05-14 12:53:03 -0600826 }
827 }
828 }
Brian Paula2ddb3d2010-02-01 18:00:12 -0700829}
Brian Paul6ca948a2008-05-14 12:53:03 -0600830
Brian Paula2ddb3d2010-02-01 18:00:12 -0700831
832/**
833 * Scan the given 'used' register flag array for the first entry
834 * that's >= firstReg.
835 * \param used vector of flags indicating registers in use (as returned
836 * by _mesa_find_used_registers())
837 * \param usedSize size of the 'used' array
838 * \param firstReg first register to start searching at
839 * \return index of unused register, or -1 if none.
840 */
841GLint
842_mesa_find_free_register(const GLboolean used[],
843 GLuint usedSize, GLuint firstReg)
844{
845 GLuint i;
846
847 assert(firstReg < usedSize);
848
849 for (i = firstReg; i < usedSize; i++)
Brian Paul6ca948a2008-05-14 12:53:03 -0600850 if (!used[i])
851 return i;
Brian Paul6ca948a2008-05-14 12:53:03 -0600852
853 return -1;
854}
Brian Paulec6ad7b2009-06-17 07:42:49 -0600855
856
Brian Paulec6ad7b2009-06-17 07:42:49 -0600857/**
858 * "Post-process" a GPU program. This is intended to be used for debugging.
859 * Example actions include no-op'ing instructions or changing instruction
860 * behaviour.
861 */
862void
863_mesa_postprocess_program(GLcontext *ctx, struct gl_program *prog)
864{
865 static const GLfloat white[4] = { 0.5, 0.5, 0.5, 0.5 };
866 GLuint i;
867 GLuint whiteSwizzle;
868 GLint whiteIndex = _mesa_add_unnamed_constant(prog->Parameters,
869 white, 4, &whiteSwizzle);
870
Brian Paul516d20f2009-06-17 07:43:44 -0600871 (void) whiteIndex;
872
Brian Paulec6ad7b2009-06-17 07:42:49 -0600873 for (i = 0; i < prog->NumInstructions; i++) {
874 struct prog_instruction *inst = prog->Instructions + i;
875 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
876
877 (void) n;
878
879 if (_mesa_is_tex_instruction(inst->Opcode)) {
880#if 0
881 /* replace TEX/TXP/TXB with MOV */
882 inst->Opcode = OPCODE_MOV;
883 inst->DstReg.WriteMask = WRITEMASK_XYZW;
884 inst->SrcReg[0].Swizzle = SWIZZLE_XYZW;
885 inst->SrcReg[0].Negate = NEGATE_NONE;
886#endif
887
888#if 0
889 /* disable shadow texture mode */
890 inst->TexShadow = 0;
891#endif
892 }
893
894 if (inst->Opcode == OPCODE_TXP) {
895#if 0
896 inst->Opcode = OPCODE_MOV;
897 inst->DstReg.WriteMask = WRITEMASK_XYZW;
898 inst->SrcReg[0].File = PROGRAM_CONSTANT;
899 inst->SrcReg[0].Index = whiteIndex;
900 inst->SrcReg[0].Swizzle = SWIZZLE_XYZW;
901 inst->SrcReg[0].Negate = NEGATE_NONE;
902#endif
903#if 0
904 inst->TexShadow = 0;
905#endif
906#if 0
907 inst->Opcode = OPCODE_TEX;
908 inst->TexShadow = 0;
909#endif
910 }
911
912 }
913}