blob: f4f701b54615c79b412dfae5001e46fe57bb380d [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
77 ctx->VertexProgram.PointSizeEnabled = GL_TRUE;
78#else
Michal Krol2861e732004-03-29 11:09:34 +000079 ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
Brian Paulaf3d9db2008-08-12 10:00:36 -060080#endif
Michal Krol2861e732004-03-29 11:09:34 +000081 ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
Briandf43fb62008-05-06 23:08:51 -060082 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
83 ctx->Shared->DefaultVertexProgram);
Michal Krol2861e732004-03-29 11:09:34 +000084 assert(ctx->VertexProgram.Current);
Michal Krol2861e732004-03-29 11:09:34 +000085 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
86 ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
87 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
88 }
Brianb26aae62007-10-31 12:00:38 -060089 ctx->VertexProgram.Cache = _mesa_new_program_cache();
Michal Krol2861e732004-03-29 11:09:34 +000090#endif
91
92#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
93 ctx->FragmentProgram.Enabled = GL_FALSE;
Briandf43fb62008-05-06 23:08:51 -060094 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
95 ctx->Shared->DefaultFragmentProgram);
Michal Krol2861e732004-03-29 11:09:34 +000096 assert(ctx->FragmentProgram.Current);
Brianb26aae62007-10-31 12:00:38 -060097 ctx->FragmentProgram.Cache = _mesa_new_program_cache();
Michal Krol2861e732004-03-29 11:09:34 +000098#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +000099
Brianb26aae62007-10-31 12:00:38 -0600100
Brian Paul63d68302005-11-19 16:43:04 +0000101 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +0000102#if FEATURE_ATI_fragment_shader
103 ctx->ATIFragmentShader.Enabled = GL_FALSE;
Brian Paulb7eea9a2008-07-29 17:19:25 -0600104 ctx->ATIFragmentShader.Current = ctx->Shared->DefaultFragmentShader;
Dave Airlie7f752fe2004-12-19 03:06:59 +0000105 assert(ctx->ATIFragmentShader.Current);
Brian Paul63d68302005-11-19 16:43:04 +0000106 ctx->ATIFragmentShader.Current->RefCount++;
Dave Airlie7f752fe2004-12-19 03:06:59 +0000107#endif
Michal Krol2861e732004-03-29 11:09:34 +0000108}
109
110
111/**
Brian Paul21841f02004-08-14 14:28:11 +0000112 * Free a context's vertex/fragment program state
113 */
114void
115_mesa_free_program_data(GLcontext *ctx)
116{
Roland Scheidegger93da6732006-03-01 23:11:14 +0000117#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Briandf43fb62008-05-06 23:08:51 -0600118 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL);
Brian1631a952007-12-26 06:57:24 -0700119 _mesa_delete_program_cache(ctx, ctx->VertexProgram.Cache);
Brian Paul21841f02004-08-14 14:28:11 +0000120#endif
Roland Scheidegger93da6732006-03-01 23:11:14 +0000121#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Briandf43fb62008-05-06 23:08:51 -0600122 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL);
Brian1631a952007-12-26 06:57:24 -0700123 _mesa_delete_program_cache(ctx, ctx->FragmentProgram.Cache);
Brian Paul21841f02004-08-14 14:28:11 +0000124#endif
Brian Paul63d68302005-11-19 16:43:04 +0000125 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +0000126#if FEATURE_ATI_fragment_shader
127 if (ctx->ATIFragmentShader.Current) {
Brian Paul63d68302005-11-19 16:43:04 +0000128 ctx->ATIFragmentShader.Current->RefCount--;
129 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500130 free(ctx->ATIFragmentShader.Current);
Brian Paul63d68302005-11-19 16:43:04 +0000131 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000132 }
133#endif
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500134 free((void *) ctx->Program.ErrorString);
Brian Paul21841f02004-08-14 14:28:11 +0000135}
136
137
Brian4b654d42007-08-23 08:53:43 +0100138/**
139 * Update the default program objects in the given context to reference those
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200140 * specified in the shared state and release those referencing the old
Brian4b654d42007-08-23 08:53:43 +0100141 * shared state.
142 */
143void
144_mesa_update_default_objects_program(GLcontext *ctx)
145{
146#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Brian Paul57e222d2008-05-14 12:10:45 -0600147 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
148 (struct gl_vertex_program *)
149 ctx->Shared->DefaultVertexProgram);
Brian4b654d42007-08-23 08:53:43 +0100150 assert(ctx->VertexProgram.Current);
Brian4b654d42007-08-23 08:53:43 +0100151#endif
152
153#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Brian Paul57e222d2008-05-14 12:10:45 -0600154 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
155 (struct gl_fragment_program *)
156 ctx->Shared->DefaultFragmentProgram);
Brian4b654d42007-08-23 08:53:43 +0100157 assert(ctx->FragmentProgram.Current);
Brian4b654d42007-08-23 08:53:43 +0100158#endif
159
160 /* XXX probably move this stuff */
161#if FEATURE_ATI_fragment_shader
162 if (ctx->ATIFragmentShader.Current) {
163 ctx->ATIFragmentShader.Current->RefCount--;
164 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500165 free(ctx->ATIFragmentShader.Current);
Brian4b654d42007-08-23 08:53:43 +0100166 }
167 }
168 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
169 assert(ctx->ATIFragmentShader.Current);
170 ctx->ATIFragmentShader.Current->RefCount++;
171#endif
172}
Brian Paul21841f02004-08-14 14:28:11 +0000173
174
175/**
Michal Krol2861e732004-03-29 11:09:34 +0000176 * Set the vertex/fragment program error state (position and error string).
177 * This is generally called from within the parsers.
178 */
179void
180_mesa_set_program_error(GLcontext *ctx, GLint pos, const char *string)
181{
182 ctx->Program.ErrorPos = pos;
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500183 free((void *) ctx->Program.ErrorString);
Michal Krol2861e732004-03-29 11:09:34 +0000184 if (!string)
185 string = "";
186 ctx->Program.ErrorString = _mesa_strdup(string);
187}
188
189
190/**
191 * Find the line number and column for 'pos' within 'string'.
192 * Return a copy of the line which contains 'pos'. Free the line with
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500193 * free().
Michal Krol2861e732004-03-29 11:09:34 +0000194 * \param string the program string
195 * \param pos the position within the string
196 * \param line returns the line number corresponding to 'pos'.
197 * \param col returns the column number corresponding to 'pos'.
198 * \return copy of the line containing 'pos'.
199 */
200const GLubyte *
201_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
202 GLint *line, GLint *col)
203{
204 const GLubyte *lineStart = string;
205 const GLubyte *p = string;
206 GLubyte *s;
207 int len;
208
209 *line = 1;
210
211 while (p != pos) {
212 if (*p == (GLubyte) '\n') {
213 (*line)++;
214 lineStart = p + 1;
215 }
216 p++;
217 }
218
219 *col = (pos - lineStart) + 1;
220
221 /* return copy of this line */
222 while (*p != 0 && *p != '\n')
223 p++;
224 len = p - lineStart;
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500225 s = (GLubyte *) malloc(len + 1);
Kenneth Graunkec7ac48622010-02-18 23:50:59 -0800226 memcpy(s, lineStart, len);
Michal Krol2861e732004-03-29 11:09:34 +0000227 s[len] = 0;
228
229 return s;
230}
231
232
Brian Paul765f1a12004-09-14 22:28:27 +0000233/**
234 * Initialize a new vertex/fragment program object.
235 */
Brian Paul122629f2006-07-20 16:49:57 +0000236static struct gl_program *
237_mesa_init_program_struct( GLcontext *ctx, struct gl_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000238 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000239{
Brian Paula6c423d2004-08-25 15:59:48 +0000240 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000241 if (prog) {
Brian Paul0c78c762008-05-18 15:46:26 -0600242 GLuint i;
Brian Paul6bf1ea82010-02-19 08:32:36 -0700243 memset(prog, 0, sizeof(*prog));
Michal Krol2861e732004-03-29 11:09:34 +0000244 prog->Id = id;
245 prog->Target = target;
246 prog->Resident = GL_TRUE;
247 prog->RefCount = 1;
Brian Paul308b85f2006-11-23 15:58:30 +0000248 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
Brian Paul0c78c762008-05-18 15:46:26 -0600249
250 /* default mapping from samplers to texture units */
251 for (i = 0; i < MAX_SAMPLERS; i++)
252 prog->SamplerUnits[i] = i;
Michal Krol2861e732004-03-29 11:09:34 +0000253 }
254
255 return prog;
256}
257
Brian Paul765f1a12004-09-14 22:28:27 +0000258
259/**
260 * Initialize a new fragment program object.
261 */
Brian Paul122629f2006-07-20 16:49:57 +0000262struct gl_program *
263_mesa_init_fragment_program( GLcontext *ctx, struct gl_fragment_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000264 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000265{
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200266 if (prog)
Michal Krol2861e732004-03-29 11:09:34 +0000267 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
268 else
269 return NULL;
270}
271
Brian Paul765f1a12004-09-14 22:28:27 +0000272
273/**
274 * Initialize a new vertex program object.
275 */
Brian Paul122629f2006-07-20 16:49:57 +0000276struct gl_program *
277_mesa_init_vertex_program( GLcontext *ctx, struct gl_vertex_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000278 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000279{
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200280 if (prog)
Michal Krol2861e732004-03-29 11:09:34 +0000281 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
282 else
283 return NULL;
284}
285
286
287/**
288 * Allocate and initialize a new fragment/vertex program object but
289 * don't put it into the program hash table. Called via
290 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
291 * device driver function to implement OO deriviation with additional
292 * types not understood by this function.
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200293 *
Michal Krol2861e732004-03-29 11:09:34 +0000294 * \param ctx context
295 * \param id program id/number
296 * \param target program target/type
297 * \return pointer to new program object
298 */
Brian Paul122629f2006-07-20 16:49:57 +0000299struct gl_program *
Michal Krol2861e732004-03-29 11:09:34 +0000300_mesa_new_program(GLcontext *ctx, GLenum target, GLuint id)
301{
Brian Paula3e86d42008-05-16 09:56:11 -0600302 struct gl_program *prog;
Michal Krol2861e732004-03-29 11:09:34 +0000303 switch (target) {
304 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
Brian Paulc5af2ed2009-04-18 10:08:54 -0600305 case GL_VERTEX_STATE_PROGRAM_NV:
Brian Paula3e86d42008-05-16 09:56:11 -0600306 prog = _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
Brian Paul122629f2006-07-20 16:49:57 +0000307 target, id );
Brian Paula3e86d42008-05-16 09:56:11 -0600308 break;
Michal Krol2861e732004-03-29 11:09:34 +0000309 case GL_FRAGMENT_PROGRAM_NV:
310 case GL_FRAGMENT_PROGRAM_ARB:
Brian Paula3e86d42008-05-16 09:56:11 -0600311 prog =_mesa_init_fragment_program(ctx,
Brian Paul122629f2006-07-20 16:49:57 +0000312 CALLOC_STRUCT(gl_fragment_program),
313 target, id );
Brian Paula3e86d42008-05-16 09:56:11 -0600314 break;
Michal Krol2861e732004-03-29 11:09:34 +0000315 default:
316 _mesa_problem(ctx, "bad target in _mesa_new_program");
Brian Paula3e86d42008-05-16 09:56:11 -0600317 prog = NULL;
Michal Krol2861e732004-03-29 11:09:34 +0000318 }
Brian Paula3e86d42008-05-16 09:56:11 -0600319 return prog;
Michal Krol2861e732004-03-29 11:09:34 +0000320}
321
322
323/**
324 * Delete a program and remove it from the hash table, ignoring the
325 * reference count.
326 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
327 * by a device driver function.
328 */
329void
Brian Paul122629f2006-07-20 16:49:57 +0000330_mesa_delete_program(GLcontext *ctx, struct gl_program *prog)
Michal Krol2861e732004-03-29 11:09:34 +0000331{
Brian Paula6c423d2004-08-25 15:59:48 +0000332 (void) ctx;
Brian Paul57e222d2008-05-14 12:10:45 -0600333 ASSERT(prog);
334 ASSERT(prog->RefCount==0);
Michal Krol2861e732004-03-29 11:09:34 +0000335
Brian Paul308b85f2006-11-23 15:58:30 +0000336 if (prog == &_mesa_DummyProgram)
337 return;
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200338
Michal Krol2861e732004-03-29 11:09:34 +0000339 if (prog->String)
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500340 free(prog->String);
Brian Paulde997602005-11-12 17:53:14 +0000341
Brian Paul19ad9cf2008-05-14 12:39:41 -0600342 _mesa_free_instructions(prog->Instructions, prog->NumInstructions);
Brian Paulde997602005-11-12 17:53:14 +0000343
Brian Paul65a51c02006-05-24 03:30:31 +0000344 if (prog->Parameters) {
Brian Paulde997602005-11-12 17:53:14 +0000345 _mesa_free_parameter_list(prog->Parameters);
Brian Paul65a51c02006-05-24 03:30:31 +0000346 }
Brianfe1d01c2006-12-13 14:54:47 -0700347 if (prog->Varying) {
348 _mesa_free_parameter_list(prog->Varying);
349 }
Brian3493e862007-03-24 16:18:13 -0600350 if (prog->Attributes) {
351 _mesa_free_parameter_list(prog->Attributes);
352 }
Brianfe1d01c2006-12-13 14:54:47 -0700353
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500354 free(prog);
Michal Krol2861e732004-03-29 11:09:34 +0000355}
356
357
Brian Paul4d12a052006-08-23 23:10:14 +0000358/**
359 * Return the gl_program object for a given ID.
360 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
361 * casts elsewhere.
362 */
363struct gl_program *
364_mesa_lookup_program(GLcontext *ctx, GLuint id)
365{
366 if (id)
367 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
368 else
369 return NULL;
370}
371
Michal Krol2861e732004-03-29 11:09:34 +0000372
Brian Paul3b9b8de2006-08-24 21:57:36 +0000373/**
Briandf43fb62008-05-06 23:08:51 -0600374 * Reference counting for vertex/fragment programs
375 */
376void
377_mesa_reference_program(GLcontext *ctx,
378 struct gl_program **ptr,
379 struct gl_program *prog)
380{
381 assert(ptr);
382 if (*ptr && prog) {
383 /* sanity check */
Brian Paul4bc39c52008-09-26 07:40:45 -0600384 if ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB)
385 ASSERT(prog->Target == GL_VERTEX_PROGRAM_ARB);
386 else if ((*ptr)->Target == GL_FRAGMENT_PROGRAM_ARB)
387 ASSERT(prog->Target == GL_FRAGMENT_PROGRAM_ARB ||
388 prog->Target == GL_FRAGMENT_PROGRAM_NV);
Briandf43fb62008-05-06 23:08:51 -0600389 }
390 if (*ptr == prog) {
391 return; /* no change */
392 }
393 if (*ptr) {
394 GLboolean deleteFlag;
395
396 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
Brian Paulb4e75d62008-05-08 10:59:31 -0600397#if 0
Brian Paula3e86d42008-05-16 09:56:11 -0600398 printf("Program %p ID=%u Target=%s Refcount-- to %d\n",
399 *ptr, (*ptr)->Id,
400 ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB ? "VP" : "FP"),
401 (*ptr)->RefCount - 1);
Briandf43fb62008-05-06 23:08:51 -0600402#endif
403 ASSERT((*ptr)->RefCount > 0);
404 (*ptr)->RefCount--;
405
406 deleteFlag = ((*ptr)->RefCount == 0);
407 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200408
Briandf43fb62008-05-06 23:08:51 -0600409 if (deleteFlag) {
410 ASSERT(ctx);
411 ctx->Driver.DeleteProgram(ctx, *ptr);
412 }
413
414 *ptr = NULL;
415 }
416
417 assert(!*ptr);
418 if (prog) {
419 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
420 prog->RefCount++;
Brian Paulb4e75d62008-05-08 10:59:31 -0600421#if 0
Brian Paula3e86d42008-05-16 09:56:11 -0600422 printf("Program %p ID=%u Target=%s Refcount++ to %d\n",
423 prog, prog->Id,
424 (prog->Target == GL_VERTEX_PROGRAM_ARB ? "VP" : "FP"),
425 prog->RefCount);
Briandf43fb62008-05-06 23:08:51 -0600426#endif
427 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
428 }
429
430 *ptr = prog;
431}
432
433
434/**
Brianb2a3a852006-12-14 13:56:58 -0700435 * Return a copy of a program.
436 * XXX Problem here if the program object is actually OO-derivation
437 * made by a device driver.
438 */
439struct gl_program *
440_mesa_clone_program(GLcontext *ctx, const struct gl_program *prog)
441{
442 struct gl_program *clone;
443
Brian5b6858c2007-07-24 09:56:44 -0600444 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
Brianb2a3a852006-12-14 13:56:58 -0700445 if (!clone)
446 return NULL;
447
448 assert(clone->Target == prog->Target);
Briandf43fb62008-05-06 23:08:51 -0600449 assert(clone->RefCount == 1);
450
Brianb2a3a852006-12-14 13:56:58 -0700451 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
Brianb2a3a852006-12-14 13:56:58 -0700452 clone->Format = prog->Format;
453 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
454 if (!clone->Instructions) {
Brian Paul57e222d2008-05-14 12:10:45 -0600455 _mesa_reference_program(ctx, &clone, NULL);
Brianb2a3a852006-12-14 13:56:58 -0700456 return NULL;
457 }
Brian12229f12007-03-22 09:11:26 -0600458 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
459 prog->NumInstructions);
Brianb2a3a852006-12-14 13:56:58 -0700460 clone->InputsRead = prog->InputsRead;
461 clone->OutputsWritten = prog->OutputsWritten;
Brian Paul0c78c762008-05-18 15:46:26 -0600462 clone->SamplersUsed = prog->SamplersUsed;
Nicolai Haehnle82635aa2008-07-05 18:03:44 +0200463 clone->ShadowSamplers = prog->ShadowSamplers;
Brianc9db2232007-01-04 17:22:19 -0700464 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
465
Brian2e76f0a2006-12-19 09:52:07 -0700466 if (prog->Parameters)
467 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
Brianb2a3a852006-12-14 13:56:58 -0700468 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian2e76f0a2006-12-19 09:52:07 -0700469 if (prog->Varying)
470 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
Brian3209c3e2007-01-09 17:49:24 -0700471 if (prog->Attributes)
472 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
Brianb2a3a852006-12-14 13:56:58 -0700473 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
474 clone->NumInstructions = prog->NumInstructions;
475 clone->NumTemporaries = prog->NumTemporaries;
476 clone->NumParameters = prog->NumParameters;
477 clone->NumAttributes = prog->NumAttributes;
478 clone->NumAddressRegs = prog->NumAddressRegs;
479 clone->NumNativeInstructions = prog->NumNativeInstructions;
480 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
481 clone->NumNativeParameters = prog->NumNativeParameters;
482 clone->NumNativeAttributes = prog->NumNativeAttributes;
483 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -0700484 clone->NumAluInstructions = prog->NumAluInstructions;
485 clone->NumTexInstructions = prog->NumTexInstructions;
486 clone->NumTexIndirections = prog->NumTexIndirections;
487 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
488 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
489 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
Brianb2a3a852006-12-14 13:56:58 -0700490
491 switch (prog->Target) {
492 case GL_VERTEX_PROGRAM_ARB:
493 {
494 const struct gl_vertex_program *vp
495 = (const struct gl_vertex_program *) prog;
496 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
497 vpc->IsPositionInvariant = vp->IsPositionInvariant;
Nicolai Hähnle736e1ae2009-09-09 19:56:57 +0200498 vpc->IsNVProgram = vp->IsNVProgram;
Brianb2a3a852006-12-14 13:56:58 -0700499 }
500 break;
501 case GL_FRAGMENT_PROGRAM_ARB:
502 {
503 const struct gl_fragment_program *fp
504 = (const struct gl_fragment_program *) prog;
505 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
Brianb2a3a852006-12-14 13:56:58 -0700506 fpc->FogOption = fp->FogOption;
507 fpc->UsesKill = fp->UsesKill;
Brian Paulb947b1d2010-02-13 13:51:38 -0700508 fpc->OriginUpperLeft = fp->OriginUpperLeft;
509 fpc->PixelCenterInteger = fp->PixelCenterInteger;
Brianb2a3a852006-12-14 13:56:58 -0700510 }
511 break;
512 default:
513 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
514 }
515
516 return clone;
517}
518
519
Brian Paul19ad9cf2008-05-14 12:39:41 -0600520/**
521 * Insert 'count' NOP instructions at 'start' in the given program.
522 * Adjust branch targets accordingly.
523 */
524GLboolean
525_mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
526{
527 const GLuint origLen = prog->NumInstructions;
528 const GLuint newLen = origLen + count;
529 struct prog_instruction *newInst;
530 GLuint i;
531
532 /* adjust branches */
533 for (i = 0; i < prog->NumInstructions; i++) {
534 struct prog_instruction *inst = prog->Instructions + i;
535 if (inst->BranchTarget > 0) {
José Fonseca457d7212008-06-24 11:34:46 +0900536 if ((GLuint)inst->BranchTarget >= start) {
Brian Paul19ad9cf2008-05-14 12:39:41 -0600537 inst->BranchTarget += count;
538 }
539 }
540 }
541
542 /* Alloc storage for new instructions */
543 newInst = _mesa_alloc_instructions(newLen);
544 if (!newInst) {
545 return GL_FALSE;
546 }
547
548 /* Copy 'start' instructions into new instruction buffer */
549 _mesa_copy_instructions(newInst, prog->Instructions, start);
550
551 /* init the new instructions */
552 _mesa_init_instructions(newInst + start, count);
553
554 /* Copy the remaining/tail instructions to new inst buffer */
555 _mesa_copy_instructions(newInst + start + count,
556 prog->Instructions + start,
557 origLen - start);
558
559 /* free old instructions */
560 _mesa_free_instructions(prog->Instructions, origLen);
561
562 /* install new instructions */
563 prog->Instructions = newInst;
564 prog->NumInstructions = newLen;
565
566 return GL_TRUE;
567}
568
Brianb2a3a852006-12-14 13:56:58 -0700569/**
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200570 * Delete 'count' instructions at 'start' in the given program.
571 * Adjust branch targets accordingly.
572 */
573GLboolean
574_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count)
575{
576 const GLuint origLen = prog->NumInstructions;
577 const GLuint newLen = origLen - count;
578 struct prog_instruction *newInst;
579 GLuint i;
580
581 /* adjust branches */
582 for (i = 0; i < prog->NumInstructions; i++) {
583 struct prog_instruction *inst = prog->Instructions + i;
584 if (inst->BranchTarget > 0) {
Brian Paul20fbb242010-01-27 17:03:04 -0700585 if (inst->BranchTarget > (GLint) start) {
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200586 inst->BranchTarget -= count;
587 }
588 }
589 }
590
591 /* Alloc storage for new instructions */
592 newInst = _mesa_alloc_instructions(newLen);
593 if (!newInst) {
594 return GL_FALSE;
595 }
596
597 /* Copy 'start' instructions into new instruction buffer */
598 _mesa_copy_instructions(newInst, prog->Instructions, start);
599
600 /* Copy the remaining/tail instructions to new inst buffer */
601 _mesa_copy_instructions(newInst + start,
602 prog->Instructions + start + count,
603 newLen - start);
604
605 /* free old instructions */
606 _mesa_free_instructions(prog->Instructions, origLen);
607
608 /* install new instructions */
609 prog->Instructions = newInst;
610 prog->NumInstructions = newLen;
611
612 return GL_TRUE;
613}
614
615
616/**
Brian Paul6ca948a2008-05-14 12:53:03 -0600617 * Search instructions for registers that match (oldFile, oldIndex),
618 * replacing them with (newFile, newIndex).
619 */
620static void
621replace_registers(struct prog_instruction *inst, GLuint numInst,
622 GLuint oldFile, GLuint oldIndex,
623 GLuint newFile, GLuint newIndex)
624{
625 GLuint i, j;
626 for (i = 0; i < numInst; i++) {
Brian Paul0c78c762008-05-18 15:46:26 -0600627 /* src regs */
Brian Paul6ca948a2008-05-14 12:53:03 -0600628 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
629 if (inst[i].SrcReg[j].File == oldFile &&
630 inst[i].SrcReg[j].Index == oldIndex) {
631 inst[i].SrcReg[j].File = newFile;
632 inst[i].SrcReg[j].Index = newIndex;
633 }
634 }
Brian Paul0c78c762008-05-18 15:46:26 -0600635 /* dst reg */
636 if (inst[i].DstReg.File == oldFile && inst[i].DstReg.Index == oldIndex) {
637 inst[i].DstReg.File = newFile;
638 inst[i].DstReg.Index = newIndex;
639 }
Brian Paul6ca948a2008-05-14 12:53:03 -0600640 }
641}
642
643
644/**
645 * Search instructions for references to program parameters. When found,
646 * increment the parameter index by 'offset'.
647 * Used when combining programs.
648 */
649static void
650adjust_param_indexes(struct prog_instruction *inst, GLuint numInst,
651 GLuint offset)
652{
653 GLuint i, j;
654 for (i = 0; i < numInst; i++) {
655 for (j = 0; j < _mesa_num_inst_src_regs(inst->Opcode); j++) {
656 GLuint f = inst[i].SrcReg[j].File;
657 if (f == PROGRAM_CONSTANT ||
658 f == PROGRAM_UNIFORM ||
659 f == PROGRAM_STATE_VAR) {
660 inst[i].SrcReg[j].Index += offset;
661 }
662 }
663 }
664}
665
666
667/**
668 * Combine two programs into one. Fix instructions so the outputs of
669 * the first program go to the inputs of the second program.
670 */
671struct gl_program *
672_mesa_combine_programs(GLcontext *ctx,
Brian Paul0c78c762008-05-18 15:46:26 -0600673 const struct gl_program *progA,
674 const struct gl_program *progB)
Brian Paul6ca948a2008-05-14 12:53:03 -0600675{
676 struct prog_instruction *newInst;
677 struct gl_program *newProg;
678 const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */
679 const GLuint lenB = progB->NumInstructions;
680 const GLuint numParamsA = _mesa_num_parameters(progA->Parameters);
681 const GLuint newLength = lenA + lenB;
Brian Paula2ddb3d2010-02-01 18:00:12 -0700682 GLboolean usedTemps[MAX_PROGRAM_TEMPS];
683 GLuint firstTemp = 0;
Brian Paul0c78c762008-05-18 15:46:26 -0600684 GLbitfield inputsB;
Brian Paul6ca948a2008-05-14 12:53:03 -0600685 GLuint i;
686
687 ASSERT(progA->Target == progB->Target);
688
689 newInst = _mesa_alloc_instructions(newLength);
690 if (!newInst)
691 return GL_FALSE;
692
693 _mesa_copy_instructions(newInst, progA->Instructions, lenA);
694 _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB);
695
696 /* adjust branch / instruction addresses for B's instructions */
697 for (i = 0; i < lenB; i++) {
698 newInst[lenA + i].BranchTarget += lenA;
699 }
700
701 newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0);
702 newProg->Instructions = newInst;
703 newProg->NumInstructions = newLength;
704
Brian Paula2ddb3d2010-02-01 18:00:12 -0700705 /* find used temp regs (we may need new temps below) */
706 _mesa_find_used_registers(newProg, PROGRAM_TEMPORARY,
707 usedTemps, MAX_PROGRAM_TEMPS);
708
Brian Paul6ca948a2008-05-14 12:53:03 -0600709 if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul0c78c762008-05-18 15:46:26 -0600710 struct gl_fragment_program *fprogA, *fprogB, *newFprog;
Brian Paul5c4bd762008-10-08 14:02:24 -0600711 GLbitfield progB_inputsRead = progB->InputsRead;
712 GLint progB_colorFile, progB_colorIndex;
713
Brian Paul0c78c762008-05-18 15:46:26 -0600714 fprogA = (struct gl_fragment_program *) progA;
715 fprogB = (struct gl_fragment_program *) progB;
716 newFprog = (struct gl_fragment_program *) newProg;
717
718 newFprog->UsesKill = fprogA->UsesKill || fprogB->UsesKill;
719
Brian Paul5c4bd762008-10-08 14:02:24 -0600720 /* We'll do a search and replace for instances
721 * of progB_colorFile/progB_colorIndex below...
722 */
723 progB_colorFile = PROGRAM_INPUT;
724 progB_colorIndex = FRAG_ATTRIB_COL0;
725
726 /*
727 * The fragment program may get color from a state var rather than
728 * a fragment input (vertex output) if it's constant.
729 * See the texenvprogram.c code.
730 * So, search the program's parameter list now to see if the program
731 * gets color from a state var instead of a conventional fragment
732 * input register.
733 */
734 for (i = 0; i < progB->Parameters->NumParameters; i++) {
735 struct gl_program_parameter *p = &progB->Parameters->Parameters[i];
736 if (p->Type == PROGRAM_STATE_VAR &&
737 p->StateIndexes[0] == STATE_INTERNAL &&
738 p->StateIndexes[1] == STATE_CURRENT_ATTRIB &&
739 p->StateIndexes[2] == VERT_ATTRIB_COLOR0) {
740 progB_inputsRead |= FRAG_BIT_COL0;
741 progB_colorFile = PROGRAM_STATE_VAR;
742 progB_colorIndex = i;
743 break;
744 }
745 }
746
Brian Paul0c78c762008-05-18 15:46:26 -0600747 /* Connect color outputs of fprogA to color inputs of fprogB, via a
748 * new temporary register.
749 */
Brian Paul8d475822009-02-28 11:49:46 -0700750 if ((progA->OutputsWritten & (1 << FRAG_RESULT_COLOR)) &&
Brian Paul5c4bd762008-10-08 14:02:24 -0600751 (progB_inputsRead & FRAG_BIT_COL0)) {
Brian Paula2ddb3d2010-02-01 18:00:12 -0700752 GLint tempReg = _mesa_find_free_register(usedTemps, MAX_PROGRAM_TEMPS,
753 firstTemp);
Brian Paule469d782008-05-19 16:03:43 -0600754 if (tempReg < 0) {
Brian Paul0c78c762008-05-18 15:46:26 -0600755 _mesa_problem(ctx, "No free temp regs found in "
756 "_mesa_combine_programs(), using 31");
757 tempReg = 31;
758 }
Brian Paula2ddb3d2010-02-01 18:00:12 -0700759 firstTemp = tempReg + 1;
760
Brian Paul0c78c762008-05-18 15:46:26 -0600761 /* replace writes to result.color[0] with tempReg */
762 replace_registers(newInst, lenA,
Brian Paul8d475822009-02-28 11:49:46 -0700763 PROGRAM_OUTPUT, FRAG_RESULT_COLOR,
Brian Paul0c78c762008-05-18 15:46:26 -0600764 PROGRAM_TEMPORARY, tempReg);
Brian Paul5c4bd762008-10-08 14:02:24 -0600765 /* replace reads from the input color with tempReg */
Brian Paul6ca948a2008-05-14 12:53:03 -0600766 replace_registers(newInst + lenA, lenB,
Brian Paul5c4bd762008-10-08 14:02:24 -0600767 progB_colorFile, progB_colorIndex, /* search for */
768 PROGRAM_TEMPORARY, tempReg /* replace with */ );
Brian Paul6ca948a2008-05-14 12:53:03 -0600769 }
770
Brian Paul5c4bd762008-10-08 14:02:24 -0600771 /* compute combined program's InputsRead */
772 inputsB = progB_inputsRead;
Brian Paul8d475822009-02-28 11:49:46 -0700773 if (progA->OutputsWritten & (1 << FRAG_RESULT_COLOR)) {
Brian Paul0c78c762008-05-18 15:46:26 -0600774 inputsB &= ~(1 << FRAG_ATTRIB_COL0);
775 }
776 newProg->InputsRead = progA->InputsRead | inputsB;
Brian Paul6ca948a2008-05-14 12:53:03 -0600777 newProg->OutputsWritten = progB->OutputsWritten;
Brian Paul0c78c762008-05-18 15:46:26 -0600778 newProg->SamplersUsed = progA->SamplersUsed | progB->SamplersUsed;
Brian Paul6ca948a2008-05-14 12:53:03 -0600779 }
780 else {
781 /* vertex program */
782 assert(0); /* XXX todo */
783 }
784
785 /*
786 * Merge parameters (uniforms, constants, etc)
787 */
788 newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters,
789 progB->Parameters);
790
791 adjust_param_indexes(newInst + lenA, lenB, numParamsA);
792
793
794 return newProg;
795}
796
797
Brian Paul6ca948a2008-05-14 12:53:03 -0600798/**
Brian Paula2ddb3d2010-02-01 18:00:12 -0700799 * Populate the 'used' array with flags indicating which registers (TEMPs,
800 * INPUTs, OUTPUTs, etc, are used by the given program.
801 * \param file type of register to scan for
802 * \param used returns true/false flags for in use / free
803 * \param usedSize size of the 'used' array
Brian Paul6ca948a2008-05-14 12:53:03 -0600804 */
Brian Paula2ddb3d2010-02-01 18:00:12 -0700805void
806_mesa_find_used_registers(const struct gl_program *prog,
807 gl_register_file file,
808 GLboolean used[], GLuint usedSize)
Brian Paul6ca948a2008-05-14 12:53:03 -0600809{
Brian Paula2ddb3d2010-02-01 18:00:12 -0700810 GLuint i, j;
Brian Paul6ca948a2008-05-14 12:53:03 -0600811
Kenneth Graunke26f8fad2010-02-18 23:51:00 -0800812 memset(used, 0, usedSize);
Brian Paul6ca948a2008-05-14 12:53:03 -0600813
814 for (i = 0; i < prog->NumInstructions; i++) {
815 const struct prog_instruction *inst = prog->Instructions + i;
816 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
817
Brian Paula2ddb3d2010-02-01 18:00:12 -0700818 if (inst->DstReg.File == file) {
Brian Paul5076a4f2009-12-11 09:16:25 -0700819 used[inst->DstReg.Index] = GL_TRUE;
820 }
Brian Paula2ddb3d2010-02-01 18:00:12 -0700821
822 for (j = 0; j < n; j++) {
823 if (inst->SrcReg[j].File == file) {
824 used[inst->SrcReg[j].Index] = GL_TRUE;
Brian Paul6ca948a2008-05-14 12:53:03 -0600825 }
826 }
827 }
Brian Paula2ddb3d2010-02-01 18:00:12 -0700828}
Brian Paul6ca948a2008-05-14 12:53:03 -0600829
Brian Paula2ddb3d2010-02-01 18:00:12 -0700830
831/**
832 * Scan the given 'used' register flag array for the first entry
833 * that's >= firstReg.
834 * \param used vector of flags indicating registers in use (as returned
835 * by _mesa_find_used_registers())
836 * \param usedSize size of the 'used' array
837 * \param firstReg first register to start searching at
838 * \return index of unused register, or -1 if none.
839 */
840GLint
841_mesa_find_free_register(const GLboolean used[],
842 GLuint usedSize, GLuint firstReg)
843{
844 GLuint i;
845
846 assert(firstReg < usedSize);
847
848 for (i = firstReg; i < usedSize; i++)
Brian Paul6ca948a2008-05-14 12:53:03 -0600849 if (!used[i])
850 return i;
Brian Paul6ca948a2008-05-14 12:53:03 -0600851
852 return -1;
853}
Brian Paulec6ad7b2009-06-17 07:42:49 -0600854
855
Brian Paulec6ad7b2009-06-17 07:42:49 -0600856/**
857 * "Post-process" a GPU program. This is intended to be used for debugging.
858 * Example actions include no-op'ing instructions or changing instruction
859 * behaviour.
860 */
861void
862_mesa_postprocess_program(GLcontext *ctx, struct gl_program *prog)
863{
864 static const GLfloat white[4] = { 0.5, 0.5, 0.5, 0.5 };
865 GLuint i;
866 GLuint whiteSwizzle;
867 GLint whiteIndex = _mesa_add_unnamed_constant(prog->Parameters,
868 white, 4, &whiteSwizzle);
869
Brian Paul516d20f2009-06-17 07:43:44 -0600870 (void) whiteIndex;
871
Brian Paulec6ad7b2009-06-17 07:42:49 -0600872 for (i = 0; i < prog->NumInstructions; i++) {
873 struct prog_instruction *inst = prog->Instructions + i;
874 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
875
876 (void) n;
877
878 if (_mesa_is_tex_instruction(inst->Opcode)) {
879#if 0
880 /* replace TEX/TXP/TXB with MOV */
881 inst->Opcode = OPCODE_MOV;
882 inst->DstReg.WriteMask = WRITEMASK_XYZW;
883 inst->SrcReg[0].Swizzle = SWIZZLE_XYZW;
884 inst->SrcReg[0].Negate = NEGATE_NONE;
885#endif
886
887#if 0
888 /* disable shadow texture mode */
889 inst->TexShadow = 0;
890#endif
891 }
892
893 if (inst->Opcode == OPCODE_TXP) {
894#if 0
895 inst->Opcode = OPCODE_MOV;
896 inst->DstReg.WriteMask = WRITEMASK_XYZW;
897 inst->SrcReg[0].File = PROGRAM_CONSTANT;
898 inst->SrcReg[0].Index = whiteIndex;
899 inst->SrcReg[0].Swizzle = SWIZZLE_XYZW;
900 inst->SrcReg[0].Negate = NEGATE_NONE;
901#endif
902#if 0
903 inst->TexShadow = 0;
904#endif
905#if 0
906 inst->Opcode = OPCODE_TEX;
907 inst->TexShadow = 0;
908#endif
909 }
910
911 }
912}