blob: 78efca9f122bb2b136073f21191a8655a67fbfdb [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 Paul8ad821d2011-03-02 09:32:45 -070074 ASSERT(ctx->Const.VertexProgram.MaxAddressOffset <= (1 << INST_INDEX_BITS));
75 ASSERT(ctx->Const.FragmentProgram.MaxAddressOffset <= (1 << INST_INDEX_BITS));
76
Brian Pauldd528f02009-08-26 10:57:18 -060077 /* If this fails, increase prog_instruction::TexSrcUnit size */
Brian Paul874a2c02011-04-05 19:02:07 -060078 ASSERT(MAX_TEXTURE_UNITS <= (1 << 5));
Brian Pauldd528f02009-08-26 10:57:18 -060079
80 /* If this fails, increase prog_instruction::TexSrcTarget size */
Brian Paul874a2c02011-04-05 19:02:07 -060081 ASSERT(NUM_TEXTURE_TARGETS <= (1 << 3));
Brian Pauldd528f02009-08-26 10:57:18 -060082
Michal Krol2861e732004-03-29 11:09:34 +000083 ctx->Program.ErrorPos = -1;
84 ctx->Program.ErrorString = _mesa_strdup("");
85
86#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
87 ctx->VertexProgram.Enabled = GL_FALSE;
Brian Paulaf3d9db2008-08-12 10:00:36 -060088#if FEATURE_es2_glsl
Kristian Høgsbergf67b0202010-05-24 10:01:38 -040089 ctx->VertexProgram.PointSizeEnabled =
90 (ctx->API == API_OPENGLES2) ? GL_TRUE : GL_FALSE;
Brian Paulaf3d9db2008-08-12 10:00:36 -060091#else
Michal Krol2861e732004-03-29 11:09:34 +000092 ctx->VertexProgram.PointSizeEnabled = GL_FALSE;
Brian Paulaf3d9db2008-08-12 10:00:36 -060093#endif
Michal Krol2861e732004-03-29 11:09:34 +000094 ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
Briandf43fb62008-05-06 23:08:51 -060095 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
96 ctx->Shared->DefaultVertexProgram);
Michal Krol2861e732004-03-29 11:09:34 +000097 assert(ctx->VertexProgram.Current);
Michal Krol2861e732004-03-29 11:09:34 +000098 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_PARAMS / 4; i++) {
99 ctx->VertexProgram.TrackMatrix[i] = GL_NONE;
100 ctx->VertexProgram.TrackMatrixTransform[i] = GL_IDENTITY_NV;
101 }
Brianb26aae62007-10-31 12:00:38 -0600102 ctx->VertexProgram.Cache = _mesa_new_program_cache();
Michal Krol2861e732004-03-29 11:09:34 +0000103#endif
104
105#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
106 ctx->FragmentProgram.Enabled = GL_FALSE;
Briandf43fb62008-05-06 23:08:51 -0600107 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
108 ctx->Shared->DefaultFragmentProgram);
Michal Krol2861e732004-03-29 11:09:34 +0000109 assert(ctx->FragmentProgram.Current);
Brianb26aae62007-10-31 12:00:38 -0600110 ctx->FragmentProgram.Cache = _mesa_new_program_cache();
Michal Krol2861e732004-03-29 11:09:34 +0000111#endif
Dave Airlie7f752fe2004-12-19 03:06:59 +0000112
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400113#if FEATURE_ARB_geometry_shader4
114 ctx->GeometryProgram.Enabled = GL_FALSE;
115 /* right now by default we don't have a geometry program */
116 _mesa_reference_geomprog(ctx, &ctx->GeometryProgram.Current,
117 NULL);
118 ctx->GeometryProgram.Cache = _mesa_new_program_cache();
119#endif
Brianb26aae62007-10-31 12:00:38 -0600120
Brian Paul63d68302005-11-19 16:43:04 +0000121 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +0000122#if FEATURE_ATI_fragment_shader
123 ctx->ATIFragmentShader.Enabled = GL_FALSE;
Brian Paulb7eea9a2008-07-29 17:19:25 -0600124 ctx->ATIFragmentShader.Current = ctx->Shared->DefaultFragmentShader;
Dave Airlie7f752fe2004-12-19 03:06:59 +0000125 assert(ctx->ATIFragmentShader.Current);
Brian Paul63d68302005-11-19 16:43:04 +0000126 ctx->ATIFragmentShader.Current->RefCount++;
Dave Airlie7f752fe2004-12-19 03:06:59 +0000127#endif
Michal Krol2861e732004-03-29 11:09:34 +0000128}
129
130
131/**
Brian Paul21841f02004-08-14 14:28:11 +0000132 * Free a context's vertex/fragment program state
133 */
134void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400135_mesa_free_program_data(struct gl_context *ctx)
Brian Paul21841f02004-08-14 14:28:11 +0000136{
Roland Scheidegger93da6732006-03-01 23:11:14 +0000137#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Briandf43fb62008-05-06 23:08:51 -0600138 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL);
Brian1631a952007-12-26 06:57:24 -0700139 _mesa_delete_program_cache(ctx, ctx->VertexProgram.Cache);
Brian Paul21841f02004-08-14 14:28:11 +0000140#endif
Roland Scheidegger93da6732006-03-01 23:11:14 +0000141#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Briandf43fb62008-05-06 23:08:51 -0600142 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL);
Eric Anholt098f9c52011-03-12 15:00:55 -0800143 _mesa_delete_program_cache(ctx, ctx->FragmentProgram.Cache);
Brian Paul21841f02004-08-14 14:28:11 +0000144#endif
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400145#if FEATURE_ARB_geometry_shader4
146 _mesa_reference_geomprog(ctx, &ctx->GeometryProgram.Current, NULL);
147 _mesa_delete_program_cache(ctx, ctx->GeometryProgram.Cache);
148#endif
Brian Paul63d68302005-11-19 16:43:04 +0000149 /* XXX probably move this stuff */
Dave Airlie7f752fe2004-12-19 03:06:59 +0000150#if FEATURE_ATI_fragment_shader
151 if (ctx->ATIFragmentShader.Current) {
Brian Paul63d68302005-11-19 16:43:04 +0000152 ctx->ATIFragmentShader.Current->RefCount--;
153 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500154 free(ctx->ATIFragmentShader.Current);
Brian Paul63d68302005-11-19 16:43:04 +0000155 }
Dave Airlie7f752fe2004-12-19 03:06:59 +0000156 }
157#endif
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500158 free((void *) ctx->Program.ErrorString);
Brian Paul21841f02004-08-14 14:28:11 +0000159}
160
161
Brian4b654d42007-08-23 08:53:43 +0100162/**
163 * Update the default program objects in the given context to reference those
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200164 * specified in the shared state and release those referencing the old
Brian4b654d42007-08-23 08:53:43 +0100165 * shared state.
166 */
167void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400168_mesa_update_default_objects_program(struct gl_context *ctx)
Brian4b654d42007-08-23 08:53:43 +0100169{
170#if FEATURE_NV_vertex_program || FEATURE_ARB_vertex_program
Brian Paul57e222d2008-05-14 12:10:45 -0600171 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
172 (struct gl_vertex_program *)
173 ctx->Shared->DefaultVertexProgram);
Brian4b654d42007-08-23 08:53:43 +0100174 assert(ctx->VertexProgram.Current);
Brian4b654d42007-08-23 08:53:43 +0100175#endif
176
177#if FEATURE_NV_fragment_program || FEATURE_ARB_fragment_program
Brian Paul57e222d2008-05-14 12:10:45 -0600178 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
179 (struct gl_fragment_program *)
180 ctx->Shared->DefaultFragmentProgram);
Brian4b654d42007-08-23 08:53:43 +0100181 assert(ctx->FragmentProgram.Current);
Brian4b654d42007-08-23 08:53:43 +0100182#endif
183
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400184#if FEATURE_ARB_geometry_shader4
185 _mesa_reference_geomprog(ctx, &ctx->GeometryProgram.Current,
186 (struct gl_geometry_program *)
187 ctx->Shared->DefaultGeometryProgram);
188#endif
189
Brian4b654d42007-08-23 08:53:43 +0100190 /* XXX probably move this stuff */
191#if FEATURE_ATI_fragment_shader
192 if (ctx->ATIFragmentShader.Current) {
193 ctx->ATIFragmentShader.Current->RefCount--;
194 if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500195 free(ctx->ATIFragmentShader.Current);
Brian4b654d42007-08-23 08:53:43 +0100196 }
197 }
198 ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
199 assert(ctx->ATIFragmentShader.Current);
200 ctx->ATIFragmentShader.Current->RefCount++;
201#endif
202}
Brian Paul21841f02004-08-14 14:28:11 +0000203
204
205/**
Michal Krol2861e732004-03-29 11:09:34 +0000206 * Set the vertex/fragment program error state (position and error string).
207 * This is generally called from within the parsers.
208 */
209void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400210_mesa_set_program_error(struct gl_context *ctx, GLint pos, const char *string)
Michal Krol2861e732004-03-29 11:09:34 +0000211{
212 ctx->Program.ErrorPos = pos;
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500213 free((void *) ctx->Program.ErrorString);
Michal Krol2861e732004-03-29 11:09:34 +0000214 if (!string)
215 string = "";
216 ctx->Program.ErrorString = _mesa_strdup(string);
217}
218
219
220/**
221 * Find the line number and column for 'pos' within 'string'.
222 * Return a copy of the line which contains 'pos'. Free the line with
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500223 * free().
Michal Krol2861e732004-03-29 11:09:34 +0000224 * \param string the program string
225 * \param pos the position within the string
226 * \param line returns the line number corresponding to 'pos'.
227 * \param col returns the column number corresponding to 'pos'.
228 * \return copy of the line containing 'pos'.
229 */
230const GLubyte *
231_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
232 GLint *line, GLint *col)
233{
234 const GLubyte *lineStart = string;
235 const GLubyte *p = string;
236 GLubyte *s;
237 int len;
238
239 *line = 1;
240
241 while (p != pos) {
242 if (*p == (GLubyte) '\n') {
243 (*line)++;
244 lineStart = p + 1;
245 }
246 p++;
247 }
248
249 *col = (pos - lineStart) + 1;
250
251 /* return copy of this line */
252 while (*p != 0 && *p != '\n')
253 p++;
254 len = p - lineStart;
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500255 s = (GLubyte *) malloc(len + 1);
Kenneth Graunkec7ac48622010-02-18 23:50:59 -0800256 memcpy(s, lineStart, len);
Michal Krol2861e732004-03-29 11:09:34 +0000257 s[len] = 0;
258
259 return s;
260}
261
262
Brian Paul765f1a12004-09-14 22:28:27 +0000263/**
264 * Initialize a new vertex/fragment program object.
265 */
Brian Paul122629f2006-07-20 16:49:57 +0000266static struct gl_program *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400267_mesa_init_program_struct( struct gl_context *ctx, struct gl_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000268 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000269{
Brian Paula6c423d2004-08-25 15:59:48 +0000270 (void) ctx;
Michal Krol2861e732004-03-29 11:09:34 +0000271 if (prog) {
Brian Paul0c78c762008-05-18 15:46:26 -0600272 GLuint i;
Brian Paul6bf1ea82010-02-19 08:32:36 -0700273 memset(prog, 0, sizeof(*prog));
Michal Krol2861e732004-03-29 11:09:34 +0000274 prog->Id = id;
275 prog->Target = target;
276 prog->Resident = GL_TRUE;
277 prog->RefCount = 1;
Brian Paul308b85f2006-11-23 15:58:30 +0000278 prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
Brian Paul0c78c762008-05-18 15:46:26 -0600279
280 /* default mapping from samplers to texture units */
281 for (i = 0; i < MAX_SAMPLERS; i++)
282 prog->SamplerUnits[i] = i;
Michal Krol2861e732004-03-29 11:09:34 +0000283 }
284
285 return prog;
286}
287
Brian Paul765f1a12004-09-14 22:28:27 +0000288
289/**
290 * Initialize a new fragment program object.
291 */
Brian Paul122629f2006-07-20 16:49:57 +0000292struct gl_program *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400293_mesa_init_fragment_program( struct gl_context *ctx, struct gl_fragment_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000294 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000295{
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200296 if (prog)
Michal Krol2861e732004-03-29 11:09:34 +0000297 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
298 else
299 return NULL;
300}
301
Brian Paul765f1a12004-09-14 22:28:27 +0000302
303/**
304 * Initialize a new vertex program object.
305 */
Brian Paul122629f2006-07-20 16:49:57 +0000306struct gl_program *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400307_mesa_init_vertex_program( struct gl_context *ctx, struct gl_vertex_program *prog,
Brian Paul765f1a12004-09-14 22:28:27 +0000308 GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000309{
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200310 if (prog)
Michal Krol2861e732004-03-29 11:09:34 +0000311 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
312 else
313 return NULL;
314}
315
316
317/**
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400318 * Initialize a new geometry program object.
319 */
320struct gl_program *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400321_mesa_init_geometry_program( struct gl_context *ctx, struct gl_geometry_program *prog,
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400322 GLenum target, GLuint id)
323{
324 if (prog)
325 return _mesa_init_program_struct( ctx, &prog->Base, target, id );
326 else
327 return NULL;
328}
329
330
331/**
Michal Krol2861e732004-03-29 11:09:34 +0000332 * Allocate and initialize a new fragment/vertex program object but
333 * don't put it into the program hash table. Called via
334 * ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
335 * device driver function to implement OO deriviation with additional
336 * types not understood by this function.
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200337 *
Michal Krol2861e732004-03-29 11:09:34 +0000338 * \param ctx context
339 * \param id program id/number
340 * \param target program target/type
341 * \return pointer to new program object
342 */
Brian Paul122629f2006-07-20 16:49:57 +0000343struct gl_program *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400344_mesa_new_program(struct gl_context *ctx, GLenum target, GLuint id)
Michal Krol2861e732004-03-29 11:09:34 +0000345{
Brian Paula3e86d42008-05-16 09:56:11 -0600346 struct gl_program *prog;
Michal Krol2861e732004-03-29 11:09:34 +0000347 switch (target) {
348 case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
Brian Paulc5af2ed2009-04-18 10:08:54 -0600349 case GL_VERTEX_STATE_PROGRAM_NV:
Brian Paula3e86d42008-05-16 09:56:11 -0600350 prog = _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
Brian Paul122629f2006-07-20 16:49:57 +0000351 target, id );
Brian Paula3e86d42008-05-16 09:56:11 -0600352 break;
Michal Krol2861e732004-03-29 11:09:34 +0000353 case GL_FRAGMENT_PROGRAM_NV:
354 case GL_FRAGMENT_PROGRAM_ARB:
Brian Paula3e86d42008-05-16 09:56:11 -0600355 prog =_mesa_init_fragment_program(ctx,
Brian Paul122629f2006-07-20 16:49:57 +0000356 CALLOC_STRUCT(gl_fragment_program),
357 target, id );
Brian Paula3e86d42008-05-16 09:56:11 -0600358 break;
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400359 case MESA_GEOMETRY_PROGRAM:
360 prog = _mesa_init_geometry_program(ctx,
361 CALLOC_STRUCT(gl_geometry_program),
362 target, id);
363 break;
Michal Krol2861e732004-03-29 11:09:34 +0000364 default:
365 _mesa_problem(ctx, "bad target in _mesa_new_program");
Brian Paula3e86d42008-05-16 09:56:11 -0600366 prog = NULL;
Michal Krol2861e732004-03-29 11:09:34 +0000367 }
Brian Paula3e86d42008-05-16 09:56:11 -0600368 return prog;
Michal Krol2861e732004-03-29 11:09:34 +0000369}
370
371
372/**
373 * Delete a program and remove it from the hash table, ignoring the
374 * reference count.
375 * Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
376 * by a device driver function.
377 */
378void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400379_mesa_delete_program(struct gl_context *ctx, struct gl_program *prog)
Michal Krol2861e732004-03-29 11:09:34 +0000380{
Brian Paula6c423d2004-08-25 15:59:48 +0000381 (void) ctx;
Brian Paul57e222d2008-05-14 12:10:45 -0600382 ASSERT(prog);
383 ASSERT(prog->RefCount==0);
Michal Krol2861e732004-03-29 11:09:34 +0000384
Brian Paul308b85f2006-11-23 15:58:30 +0000385 if (prog == &_mesa_DummyProgram)
386 return;
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200387
Michal Krol2861e732004-03-29 11:09:34 +0000388 if (prog->String)
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500389 free(prog->String);
Brian Paulde997602005-11-12 17:53:14 +0000390
Brian Paul19ad9cf2008-05-14 12:39:41 -0600391 _mesa_free_instructions(prog->Instructions, prog->NumInstructions);
Brian Paulde997602005-11-12 17:53:14 +0000392
Brian Paul65a51c02006-05-24 03:30:31 +0000393 if (prog->Parameters) {
Brian Paulde997602005-11-12 17:53:14 +0000394 _mesa_free_parameter_list(prog->Parameters);
Brian Paul65a51c02006-05-24 03:30:31 +0000395 }
Brianfe1d01c2006-12-13 14:54:47 -0700396 if (prog->Varying) {
397 _mesa_free_parameter_list(prog->Varying);
398 }
Brian3493e862007-03-24 16:18:13 -0600399 if (prog->Attributes) {
400 _mesa_free_parameter_list(prog->Attributes);
401 }
Brianfe1d01c2006-12-13 14:54:47 -0700402
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500403 free(prog);
Michal Krol2861e732004-03-29 11:09:34 +0000404}
405
406
Brian Paul4d12a052006-08-23 23:10:14 +0000407/**
408 * Return the gl_program object for a given ID.
409 * Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
410 * casts elsewhere.
411 */
412struct gl_program *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400413_mesa_lookup_program(struct gl_context *ctx, GLuint id)
Brian Paul4d12a052006-08-23 23:10:14 +0000414{
415 if (id)
416 return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
417 else
418 return NULL;
419}
420
Michal Krol2861e732004-03-29 11:09:34 +0000421
Brian Paul3b9b8de2006-08-24 21:57:36 +0000422/**
Briandf43fb62008-05-06 23:08:51 -0600423 * Reference counting for vertex/fragment programs
424 */
425void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400426_mesa_reference_program(struct gl_context *ctx,
Briandf43fb62008-05-06 23:08:51 -0600427 struct gl_program **ptr,
428 struct gl_program *prog)
429{
430 assert(ptr);
431 if (*ptr && prog) {
432 /* sanity check */
Brian Paul4bc39c52008-09-26 07:40:45 -0600433 if ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB)
434 ASSERT(prog->Target == GL_VERTEX_PROGRAM_ARB);
435 else if ((*ptr)->Target == GL_FRAGMENT_PROGRAM_ARB)
436 ASSERT(prog->Target == GL_FRAGMENT_PROGRAM_ARB ||
437 prog->Target == GL_FRAGMENT_PROGRAM_NV);
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400438 else if ((*ptr)->Target == MESA_GEOMETRY_PROGRAM)
439 ASSERT(prog->Target == MESA_GEOMETRY_PROGRAM);
Briandf43fb62008-05-06 23:08:51 -0600440 }
441 if (*ptr == prog) {
442 return; /* no change */
443 }
444 if (*ptr) {
445 GLboolean deleteFlag;
446
447 /*_glthread_LOCK_MUTEX((*ptr)->Mutex);*/
Brian Paulb4e75d62008-05-08 10:59:31 -0600448#if 0
Brian Paula3e86d42008-05-16 09:56:11 -0600449 printf("Program %p ID=%u Target=%s Refcount-- to %d\n",
450 *ptr, (*ptr)->Id,
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400451 ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB ? "VP" :
452 ((*ptr)->Target == MESA_GEOMETRY_PROGRAM ? "GP" : "FP")),
Brian Paula3e86d42008-05-16 09:56:11 -0600453 (*ptr)->RefCount - 1);
Briandf43fb62008-05-06 23:08:51 -0600454#endif
455 ASSERT((*ptr)->RefCount > 0);
456 (*ptr)->RefCount--;
457
458 deleteFlag = ((*ptr)->RefCount == 0);
459 /*_glthread_UNLOCK_MUTEX((*ptr)->Mutex);*/
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200460
Briandf43fb62008-05-06 23:08:51 -0600461 if (deleteFlag) {
462 ASSERT(ctx);
463 ctx->Driver.DeleteProgram(ctx, *ptr);
464 }
465
466 *ptr = NULL;
467 }
468
469 assert(!*ptr);
470 if (prog) {
471 /*_glthread_LOCK_MUTEX(prog->Mutex);*/
472 prog->RefCount++;
Brian Paulb4e75d62008-05-08 10:59:31 -0600473#if 0
Brian Paula3e86d42008-05-16 09:56:11 -0600474 printf("Program %p ID=%u Target=%s Refcount++ to %d\n",
475 prog, prog->Id,
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400476 (prog->Target == GL_VERTEX_PROGRAM_ARB ? "VP" :
477 (prog->Target == MESA_GEOMETRY_PROGRAM ? "GP" : "FP")),
Brian Paula3e86d42008-05-16 09:56:11 -0600478 prog->RefCount);
Briandf43fb62008-05-06 23:08:51 -0600479#endif
480 /*_glthread_UNLOCK_MUTEX(prog->Mutex);*/
481 }
482
483 *ptr = prog;
484}
485
486
487/**
Brianb2a3a852006-12-14 13:56:58 -0700488 * Return a copy of a program.
489 * XXX Problem here if the program object is actually OO-derivation
490 * made by a device driver.
491 */
492struct gl_program *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400493_mesa_clone_program(struct gl_context *ctx, const struct gl_program *prog)
Brianb2a3a852006-12-14 13:56:58 -0700494{
495 struct gl_program *clone;
496
Brian5b6858c2007-07-24 09:56:44 -0600497 clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
Brianb2a3a852006-12-14 13:56:58 -0700498 if (!clone)
499 return NULL;
500
501 assert(clone->Target == prog->Target);
Briandf43fb62008-05-06 23:08:51 -0600502 assert(clone->RefCount == 1);
503
Brianb2a3a852006-12-14 13:56:58 -0700504 clone->String = (GLubyte *) _mesa_strdup((char *) prog->String);
Brianb2a3a852006-12-14 13:56:58 -0700505 clone->Format = prog->Format;
506 clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
507 if (!clone->Instructions) {
Brian Paul57e222d2008-05-14 12:10:45 -0600508 _mesa_reference_program(ctx, &clone, NULL);
Brianb2a3a852006-12-14 13:56:58 -0700509 return NULL;
510 }
Brian12229f12007-03-22 09:11:26 -0600511 _mesa_copy_instructions(clone->Instructions, prog->Instructions,
512 prog->NumInstructions);
Brianb2a3a852006-12-14 13:56:58 -0700513 clone->InputsRead = prog->InputsRead;
514 clone->OutputsWritten = prog->OutputsWritten;
Brian Paul0c78c762008-05-18 15:46:26 -0600515 clone->SamplersUsed = prog->SamplersUsed;
Nicolai Haehnle82635aa2008-07-05 18:03:44 +0200516 clone->ShadowSamplers = prog->ShadowSamplers;
Brianc9db2232007-01-04 17:22:19 -0700517 memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
518
Brian2e76f0a2006-12-19 09:52:07 -0700519 if (prog->Parameters)
520 clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
Brianb2a3a852006-12-14 13:56:58 -0700521 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian2e76f0a2006-12-19 09:52:07 -0700522 if (prog->Varying)
523 clone->Varying = _mesa_clone_parameter_list(prog->Varying);
Brian3209c3e2007-01-09 17:49:24 -0700524 if (prog->Attributes)
525 clone->Attributes = _mesa_clone_parameter_list(prog->Attributes);
Brianb2a3a852006-12-14 13:56:58 -0700526 memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
Brian Paul56643092010-07-30 14:24:23 -0600527 clone->IndirectRegisterFiles = prog->IndirectRegisterFiles;
Brianb2a3a852006-12-14 13:56:58 -0700528 clone->NumInstructions = prog->NumInstructions;
529 clone->NumTemporaries = prog->NumTemporaries;
530 clone->NumParameters = prog->NumParameters;
531 clone->NumAttributes = prog->NumAttributes;
532 clone->NumAddressRegs = prog->NumAddressRegs;
533 clone->NumNativeInstructions = prog->NumNativeInstructions;
534 clone->NumNativeTemporaries = prog->NumNativeTemporaries;
535 clone->NumNativeParameters = prog->NumNativeParameters;
536 clone->NumNativeAttributes = prog->NumNativeAttributes;
537 clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
Brian21f99792007-01-09 11:00:21 -0700538 clone->NumAluInstructions = prog->NumAluInstructions;
539 clone->NumTexInstructions = prog->NumTexInstructions;
540 clone->NumTexIndirections = prog->NumTexIndirections;
541 clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
542 clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
543 clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
Brianb2a3a852006-12-14 13:56:58 -0700544
545 switch (prog->Target) {
546 case GL_VERTEX_PROGRAM_ARB:
547 {
548 const struct gl_vertex_program *vp
549 = (const struct gl_vertex_program *) prog;
550 struct gl_vertex_program *vpc = (struct gl_vertex_program *) clone;
551 vpc->IsPositionInvariant = vp->IsPositionInvariant;
Nicolai Hähnle736e1ae2009-09-09 19:56:57 +0200552 vpc->IsNVProgram = vp->IsNVProgram;
Brianb2a3a852006-12-14 13:56:58 -0700553 }
554 break;
555 case GL_FRAGMENT_PROGRAM_ARB:
556 {
557 const struct gl_fragment_program *fp
558 = (const struct gl_fragment_program *) prog;
559 struct gl_fragment_program *fpc = (struct gl_fragment_program *) clone;
Brianb2a3a852006-12-14 13:56:58 -0700560 fpc->UsesKill = fp->UsesKill;
Brian Paulb947b1d2010-02-13 13:51:38 -0700561 fpc->OriginUpperLeft = fp->OriginUpperLeft;
562 fpc->PixelCenterInteger = fp->PixelCenterInteger;
Brianb2a3a852006-12-14 13:56:58 -0700563 }
564 break;
Zack Rusinda7bd6a2010-06-28 17:31:21 -0400565 case MESA_GEOMETRY_PROGRAM:
566 {
567 const struct gl_geometry_program *gp
568 = (const struct gl_geometry_program *) prog;
569 struct gl_geometry_program *gpc = (struct gl_geometry_program *) clone;
570 gpc->VerticesOut = gp->VerticesOut;
571 gpc->InputType = gp->InputType;
572 gpc->OutputType = gp->OutputType;
573 }
574 break;
Brianb2a3a852006-12-14 13:56:58 -0700575 default:
576 _mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
577 }
578
579 return clone;
580}
581
582
Brian Paul19ad9cf2008-05-14 12:39:41 -0600583/**
584 * Insert 'count' NOP instructions at 'start' in the given program.
585 * Adjust branch targets accordingly.
586 */
587GLboolean
588_mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
589{
590 const GLuint origLen = prog->NumInstructions;
591 const GLuint newLen = origLen + count;
592 struct prog_instruction *newInst;
593 GLuint i;
594
595 /* adjust branches */
596 for (i = 0; i < prog->NumInstructions; i++) {
597 struct prog_instruction *inst = prog->Instructions + i;
598 if (inst->BranchTarget > 0) {
José Fonseca457d7212008-06-24 11:34:46 +0900599 if ((GLuint)inst->BranchTarget >= start) {
Brian Paul19ad9cf2008-05-14 12:39:41 -0600600 inst->BranchTarget += count;
601 }
602 }
603 }
604
605 /* Alloc storage for new instructions */
606 newInst = _mesa_alloc_instructions(newLen);
607 if (!newInst) {
608 return GL_FALSE;
609 }
610
611 /* Copy 'start' instructions into new instruction buffer */
612 _mesa_copy_instructions(newInst, prog->Instructions, start);
613
614 /* init the new instructions */
615 _mesa_init_instructions(newInst + start, count);
616
617 /* Copy the remaining/tail instructions to new inst buffer */
618 _mesa_copy_instructions(newInst + start + count,
619 prog->Instructions + start,
620 origLen - start);
621
622 /* free old instructions */
623 _mesa_free_instructions(prog->Instructions, origLen);
624
625 /* install new instructions */
626 prog->Instructions = newInst;
627 prog->NumInstructions = newLen;
628
629 return GL_TRUE;
630}
631
Brianb2a3a852006-12-14 13:56:58 -0700632/**
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200633 * Delete 'count' instructions at 'start' in the given program.
634 * Adjust branch targets accordingly.
635 */
636GLboolean
637_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count)
638{
639 const GLuint origLen = prog->NumInstructions;
640 const GLuint newLen = origLen - count;
641 struct prog_instruction *newInst;
642 GLuint i;
643
644 /* adjust branches */
645 for (i = 0; i < prog->NumInstructions; i++) {
646 struct prog_instruction *inst = prog->Instructions + i;
647 if (inst->BranchTarget > 0) {
Brian Paul20fbb242010-01-27 17:03:04 -0700648 if (inst->BranchTarget > (GLint) start) {
Nicolai Haehnled8d086c2008-07-06 19:48:50 +0200649 inst->BranchTarget -= count;
650 }
651 }
652 }
653
654 /* Alloc storage for new instructions */
655 newInst = _mesa_alloc_instructions(newLen);
656 if (!newInst) {
657 return GL_FALSE;
658 }
659
660 /* Copy 'start' instructions into new instruction buffer */
661 _mesa_copy_instructions(newInst, prog->Instructions, start);
662
663 /* Copy the remaining/tail instructions to new inst buffer */
664 _mesa_copy_instructions(newInst + start,
665 prog->Instructions + start + count,
666 newLen - start);
667
668 /* free old instructions */
669 _mesa_free_instructions(prog->Instructions, origLen);
670
671 /* install new instructions */
672 prog->Instructions = newInst;
673 prog->NumInstructions = newLen;
674
675 return GL_TRUE;
676}
677
678
679/**
Brian Paul6ca948a2008-05-14 12:53:03 -0600680 * Search instructions for registers that match (oldFile, oldIndex),
681 * replacing them with (newFile, newIndex).
682 */
683static void
684replace_registers(struct prog_instruction *inst, GLuint numInst,
685 GLuint oldFile, GLuint oldIndex,
686 GLuint newFile, GLuint newIndex)
687{
688 GLuint i, j;
689 for (i = 0; i < numInst; i++) {
Brian Paul0c78c762008-05-18 15:46:26 -0600690 /* src regs */
Brian Paulb22a00b2010-04-09 10:03:31 -0600691 for (j = 0; j < _mesa_num_inst_src_regs(inst[i].Opcode); j++) {
Brian Paul6ca948a2008-05-14 12:53:03 -0600692 if (inst[i].SrcReg[j].File == oldFile &&
693 inst[i].SrcReg[j].Index == oldIndex) {
694 inst[i].SrcReg[j].File = newFile;
695 inst[i].SrcReg[j].Index = newIndex;
696 }
697 }
Brian Paul0c78c762008-05-18 15:46:26 -0600698 /* dst reg */
699 if (inst[i].DstReg.File == oldFile && inst[i].DstReg.Index == oldIndex) {
700 inst[i].DstReg.File = newFile;
701 inst[i].DstReg.Index = newIndex;
702 }
Brian Paul6ca948a2008-05-14 12:53:03 -0600703 }
704}
705
706
707/**
708 * Search instructions for references to program parameters. When found,
709 * increment the parameter index by 'offset'.
710 * Used when combining programs.
711 */
712static void
713adjust_param_indexes(struct prog_instruction *inst, GLuint numInst,
714 GLuint offset)
715{
716 GLuint i, j;
717 for (i = 0; i < numInst; i++) {
Brian Paulb22a00b2010-04-09 10:03:31 -0600718 for (j = 0; j < _mesa_num_inst_src_regs(inst[i].Opcode); j++) {
Brian Paul6ca948a2008-05-14 12:53:03 -0600719 GLuint f = inst[i].SrcReg[j].File;
720 if (f == PROGRAM_CONSTANT ||
721 f == PROGRAM_UNIFORM ||
722 f == PROGRAM_STATE_VAR) {
723 inst[i].SrcReg[j].Index += offset;
724 }
725 }
726 }
727}
728
729
730/**
731 * Combine two programs into one. Fix instructions so the outputs of
732 * the first program go to the inputs of the second program.
733 */
734struct gl_program *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400735_mesa_combine_programs(struct gl_context *ctx,
Brian Paul0c78c762008-05-18 15:46:26 -0600736 const struct gl_program *progA,
737 const struct gl_program *progB)
Brian Paul6ca948a2008-05-14 12:53:03 -0600738{
739 struct prog_instruction *newInst;
740 struct gl_program *newProg;
741 const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */
742 const GLuint lenB = progB->NumInstructions;
743 const GLuint numParamsA = _mesa_num_parameters(progA->Parameters);
744 const GLuint newLength = lenA + lenB;
Brian Paula2ddb3d2010-02-01 18:00:12 -0700745 GLboolean usedTemps[MAX_PROGRAM_TEMPS];
746 GLuint firstTemp = 0;
Brian Paul0c78c762008-05-18 15:46:26 -0600747 GLbitfield inputsB;
Brian Paul6ca948a2008-05-14 12:53:03 -0600748 GLuint i;
749
750 ASSERT(progA->Target == progB->Target);
751
752 newInst = _mesa_alloc_instructions(newLength);
753 if (!newInst)
754 return GL_FALSE;
755
756 _mesa_copy_instructions(newInst, progA->Instructions, lenA);
757 _mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB);
758
759 /* adjust branch / instruction addresses for B's instructions */
760 for (i = 0; i < lenB; i++) {
761 newInst[lenA + i].BranchTarget += lenA;
762 }
763
764 newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0);
765 newProg->Instructions = newInst;
766 newProg->NumInstructions = newLength;
767
Brian Paula2ddb3d2010-02-01 18:00:12 -0700768 /* find used temp regs (we may need new temps below) */
769 _mesa_find_used_registers(newProg, PROGRAM_TEMPORARY,
770 usedTemps, MAX_PROGRAM_TEMPS);
771
Brian Paul6ca948a2008-05-14 12:53:03 -0600772 if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) {
Brian Paul0c78c762008-05-18 15:46:26 -0600773 struct gl_fragment_program *fprogA, *fprogB, *newFprog;
Brian Paul5c4bd762008-10-08 14:02:24 -0600774 GLbitfield progB_inputsRead = progB->InputsRead;
775 GLint progB_colorFile, progB_colorIndex;
776
Brian Paul0c78c762008-05-18 15:46:26 -0600777 fprogA = (struct gl_fragment_program *) progA;
778 fprogB = (struct gl_fragment_program *) progB;
779 newFprog = (struct gl_fragment_program *) newProg;
780
781 newFprog->UsesKill = fprogA->UsesKill || fprogB->UsesKill;
782
Brian Paul5c4bd762008-10-08 14:02:24 -0600783 /* We'll do a search and replace for instances
784 * of progB_colorFile/progB_colorIndex below...
785 */
786 progB_colorFile = PROGRAM_INPUT;
787 progB_colorIndex = FRAG_ATTRIB_COL0;
788
789 /*
790 * The fragment program may get color from a state var rather than
791 * a fragment input (vertex output) if it's constant.
792 * See the texenvprogram.c code.
793 * So, search the program's parameter list now to see if the program
794 * gets color from a state var instead of a conventional fragment
795 * input register.
796 */
797 for (i = 0; i < progB->Parameters->NumParameters; i++) {
798 struct gl_program_parameter *p = &progB->Parameters->Parameters[i];
799 if (p->Type == PROGRAM_STATE_VAR &&
800 p->StateIndexes[0] == STATE_INTERNAL &&
801 p->StateIndexes[1] == STATE_CURRENT_ATTRIB &&
Brian Paulf72e4b32010-10-25 09:10:32 -0600802 (int) p->StateIndexes[2] == (int) VERT_ATTRIB_COLOR0) {
Brian Paul5c4bd762008-10-08 14:02:24 -0600803 progB_inputsRead |= FRAG_BIT_COL0;
804 progB_colorFile = PROGRAM_STATE_VAR;
805 progB_colorIndex = i;
806 break;
807 }
808 }
809
Brian Paul0c78c762008-05-18 15:46:26 -0600810 /* Connect color outputs of fprogA to color inputs of fprogB, via a
811 * new temporary register.
812 */
Brian Paul11150e42011-03-15 09:48:26 -0600813 if ((progA->OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_COLOR)) &&
Brian Paul5c4bd762008-10-08 14:02:24 -0600814 (progB_inputsRead & FRAG_BIT_COL0)) {
Brian Paula2ddb3d2010-02-01 18:00:12 -0700815 GLint tempReg = _mesa_find_free_register(usedTemps, MAX_PROGRAM_TEMPS,
816 firstTemp);
Brian Paule469d782008-05-19 16:03:43 -0600817 if (tempReg < 0) {
Brian Paul0c78c762008-05-18 15:46:26 -0600818 _mesa_problem(ctx, "No free temp regs found in "
819 "_mesa_combine_programs(), using 31");
820 tempReg = 31;
821 }
Brian Paula2ddb3d2010-02-01 18:00:12 -0700822 firstTemp = tempReg + 1;
823
Brian Paul0c78c762008-05-18 15:46:26 -0600824 /* replace writes to result.color[0] with tempReg */
825 replace_registers(newInst, lenA,
Brian Paul8d475822009-02-28 11:49:46 -0700826 PROGRAM_OUTPUT, FRAG_RESULT_COLOR,
Brian Paul0c78c762008-05-18 15:46:26 -0600827 PROGRAM_TEMPORARY, tempReg);
Brian Paul5c4bd762008-10-08 14:02:24 -0600828 /* replace reads from the input color with tempReg */
Brian Paul6ca948a2008-05-14 12:53:03 -0600829 replace_registers(newInst + lenA, lenB,
Brian Paul5c4bd762008-10-08 14:02:24 -0600830 progB_colorFile, progB_colorIndex, /* search for */
831 PROGRAM_TEMPORARY, tempReg /* replace with */ );
Brian Paul6ca948a2008-05-14 12:53:03 -0600832 }
833
Brian Paul5c4bd762008-10-08 14:02:24 -0600834 /* compute combined program's InputsRead */
835 inputsB = progB_inputsRead;
Brian Paul11150e42011-03-15 09:48:26 -0600836 if (progA->OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_COLOR)) {
Brian Paul0c78c762008-05-18 15:46:26 -0600837 inputsB &= ~(1 << FRAG_ATTRIB_COL0);
838 }
839 newProg->InputsRead = progA->InputsRead | inputsB;
Brian Paul6ca948a2008-05-14 12:53:03 -0600840 newProg->OutputsWritten = progB->OutputsWritten;
Brian Paul0c78c762008-05-18 15:46:26 -0600841 newProg->SamplersUsed = progA->SamplersUsed | progB->SamplersUsed;
Brian Paul6ca948a2008-05-14 12:53:03 -0600842 }
843 else {
844 /* vertex program */
845 assert(0); /* XXX todo */
846 }
847
848 /*
849 * Merge parameters (uniforms, constants, etc)
850 */
851 newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters,
852 progB->Parameters);
853
854 adjust_param_indexes(newInst + lenA, lenB, numParamsA);
855
856
857 return newProg;
858}
859
860
Brian Paul6ca948a2008-05-14 12:53:03 -0600861/**
Brian Paula2ddb3d2010-02-01 18:00:12 -0700862 * Populate the 'used' array with flags indicating which registers (TEMPs,
863 * INPUTs, OUTPUTs, etc, are used by the given program.
864 * \param file type of register to scan for
865 * \param used returns true/false flags for in use / free
866 * \param usedSize size of the 'used' array
Brian Paul6ca948a2008-05-14 12:53:03 -0600867 */
Brian Paula2ddb3d2010-02-01 18:00:12 -0700868void
869_mesa_find_used_registers(const struct gl_program *prog,
870 gl_register_file file,
871 GLboolean used[], GLuint usedSize)
Brian Paul6ca948a2008-05-14 12:53:03 -0600872{
Brian Paula2ddb3d2010-02-01 18:00:12 -0700873 GLuint i, j;
Brian Paul6ca948a2008-05-14 12:53:03 -0600874
Kenneth Graunke26f8fad2010-02-18 23:51:00 -0800875 memset(used, 0, usedSize);
Brian Paul6ca948a2008-05-14 12:53:03 -0600876
877 for (i = 0; i < prog->NumInstructions; i++) {
878 const struct prog_instruction *inst = prog->Instructions + i;
879 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
880
Brian Paula2ddb3d2010-02-01 18:00:12 -0700881 if (inst->DstReg.File == file) {
Luca Barbieri029c1812010-09-05 17:57:12 +0200882 ASSERT(inst->DstReg.Index < usedSize);
883 if(inst->DstReg.Index < usedSize)
884 used[inst->DstReg.Index] = GL_TRUE;
Brian Paul5076a4f2009-12-11 09:16:25 -0700885 }
Brian Paula2ddb3d2010-02-01 18:00:12 -0700886
887 for (j = 0; j < n; j++) {
888 if (inst->SrcReg[j].File == file) {
Luca Barbieri029c1812010-09-05 17:57:12 +0200889 ASSERT(inst->SrcReg[j].Index < usedSize);
890 if(inst->SrcReg[j].Index < usedSize)
891 used[inst->SrcReg[j].Index] = GL_TRUE;
Brian Paul6ca948a2008-05-14 12:53:03 -0600892 }
893 }
894 }
Brian Paula2ddb3d2010-02-01 18:00:12 -0700895}
Brian Paul6ca948a2008-05-14 12:53:03 -0600896
Brian Paula2ddb3d2010-02-01 18:00:12 -0700897
898/**
899 * Scan the given 'used' register flag array for the first entry
900 * that's >= firstReg.
901 * \param used vector of flags indicating registers in use (as returned
902 * by _mesa_find_used_registers())
903 * \param usedSize size of the 'used' array
904 * \param firstReg first register to start searching at
905 * \return index of unused register, or -1 if none.
906 */
907GLint
908_mesa_find_free_register(const GLboolean used[],
909 GLuint usedSize, GLuint firstReg)
910{
911 GLuint i;
912
913 assert(firstReg < usedSize);
914
915 for (i = firstReg; i < usedSize; i++)
Brian Paul6ca948a2008-05-14 12:53:03 -0600916 if (!used[i])
917 return i;
Brian Paul6ca948a2008-05-14 12:53:03 -0600918
919 return -1;
920}
Brian Paulec6ad7b2009-06-17 07:42:49 -0600921
922
Brian Paul512f8402010-11-23 10:12:55 -0700923
924/**
925 * Check if the given register index is valid (doesn't exceed implementation-
926 * dependent limits).
927 * \return GL_TRUE if OK, GL_FALSE if bad index
928 */
929GLboolean
930_mesa_valid_register_index(const struct gl_context *ctx,
Brian Paulc628fd72010-11-23 10:28:43 -0700931 gl_shader_type shaderType,
Brian Paul512f8402010-11-23 10:12:55 -0700932 gl_register_file file, GLint index)
933{
934 const struct gl_program_constants *c;
935
936 switch (shaderType) {
937 case MESA_SHADER_VERTEX:
938 c = &ctx->Const.VertexProgram;
939 break;
940 case MESA_SHADER_FRAGMENT:
941 c = &ctx->Const.FragmentProgram;
942 break;
943 case MESA_SHADER_GEOMETRY:
944 c = &ctx->Const.GeometryProgram;
945 break;
946 default:
947 _mesa_problem(ctx,
948 "unexpected shader type in _mesa_valid_register_index()");
949 return GL_FALSE;
950 }
951
952 switch (file) {
953 case PROGRAM_UNDEFINED:
954 return GL_TRUE; /* XXX or maybe false? */
955
956 case PROGRAM_TEMPORARY:
957 return index >= 0 && index < c->MaxTemps;
958
959 case PROGRAM_ENV_PARAM:
960 return index >= 0 && index < c->MaxEnvParams;
961
962 case PROGRAM_LOCAL_PARAM:
963 return index >= 0 && index < c->MaxLocalParams;
964
965 case PROGRAM_NAMED_PARAM:
966 return index >= 0 && index < c->MaxParameters;
967
968 case PROGRAM_UNIFORM:
969 case PROGRAM_STATE_VAR:
970 /* aka constant buffer */
971 return index >= 0 && index < c->MaxUniformComponents / 4;
972
973 case PROGRAM_CONSTANT:
974 /* constant buffer w/ possible relative negative addressing */
975 return (index > (int) c->MaxUniformComponents / -4 &&
976 index < c->MaxUniformComponents / 4);
977
978 case PROGRAM_INPUT:
979 if (index < 0)
980 return GL_FALSE;
981
982 switch (shaderType) {
983 case MESA_SHADER_VERTEX:
984 return index < VERT_ATTRIB_GENERIC0 + c->MaxAttribs;
985 case MESA_SHADER_FRAGMENT:
986 return index < FRAG_ATTRIB_VAR0 + ctx->Const.MaxVarying;
987 case MESA_SHADER_GEOMETRY:
988 return index < GEOM_ATTRIB_VAR0 + ctx->Const.MaxVarying;
989 default:
990 return GL_FALSE;
991 }
992
993 case PROGRAM_OUTPUT:
994 if (index < 0)
995 return GL_FALSE;
996
997 switch (shaderType) {
998 case MESA_SHADER_VERTEX:
999 return index < VERT_RESULT_VAR0 + ctx->Const.MaxVarying;
1000 case MESA_SHADER_FRAGMENT:
1001 return index < FRAG_RESULT_DATA0 + ctx->Const.MaxDrawBuffers;
1002 case MESA_SHADER_GEOMETRY:
1003 return index < GEOM_RESULT_VAR0 + ctx->Const.MaxVarying;
1004 default:
1005 return GL_FALSE;
1006 }
1007
1008 case PROGRAM_ADDRESS:
1009 return index >= 0 && index < c->MaxAddressRegs;
1010
1011 default:
1012 _mesa_problem(ctx,
1013 "unexpected register file in _mesa_valid_register_index()");
1014 return GL_FALSE;
1015 }
1016}
1017
1018
1019
Brian Paulec6ad7b2009-06-17 07:42:49 -06001020/**
1021 * "Post-process" a GPU program. This is intended to be used for debugging.
1022 * Example actions include no-op'ing instructions or changing instruction
1023 * behaviour.
1024 */
1025void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -04001026_mesa_postprocess_program(struct gl_context *ctx, struct gl_program *prog)
Brian Paulec6ad7b2009-06-17 07:42:49 -06001027{
1028 static const GLfloat white[4] = { 0.5, 0.5, 0.5, 0.5 };
1029 GLuint i;
1030 GLuint whiteSwizzle;
1031 GLint whiteIndex = _mesa_add_unnamed_constant(prog->Parameters,
1032 white, 4, &whiteSwizzle);
1033
Brian Paul516d20f2009-06-17 07:43:44 -06001034 (void) whiteIndex;
1035
Brian Paulec6ad7b2009-06-17 07:42:49 -06001036 for (i = 0; i < prog->NumInstructions; i++) {
1037 struct prog_instruction *inst = prog->Instructions + i;
1038 const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
1039
1040 (void) n;
1041
1042 if (_mesa_is_tex_instruction(inst->Opcode)) {
1043#if 0
1044 /* replace TEX/TXP/TXB with MOV */
1045 inst->Opcode = OPCODE_MOV;
1046 inst->DstReg.WriteMask = WRITEMASK_XYZW;
1047 inst->SrcReg[0].Swizzle = SWIZZLE_XYZW;
1048 inst->SrcReg[0].Negate = NEGATE_NONE;
1049#endif
1050
1051#if 0
1052 /* disable shadow texture mode */
1053 inst->TexShadow = 0;
1054#endif
1055 }
1056
1057 if (inst->Opcode == OPCODE_TXP) {
1058#if 0
1059 inst->Opcode = OPCODE_MOV;
1060 inst->DstReg.WriteMask = WRITEMASK_XYZW;
1061 inst->SrcReg[0].File = PROGRAM_CONSTANT;
1062 inst->SrcReg[0].Index = whiteIndex;
1063 inst->SrcReg[0].Swizzle = SWIZZLE_XYZW;
1064 inst->SrcReg[0].Negate = NEGATE_NONE;
1065#endif
1066#if 0
1067 inst->TexShadow = 0;
1068#endif
1069#if 0
1070 inst->Opcode = OPCODE_TEX;
1071 inst->TexShadow = 0;
1072#endif
1073 }
1074
1075 }
1076}