2004-03-29 11:09:34 +00:00
|
|
|
/*
|
|
|
|
* Mesa 3-D graphics library
|
|
|
|
*
|
2007-02-09 15:40:00 -07:00
|
|
|
* Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
|
2004-03-29 11:09:34 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
2013-04-21 13:46:48 -07:00
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
|
|
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
2004-03-29 11:09:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file program.c
|
|
|
|
* Vertex and fragment program support functions.
|
|
|
|
* \author Brian Paul
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2008-07-23 21:06:01 +09:00
|
|
|
#include "main/glheader.h"
|
|
|
|
#include "main/context.h"
|
|
|
|
#include "main/hash.h"
|
2013-10-16 17:22:18 -07:00
|
|
|
#include "main/macros.h"
|
2006-12-14 15:01:28 -07:00
|
|
|
#include "program.h"
|
2007-10-31 12:00:38 -06:00
|
|
|
#include "prog_cache.h"
|
2006-12-14 15:01:28 -07:00
|
|
|
#include "prog_parameter.h"
|
|
|
|
#include "prog_instruction.h"
|
2015-04-07 15:15:09 -07:00
|
|
|
#include "util/ralloc.h"
|
2004-03-29 11:09:34 +00:00
|
|
|
|
|
|
|
|
2007-02-09 15:40:00 -07:00
|
|
|
/**
|
|
|
|
* A pointer to this dummy program is put into the hash table when
|
2004-09-14 22:28:27 +00:00
|
|
|
* glGenPrograms is called.
|
|
|
|
*/
|
2006-07-20 16:49:57 +00:00
|
|
|
struct gl_program _mesa_DummyProgram;
|
2004-09-14 22:28:27 +00:00
|
|
|
|
|
|
|
|
2004-03-29 11:09:34 +00:00
|
|
|
/**
|
2004-08-14 14:28:11 +00:00
|
|
|
* Init context's vertex/fragment program state
|
2004-03-29 11:09:34 +00:00
|
|
|
*/
|
|
|
|
void
|
2010-10-12 12:26:10 -04:00
|
|
|
_mesa_init_program(struct gl_context *ctx)
|
2004-03-29 11:09:34 +00:00
|
|
|
{
|
2009-02-18 11:47:40 -07:00
|
|
|
/*
|
|
|
|
* If this assertion fails, we need to increase the field
|
2010-08-10 08:38:12 -06:00
|
|
|
* size for register indexes (see INST_INDEX_BITS).
|
2009-02-18 11:47:40 -07:00
|
|
|
*/
|
2015-02-20 20:18:47 -08:00
|
|
|
assert(ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents / 4
|
2009-02-18 11:47:40 -07:00
|
|
|
<= (1 << INST_INDEX_BITS));
|
2015-02-20 20:18:47 -08:00
|
|
|
assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents / 4
|
2009-02-18 11:47:40 -07:00
|
|
|
<= (1 << INST_INDEX_BITS));
|
|
|
|
|
2015-02-20 20:18:47 -08:00
|
|
|
assert(ctx->Const.Program[MESA_SHADER_VERTEX].MaxTemps <= (1 << INST_INDEX_BITS));
|
|
|
|
assert(ctx->Const.Program[MESA_SHADER_VERTEX].MaxLocalParams <= (1 << INST_INDEX_BITS));
|
|
|
|
assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTemps <= (1 << INST_INDEX_BITS));
|
|
|
|
assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxLocalParams <= (1 << INST_INDEX_BITS));
|
2010-08-10 08:38:12 -06:00
|
|
|
|
2015-02-20 20:18:47 -08:00
|
|
|
assert(ctx->Const.Program[MESA_SHADER_VERTEX].MaxUniformComponents <= 4 * MAX_UNIFORMS);
|
|
|
|
assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxUniformComponents <= 4 * MAX_UNIFORMS);
|
2010-08-10 08:38:12 -06:00
|
|
|
|
2015-02-20 20:18:47 -08:00
|
|
|
assert(ctx->Const.Program[MESA_SHADER_VERTEX].MaxAddressOffset <= (1 << INST_INDEX_BITS));
|
|
|
|
assert(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxAddressOffset <= (1 << INST_INDEX_BITS));
|
2011-03-02 09:32:45 -07:00
|
|
|
|
2009-08-26 10:57:18 -06:00
|
|
|
/* If this fails, increase prog_instruction::TexSrcUnit size */
|
2013-09-19 20:06:35 -07:00
|
|
|
STATIC_ASSERT(MAX_TEXTURE_UNITS <= (1 << 5));
|
2009-08-26 10:57:18 -06:00
|
|
|
|
|
|
|
/* If this fails, increase prog_instruction::TexSrcTarget size */
|
2013-09-19 20:06:35 -07:00
|
|
|
STATIC_ASSERT(NUM_TEXTURE_TARGETS <= (1 << 4));
|
2009-08-26 10:57:18 -06:00
|
|
|
|
2004-03-29 11:09:34 +00:00
|
|
|
ctx->Program.ErrorPos = -1;
|
2015-03-07 13:15:22 -07:00
|
|
|
ctx->Program.ErrorString = strdup("");
|
2004-03-29 11:09:34 +00:00
|
|
|
|
|
|
|
ctx->VertexProgram.Enabled = GL_FALSE;
|
2010-05-24 10:01:38 -04:00
|
|
|
ctx->VertexProgram.PointSizeEnabled =
|
|
|
|
(ctx->API == API_OPENGLES2) ? GL_TRUE : GL_FALSE;
|
2004-03-29 11:09:34 +00:00
|
|
|
ctx->VertexProgram.TwoSideEnabled = GL_FALSE;
|
2008-05-06 23:08:51 -06:00
|
|
|
_mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
|
|
|
|
ctx->Shared->DefaultVertexProgram);
|
2004-03-29 11:09:34 +00:00
|
|
|
assert(ctx->VertexProgram.Current);
|
2007-10-31 12:00:38 -06:00
|
|
|
ctx->VertexProgram.Cache = _mesa_new_program_cache();
|
2004-03-29 11:09:34 +00:00
|
|
|
|
|
|
|
ctx->FragmentProgram.Enabled = GL_FALSE;
|
2008-05-06 23:08:51 -06:00
|
|
|
_mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
|
|
|
|
ctx->Shared->DefaultFragmentProgram);
|
2004-03-29 11:09:34 +00:00
|
|
|
assert(ctx->FragmentProgram.Current);
|
2007-10-31 12:00:38 -06:00
|
|
|
ctx->FragmentProgram.Cache = _mesa_new_program_cache();
|
2004-12-19 03:06:59 +00:00
|
|
|
|
2005-11-19 16:43:04 +00:00
|
|
|
/* XXX probably move this stuff */
|
2004-12-19 03:06:59 +00:00
|
|
|
ctx->ATIFragmentShader.Enabled = GL_FALSE;
|
2008-07-29 17:19:25 -06:00
|
|
|
ctx->ATIFragmentShader.Current = ctx->Shared->DefaultFragmentShader;
|
2004-12-19 03:06:59 +00:00
|
|
|
assert(ctx->ATIFragmentShader.Current);
|
2005-11-19 16:43:04 +00:00
|
|
|
ctx->ATIFragmentShader.Current->RefCount++;
|
2004-03-29 11:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-14 14:28:11 +00:00
|
|
|
/**
|
|
|
|
* Free a context's vertex/fragment program state
|
|
|
|
*/
|
|
|
|
void
|
2010-10-12 12:26:10 -04:00
|
|
|
_mesa_free_program_data(struct gl_context *ctx)
|
2004-08-14 14:28:11 +00:00
|
|
|
{
|
2008-05-06 23:08:51 -06:00
|
|
|
_mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current, NULL);
|
2007-12-26 06:57:24 -07:00
|
|
|
_mesa_delete_program_cache(ctx, ctx->VertexProgram.Cache);
|
2008-05-06 23:08:51 -06:00
|
|
|
_mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, NULL);
|
2010-11-01 13:16:21 -07:00
|
|
|
_mesa_delete_shader_cache(ctx, ctx->FragmentProgram.Cache);
|
2012-09-11 11:15:14 +03:00
|
|
|
|
2005-11-19 16:43:04 +00:00
|
|
|
/* XXX probably move this stuff */
|
2004-12-19 03:06:59 +00:00
|
|
|
if (ctx->ATIFragmentShader.Current) {
|
2005-11-19 16:43:04 +00:00
|
|
|
ctx->ATIFragmentShader.Current->RefCount--;
|
|
|
|
if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
|
2010-02-19 11:58:49 -05:00
|
|
|
free(ctx->ATIFragmentShader.Current);
|
2005-11-19 16:43:04 +00:00
|
|
|
}
|
2004-12-19 03:06:59 +00:00
|
|
|
}
|
2012-09-11 11:15:14 +03:00
|
|
|
|
2010-02-19 11:58:49 -05:00
|
|
|
free((void *) ctx->Program.ErrorString);
|
2004-08-14 14:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-23 08:53:43 +01:00
|
|
|
/**
|
|
|
|
* Update the default program objects in the given context to reference those
|
2008-07-06 19:48:50 +02:00
|
|
|
* specified in the shared state and release those referencing the old
|
2007-08-23 08:53:43 +01:00
|
|
|
* shared state.
|
|
|
|
*/
|
|
|
|
void
|
2010-10-12 12:26:10 -04:00
|
|
|
_mesa_update_default_objects_program(struct gl_context *ctx)
|
2007-08-23 08:53:43 +01:00
|
|
|
{
|
2008-05-14 12:10:45 -06:00
|
|
|
_mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
|
|
|
|
ctx->Shared->DefaultVertexProgram);
|
2007-08-23 08:53:43 +01:00
|
|
|
assert(ctx->VertexProgram.Current);
|
|
|
|
|
2008-05-14 12:10:45 -06:00
|
|
|
_mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
|
|
|
|
ctx->Shared->DefaultFragmentProgram);
|
2007-08-23 08:53:43 +01:00
|
|
|
assert(ctx->FragmentProgram.Current);
|
|
|
|
|
|
|
|
/* XXX probably move this stuff */
|
|
|
|
if (ctx->ATIFragmentShader.Current) {
|
|
|
|
ctx->ATIFragmentShader.Current->RefCount--;
|
|
|
|
if (ctx->ATIFragmentShader.Current->RefCount <= 0) {
|
2010-02-19 11:58:49 -05:00
|
|
|
free(ctx->ATIFragmentShader.Current);
|
2007-08-23 08:53:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx->ATIFragmentShader.Current = (struct ati_fragment_shader *) ctx->Shared->DefaultFragmentShader;
|
|
|
|
assert(ctx->ATIFragmentShader.Current);
|
|
|
|
ctx->ATIFragmentShader.Current->RefCount++;
|
|
|
|
}
|
2004-08-14 14:28:11 +00:00
|
|
|
|
|
|
|
|
2004-03-29 11:09:34 +00:00
|
|
|
/**
|
|
|
|
* Set the vertex/fragment program error state (position and error string).
|
|
|
|
* This is generally called from within the parsers.
|
|
|
|
*/
|
|
|
|
void
|
2010-10-12 12:26:10 -04:00
|
|
|
_mesa_set_program_error(struct gl_context *ctx, GLint pos, const char *string)
|
2004-03-29 11:09:34 +00:00
|
|
|
{
|
|
|
|
ctx->Program.ErrorPos = pos;
|
2010-02-19 11:58:49 -05:00
|
|
|
free((void *) ctx->Program.ErrorString);
|
2004-03-29 11:09:34 +00:00
|
|
|
if (!string)
|
|
|
|
string = "";
|
2015-03-07 13:15:22 -07:00
|
|
|
ctx->Program.ErrorString = strdup(string);
|
2004-03-29 11:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the line number and column for 'pos' within 'string'.
|
|
|
|
* Return a copy of the line which contains 'pos'. Free the line with
|
2010-02-19 11:58:49 -05:00
|
|
|
* free().
|
2004-03-29 11:09:34 +00:00
|
|
|
* \param string the program string
|
|
|
|
* \param pos the position within the string
|
|
|
|
* \param line returns the line number corresponding to 'pos'.
|
|
|
|
* \param col returns the column number corresponding to 'pos'.
|
|
|
|
* \return copy of the line containing 'pos'.
|
|
|
|
*/
|
|
|
|
const GLubyte *
|
|
|
|
_mesa_find_line_column(const GLubyte *string, const GLubyte *pos,
|
|
|
|
GLint *line, GLint *col)
|
|
|
|
{
|
|
|
|
const GLubyte *lineStart = string;
|
|
|
|
const GLubyte *p = string;
|
|
|
|
GLubyte *s;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
*line = 1;
|
|
|
|
|
|
|
|
while (p != pos) {
|
|
|
|
if (*p == (GLubyte) '\n') {
|
|
|
|
(*line)++;
|
|
|
|
lineStart = p + 1;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*col = (pos - lineStart) + 1;
|
|
|
|
|
|
|
|
/* return copy of this line */
|
|
|
|
while (*p != 0 && *p != '\n')
|
|
|
|
p++;
|
|
|
|
len = p - lineStart;
|
2012-09-03 19:44:00 -07:00
|
|
|
s = malloc(len + 1);
|
2010-02-18 23:50:59 -08:00
|
|
|
memcpy(s, lineStart, len);
|
2004-03-29 11:09:34 +00:00
|
|
|
s[len] = 0;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-14 22:28:27 +00:00
|
|
|
/**
|
2014-08-08 13:19:49 -06:00
|
|
|
* Initialize a new gl_program object.
|
2004-09-14 22:28:27 +00:00
|
|
|
*/
|
2014-08-08 13:19:49 -06:00
|
|
|
static void
|
|
|
|
init_program_struct(struct gl_program *prog, GLenum target, GLuint id)
|
2004-03-29 11:09:34 +00:00
|
|
|
{
|
2014-08-08 13:19:49 -06:00
|
|
|
GLuint i;
|
|
|
|
|
|
|
|
assert(prog);
|
2004-03-29 11:09:34 +00:00
|
|
|
|
2014-08-08 13:19:49 -06:00
|
|
|
memset(prog, 0, sizeof(*prog));
|
|
|
|
prog->Id = id;
|
|
|
|
prog->Target = target;
|
|
|
|
prog->RefCount = 1;
|
|
|
|
prog->Format = GL_PROGRAM_FORMAT_ASCII_ARB;
|
|
|
|
|
|
|
|
/* default mapping from samplers to texture units */
|
|
|
|
for (i = 0; i < MAX_SAMPLERS; i++)
|
|
|
|
prog->SamplerUnits[i] = i;
|
2004-03-29 11:09:34 +00:00
|
|
|
}
|
|
|
|
|
2004-09-14 22:28:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize a new fragment program object.
|
|
|
|
*/
|
2006-07-20 16:49:57 +00:00
|
|
|
struct gl_program *
|
2014-08-08 13:22:28 -06:00
|
|
|
_mesa_init_fragment_program(struct gl_context *ctx,
|
|
|
|
struct gl_fragment_program *prog,
|
|
|
|
GLenum target, GLuint id)
|
2004-03-29 11:09:34 +00:00
|
|
|
{
|
2014-08-08 13:19:49 -06:00
|
|
|
if (prog) {
|
|
|
|
init_program_struct(&prog->Base, target, id);
|
|
|
|
return &prog->Base;
|
|
|
|
}
|
|
|
|
return NULL;
|
2004-03-29 11:09:34 +00:00
|
|
|
}
|
|
|
|
|
2004-09-14 22:28:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize a new vertex program object.
|
|
|
|
*/
|
2006-07-20 16:49:57 +00:00
|
|
|
struct gl_program *
|
2014-08-08 13:22:28 -06:00
|
|
|
_mesa_init_vertex_program(struct gl_context *ctx,
|
|
|
|
struct gl_vertex_program *prog,
|
|
|
|
GLenum target, GLuint id)
|
2004-03-29 11:09:34 +00:00
|
|
|
{
|
2014-08-08 13:19:49 -06:00
|
|
|
if (prog) {
|
|
|
|
init_program_struct(&prog->Base, target, id);
|
|
|
|
return &prog->Base;
|
|
|
|
}
|
|
|
|
return NULL;
|
2004-03-29 11:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-07 15:50:39 -08:00
|
|
|
/**
|
|
|
|
* Initialize a new compute program object.
|
|
|
|
*/
|
|
|
|
struct gl_program *
|
|
|
|
_mesa_init_compute_program(struct gl_context *ctx,
|
2014-08-08 13:22:28 -06:00
|
|
|
struct gl_compute_program *prog,
|
|
|
|
GLenum target, GLuint id)
|
2014-01-07 15:50:39 -08:00
|
|
|
{
|
2014-08-08 13:19:49 -06:00
|
|
|
if (prog) {
|
|
|
|
init_program_struct(&prog->Base, target, id);
|
|
|
|
return &prog->Base;
|
|
|
|
}
|
|
|
|
return NULL;
|
2014-01-07 15:50:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-07 10:28:03 +01:00
|
|
|
/**
|
|
|
|
* Initialize a new tessellation control program object.
|
|
|
|
*/
|
|
|
|
struct gl_program *
|
|
|
|
_mesa_init_tess_ctrl_program(struct gl_context *ctx,
|
|
|
|
struct gl_tess_ctrl_program *prog,
|
|
|
|
GLenum target, GLuint id)
|
|
|
|
{
|
|
|
|
if (prog) {
|
|
|
|
init_program_struct(&prog->Base, target, id);
|
|
|
|
return &prog->Base;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize a new tessellation evaluation program object.
|
|
|
|
*/
|
|
|
|
struct gl_program *
|
|
|
|
_mesa_init_tess_eval_program(struct gl_context *ctx,
|
|
|
|
struct gl_tess_eval_program *prog,
|
|
|
|
GLenum target, GLuint id)
|
|
|
|
{
|
|
|
|
if (prog) {
|
|
|
|
init_program_struct(&prog->Base, target, id);
|
|
|
|
return &prog->Base;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-28 17:31:21 -04:00
|
|
|
/**
|
|
|
|
* Initialize a new geometry program object.
|
|
|
|
*/
|
|
|
|
struct gl_program *
|
2014-08-08 13:22:28 -06:00
|
|
|
_mesa_init_geometry_program(struct gl_context *ctx,
|
|
|
|
struct gl_geometry_program *prog,
|
|
|
|
GLenum target, GLuint id)
|
2010-06-28 17:31:21 -04:00
|
|
|
{
|
2014-08-08 13:19:49 -06:00
|
|
|
if (prog) {
|
|
|
|
init_program_struct(&prog->Base, target, id);
|
|
|
|
return &prog->Base;
|
|
|
|
}
|
|
|
|
return NULL;
|
2010-06-28 17:31:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-29 11:09:34 +00:00
|
|
|
/**
|
|
|
|
* Allocate and initialize a new fragment/vertex program object but
|
|
|
|
* don't put it into the program hash table. Called via
|
|
|
|
* ctx->Driver.NewProgram. May be overridden (ie. replaced) by a
|
|
|
|
* device driver function to implement OO deriviation with additional
|
|
|
|
* types not understood by this function.
|
2008-07-06 19:48:50 +02:00
|
|
|
*
|
2004-03-29 11:09:34 +00:00
|
|
|
* \param ctx context
|
|
|
|
* \param id program id/number
|
|
|
|
* \param target program target/type
|
|
|
|
* \return pointer to new program object
|
|
|
|
*/
|
2006-07-20 16:49:57 +00:00
|
|
|
struct gl_program *
|
2010-10-12 12:26:10 -04:00
|
|
|
_mesa_new_program(struct gl_context *ctx, GLenum target, GLuint id)
|
2004-03-29 11:09:34 +00:00
|
|
|
{
|
2008-05-16 09:56:11 -06:00
|
|
|
struct gl_program *prog;
|
2004-03-29 11:09:34 +00:00
|
|
|
switch (target) {
|
|
|
|
case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
|
2008-05-16 09:56:11 -06:00
|
|
|
prog = _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program),
|
2006-07-20 16:49:57 +00:00
|
|
|
target, id );
|
2008-05-16 09:56:11 -06:00
|
|
|
break;
|
2004-03-29 11:09:34 +00:00
|
|
|
case GL_FRAGMENT_PROGRAM_NV:
|
|
|
|
case GL_FRAGMENT_PROGRAM_ARB:
|
2008-05-16 09:56:11 -06:00
|
|
|
prog =_mesa_init_fragment_program(ctx,
|
2006-07-20 16:49:57 +00:00
|
|
|
CALLOC_STRUCT(gl_fragment_program),
|
|
|
|
target, id );
|
2008-05-16 09:56:11 -06:00
|
|
|
break;
|
2015-05-28 18:14:29 +02:00
|
|
|
case GL_GEOMETRY_PROGRAM_NV:
|
2010-06-28 17:31:21 -04:00
|
|
|
prog = _mesa_init_geometry_program(ctx,
|
|
|
|
CALLOC_STRUCT(gl_geometry_program),
|
|
|
|
target, id);
|
|
|
|
break;
|
2014-03-10 11:58:37 +01:00
|
|
|
case GL_TESS_CONTROL_PROGRAM_NV:
|
|
|
|
prog = _mesa_init_tess_ctrl_program(ctx,
|
|
|
|
CALLOC_STRUCT(gl_tess_ctrl_program),
|
|
|
|
target, id);
|
|
|
|
break;
|
|
|
|
case GL_TESS_EVALUATION_PROGRAM_NV:
|
|
|
|
prog = _mesa_init_tess_eval_program(ctx,
|
|
|
|
CALLOC_STRUCT(gl_tess_eval_program),
|
|
|
|
target, id);
|
|
|
|
break;
|
2014-01-07 15:50:39 -08:00
|
|
|
case GL_COMPUTE_PROGRAM_NV:
|
|
|
|
prog = _mesa_init_compute_program(ctx,
|
|
|
|
CALLOC_STRUCT(gl_compute_program),
|
|
|
|
target, id);
|
|
|
|
break;
|
2004-03-29 11:09:34 +00:00
|
|
|
default:
|
|
|
|
_mesa_problem(ctx, "bad target in _mesa_new_program");
|
2008-05-16 09:56:11 -06:00
|
|
|
prog = NULL;
|
2004-03-29 11:09:34 +00:00
|
|
|
}
|
2008-05-16 09:56:11 -06:00
|
|
|
return prog;
|
2004-03-29 11:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a program and remove it from the hash table, ignoring the
|
|
|
|
* reference count.
|
|
|
|
* Called via ctx->Driver.DeleteProgram. May be wrapped (OO deriviation)
|
|
|
|
* by a device driver function.
|
|
|
|
*/
|
|
|
|
void
|
2010-10-12 12:26:10 -04:00
|
|
|
_mesa_delete_program(struct gl_context *ctx, struct gl_program *prog)
|
2004-03-29 11:09:34 +00:00
|
|
|
{
|
2004-08-25 15:59:48 +00:00
|
|
|
(void) ctx;
|
2015-02-20 20:18:47 -08:00
|
|
|
assert(prog);
|
|
|
|
assert(prog->RefCount==0);
|
2004-03-29 11:09:34 +00:00
|
|
|
|
2006-11-23 15:58:30 +00:00
|
|
|
if (prog == &_mesa_DummyProgram)
|
|
|
|
return;
|
2008-07-06 19:48:50 +02:00
|
|
|
|
2012-09-03 20:24:35 -07:00
|
|
|
free(prog->String);
|
2013-09-20 10:13:32 -07:00
|
|
|
free(prog->LocalParams);
|
2005-11-12 17:53:14 +00:00
|
|
|
|
2011-04-29 19:00:24 -05:00
|
|
|
if (prog->Instructions) {
|
|
|
|
_mesa_free_instructions(prog->Instructions, prog->NumInstructions);
|
|
|
|
}
|
2006-05-24 03:30:31 +00:00
|
|
|
if (prog->Parameters) {
|
2005-11-12 17:53:14 +00:00
|
|
|
_mesa_free_parameter_list(prog->Parameters);
|
2006-05-24 03:30:31 +00:00
|
|
|
}
|
2006-12-13 14:54:47 -07:00
|
|
|
|
2015-04-07 15:15:09 -07:00
|
|
|
if (prog->nir) {
|
|
|
|
ralloc_free(prog->nir);
|
|
|
|
}
|
|
|
|
|
2010-02-19 11:58:49 -05:00
|
|
|
free(prog);
|
2004-03-29 11:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-23 23:10:14 +00:00
|
|
|
/**
|
|
|
|
* Return the gl_program object for a given ID.
|
|
|
|
* Basically just a wrapper for _mesa_HashLookup() to avoid a lot of
|
|
|
|
* casts elsewhere.
|
|
|
|
*/
|
|
|
|
struct gl_program *
|
2010-10-12 12:26:10 -04:00
|
|
|
_mesa_lookup_program(struct gl_context *ctx, GLuint id)
|
2006-08-23 23:10:14 +00:00
|
|
|
{
|
|
|
|
if (id)
|
|
|
|
return (struct gl_program *) _mesa_HashLookup(ctx->Shared->Programs, id);
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-03-29 11:09:34 +00:00
|
|
|
|
2008-05-06 23:08:51 -06:00
|
|
|
/**
|
|
|
|
* Reference counting for vertex/fragment programs
|
2011-07-14 08:09:21 -06:00
|
|
|
* This is normally only called from the _mesa_reference_program() macro
|
|
|
|
* when there's a real pointer change.
|
2008-05-06 23:08:51 -06:00
|
|
|
*/
|
|
|
|
void
|
2011-07-14 08:09:21 -06:00
|
|
|
_mesa_reference_program_(struct gl_context *ctx,
|
|
|
|
struct gl_program **ptr,
|
|
|
|
struct gl_program *prog)
|
2008-05-06 23:08:51 -06:00
|
|
|
{
|
2011-07-14 08:09:21 -06:00
|
|
|
#ifndef NDEBUG
|
2008-05-06 23:08:51 -06:00
|
|
|
assert(ptr);
|
|
|
|
if (*ptr && prog) {
|
|
|
|
/* sanity check */
|
2008-09-26 07:40:45 -06:00
|
|
|
if ((*ptr)->Target == GL_VERTEX_PROGRAM_ARB)
|
2015-02-20 20:18:47 -08:00
|
|
|
assert(prog->Target == GL_VERTEX_PROGRAM_ARB);
|
2008-09-26 07:40:45 -06:00
|
|
|
else if ((*ptr)->Target == GL_FRAGMENT_PROGRAM_ARB)
|
2015-02-20 20:18:47 -08:00
|
|
|
assert(prog->Target == GL_FRAGMENT_PROGRAM_ARB ||
|
2008-09-26 07:40:45 -06:00
|
|
|
prog->Target == GL_FRAGMENT_PROGRAM_NV);
|
2015-05-28 18:14:29 +02:00
|
|
|
else if ((*ptr)->Target == GL_GEOMETRY_PROGRAM_NV)
|
|
|
|
assert(prog->Target == GL_GEOMETRY_PROGRAM_NV);
|
2008-05-06 23:08:51 -06:00
|
|
|
}
|
2011-07-14 08:09:21 -06:00
|
|
|
#endif
|
|
|
|
|
2008-05-06 23:08:51 -06:00
|
|
|
if (*ptr) {
|
|
|
|
GLboolean deleteFlag;
|
|
|
|
|
2014-03-01 10:21:07 -07:00
|
|
|
/*mtx_lock(&(*ptr)->Mutex);*/
|
2015-02-20 20:18:47 -08:00
|
|
|
assert((*ptr)->RefCount > 0);
|
2008-05-06 23:08:51 -06:00
|
|
|
(*ptr)->RefCount--;
|
|
|
|
|
|
|
|
deleteFlag = ((*ptr)->RefCount == 0);
|
2014-03-01 10:21:07 -07:00
|
|
|
/*mtx_lock(&(*ptr)->Mutex);*/
|
2008-07-06 19:48:50 +02:00
|
|
|
|
2008-05-06 23:08:51 -06:00
|
|
|
if (deleteFlag) {
|
2015-02-20 20:18:47 -08:00
|
|
|
assert(ctx);
|
2008-05-06 23:08:51 -06:00
|
|
|
ctx->Driver.DeleteProgram(ctx, *ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
*ptr = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!*ptr);
|
|
|
|
if (prog) {
|
2014-03-01 10:21:07 -07:00
|
|
|
/*mtx_lock(&prog->Mutex);*/
|
2008-05-06 23:08:51 -06:00
|
|
|
prog->RefCount++;
|
2014-03-01 10:21:07 -07:00
|
|
|
/*mtx_unlock(&prog->Mutex);*/
|
2008-05-06 23:08:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
*ptr = prog;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-14 13:56:58 -07:00
|
|
|
/**
|
|
|
|
* Return a copy of a program.
|
|
|
|
* XXX Problem here if the program object is actually OO-derivation
|
|
|
|
* made by a device driver.
|
|
|
|
*/
|
|
|
|
struct gl_program *
|
2010-10-12 12:26:10 -04:00
|
|
|
_mesa_clone_program(struct gl_context *ctx, const struct gl_program *prog)
|
2006-12-14 13:56:58 -07:00
|
|
|
{
|
|
|
|
struct gl_program *clone;
|
|
|
|
|
2007-07-24 09:56:44 -06:00
|
|
|
clone = ctx->Driver.NewProgram(ctx, prog->Target, prog->Id);
|
2006-12-14 13:56:58 -07:00
|
|
|
if (!clone)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
assert(clone->Target == prog->Target);
|
2008-05-06 23:08:51 -06:00
|
|
|
assert(clone->RefCount == 1);
|
|
|
|
|
2015-03-07 13:15:22 -07:00
|
|
|
clone->String = (GLubyte *) strdup((char *) prog->String);
|
2006-12-14 13:56:58 -07:00
|
|
|
clone->Format = prog->Format;
|
|
|
|
clone->Instructions = _mesa_alloc_instructions(prog->NumInstructions);
|
|
|
|
if (!clone->Instructions) {
|
2008-05-14 12:10:45 -06:00
|
|
|
_mesa_reference_program(ctx, &clone, NULL);
|
2006-12-14 13:56:58 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
2007-03-22 09:11:26 -06:00
|
|
|
_mesa_copy_instructions(clone->Instructions, prog->Instructions,
|
|
|
|
prog->NumInstructions);
|
2006-12-14 13:56:58 -07:00
|
|
|
clone->InputsRead = prog->InputsRead;
|
|
|
|
clone->OutputsWritten = prog->OutputsWritten;
|
2008-05-18 15:46:26 -06:00
|
|
|
clone->SamplersUsed = prog->SamplersUsed;
|
2008-07-05 18:03:44 +02:00
|
|
|
clone->ShadowSamplers = prog->ShadowSamplers;
|
2007-01-04 17:22:19 -07:00
|
|
|
memcpy(clone->TexturesUsed, prog->TexturesUsed, sizeof(prog->TexturesUsed));
|
|
|
|
|
2006-12-19 09:52:07 -07:00
|
|
|
if (prog->Parameters)
|
|
|
|
clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
|
2013-09-20 10:13:32 -07:00
|
|
|
if (prog->LocalParams) {
|
|
|
|
clone->LocalParams = malloc(MAX_PROGRAM_LOCAL_PARAMS *
|
|
|
|
sizeof(float[4]));
|
|
|
|
if (!clone->LocalParams) {
|
|
|
|
_mesa_reference_program(ctx, &clone, NULL);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy(clone->LocalParams, prog->LocalParams,
|
|
|
|
MAX_PROGRAM_LOCAL_PARAMS * sizeof(float[4]));
|
|
|
|
}
|
2010-07-30 14:24:23 -06:00
|
|
|
clone->IndirectRegisterFiles = prog->IndirectRegisterFiles;
|
2006-12-14 13:56:58 -07:00
|
|
|
clone->NumInstructions = prog->NumInstructions;
|
|
|
|
clone->NumTemporaries = prog->NumTemporaries;
|
|
|
|
clone->NumParameters = prog->NumParameters;
|
|
|
|
clone->NumAttributes = prog->NumAttributes;
|
|
|
|
clone->NumAddressRegs = prog->NumAddressRegs;
|
|
|
|
clone->NumNativeInstructions = prog->NumNativeInstructions;
|
|
|
|
clone->NumNativeTemporaries = prog->NumNativeTemporaries;
|
|
|
|
clone->NumNativeParameters = prog->NumNativeParameters;
|
|
|
|
clone->NumNativeAttributes = prog->NumNativeAttributes;
|
|
|
|
clone->NumNativeAddressRegs = prog->NumNativeAddressRegs;
|
2007-01-09 11:00:21 -07:00
|
|
|
clone->NumAluInstructions = prog->NumAluInstructions;
|
|
|
|
clone->NumTexInstructions = prog->NumTexInstructions;
|
|
|
|
clone->NumTexIndirections = prog->NumTexIndirections;
|
|
|
|
clone->NumNativeAluInstructions = prog->NumNativeAluInstructions;
|
|
|
|
clone->NumNativeTexInstructions = prog->NumNativeTexInstructions;
|
|
|
|
clone->NumNativeTexIndirections = prog->NumNativeTexIndirections;
|
2006-12-14 13:56:58 -07:00
|
|
|
|
|
|
|
switch (prog->Target) {
|
|
|
|
case GL_VERTEX_PROGRAM_ARB:
|
|
|
|
{
|
2012-07-18 15:33:11 -06:00
|
|
|
const struct gl_vertex_program *vp = gl_vertex_program_const(prog);
|
|
|
|
struct gl_vertex_program *vpc = gl_vertex_program(clone);
|
2006-12-14 13:56:58 -07:00
|
|
|
vpc->IsPositionInvariant = vp->IsPositionInvariant;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GL_FRAGMENT_PROGRAM_ARB:
|
|
|
|
{
|
2012-07-18 15:33:11 -06:00
|
|
|
const struct gl_fragment_program *fp = gl_fragment_program_const(prog);
|
|
|
|
struct gl_fragment_program *fpc = gl_fragment_program(clone);
|
2006-12-14 13:56:58 -07:00
|
|
|
fpc->UsesKill = fp->UsesKill;
|
2012-06-20 12:31:46 -07:00
|
|
|
fpc->UsesDFdy = fp->UsesDFdy;
|
2010-02-13 13:51:38 -07:00
|
|
|
fpc->OriginUpperLeft = fp->OriginUpperLeft;
|
|
|
|
fpc->PixelCenterInteger = fp->PixelCenterInteger;
|
2006-12-14 13:56:58 -07:00
|
|
|
}
|
|
|
|
break;
|
2015-05-28 18:14:29 +02:00
|
|
|
case GL_GEOMETRY_PROGRAM_NV:
|
2010-06-28 17:31:21 -04:00
|
|
|
{
|
2012-07-18 15:33:11 -06:00
|
|
|
const struct gl_geometry_program *gp = gl_geometry_program_const(prog);
|
|
|
|
struct gl_geometry_program *gpc = gl_geometry_program(clone);
|
2010-06-28 17:31:21 -04:00
|
|
|
gpc->VerticesOut = gp->VerticesOut;
|
|
|
|
gpc->InputType = gp->InputType;
|
2014-01-25 12:34:24 -08:00
|
|
|
gpc->Invocations = gp->Invocations;
|
2010-06-28 17:31:21 -04:00
|
|
|
gpc->OutputType = gp->OutputType;
|
2014-06-16 17:00:15 +02:00
|
|
|
gpc->UsesEndPrimitive = gp->UsesEndPrimitive;
|
2014-06-06 12:31:30 +02:00
|
|
|
gpc->UsesStreams = gp->UsesStreams;
|
2010-06-28 17:31:21 -04:00
|
|
|
}
|
|
|
|
break;
|
2014-03-10 11:58:37 +01:00
|
|
|
case GL_TESS_CONTROL_PROGRAM_NV:
|
|
|
|
{
|
|
|
|
const struct gl_tess_ctrl_program *tcp = gl_tess_ctrl_program_const(prog);
|
|
|
|
struct gl_tess_ctrl_program *tcpc = gl_tess_ctrl_program(clone);
|
|
|
|
tcpc->VerticesOut = tcp->VerticesOut;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GL_TESS_EVALUATION_PROGRAM_NV:
|
|
|
|
{
|
|
|
|
const struct gl_tess_eval_program *tep = gl_tess_eval_program_const(prog);
|
|
|
|
struct gl_tess_eval_program *tepc = gl_tess_eval_program(clone);
|
|
|
|
tepc->PrimitiveMode = tep->PrimitiveMode;
|
|
|
|
tepc->Spacing = tep->Spacing;
|
|
|
|
tepc->VertexOrder = tep->VertexOrder;
|
|
|
|
tepc->PointMode = tep->PointMode;
|
|
|
|
}
|
|
|
|
break;
|
2006-12-14 13:56:58 -07:00
|
|
|
default:
|
|
|
|
_mesa_problem(NULL, "Unexpected target in _mesa_clone_program");
|
|
|
|
}
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-14 12:39:41 -06:00
|
|
|
/**
|
|
|
|
* Insert 'count' NOP instructions at 'start' in the given program.
|
|
|
|
* Adjust branch targets accordingly.
|
|
|
|
*/
|
|
|
|
GLboolean
|
|
|
|
_mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
|
|
|
|
{
|
|
|
|
const GLuint origLen = prog->NumInstructions;
|
|
|
|
const GLuint newLen = origLen + count;
|
|
|
|
struct prog_instruction *newInst;
|
|
|
|
GLuint i;
|
|
|
|
|
|
|
|
/* adjust branches */
|
|
|
|
for (i = 0; i < prog->NumInstructions; i++) {
|
|
|
|
struct prog_instruction *inst = prog->Instructions + i;
|
|
|
|
if (inst->BranchTarget > 0) {
|
2008-06-24 11:34:46 +09:00
|
|
|
if ((GLuint)inst->BranchTarget >= start) {
|
2008-05-14 12:39:41 -06:00
|
|
|
inst->BranchTarget += count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Alloc storage for new instructions */
|
|
|
|
newInst = _mesa_alloc_instructions(newLen);
|
|
|
|
if (!newInst) {
|
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy 'start' instructions into new instruction buffer */
|
|
|
|
_mesa_copy_instructions(newInst, prog->Instructions, start);
|
|
|
|
|
|
|
|
/* init the new instructions */
|
|
|
|
_mesa_init_instructions(newInst + start, count);
|
|
|
|
|
|
|
|
/* Copy the remaining/tail instructions to new inst buffer */
|
|
|
|
_mesa_copy_instructions(newInst + start + count,
|
|
|
|
prog->Instructions + start,
|
|
|
|
origLen - start);
|
|
|
|
|
|
|
|
/* free old instructions */
|
|
|
|
_mesa_free_instructions(prog->Instructions, origLen);
|
|
|
|
|
|
|
|
/* install new instructions */
|
|
|
|
prog->Instructions = newInst;
|
|
|
|
prog->NumInstructions = newLen;
|
|
|
|
|
|
|
|
return GL_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-07-06 19:48:50 +02:00
|
|
|
/**
|
|
|
|
* Delete 'count' instructions at 'start' in the given program.
|
|
|
|
* Adjust branch targets accordingly.
|
|
|
|
*/
|
|
|
|
GLboolean
|
|
|
|
_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count)
|
|
|
|
{
|
|
|
|
const GLuint origLen = prog->NumInstructions;
|
|
|
|
const GLuint newLen = origLen - count;
|
|
|
|
struct prog_instruction *newInst;
|
|
|
|
GLuint i;
|
|
|
|
|
|
|
|
/* adjust branches */
|
|
|
|
for (i = 0; i < prog->NumInstructions; i++) {
|
|
|
|
struct prog_instruction *inst = prog->Instructions + i;
|
|
|
|
if (inst->BranchTarget > 0) {
|
2010-01-27 17:03:04 -07:00
|
|
|
if (inst->BranchTarget > (GLint) start) {
|
2008-07-06 19:48:50 +02:00
|
|
|
inst->BranchTarget -= count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Alloc storage for new instructions */
|
|
|
|
newInst = _mesa_alloc_instructions(newLen);
|
|
|
|
if (!newInst) {
|
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy 'start' instructions into new instruction buffer */
|
|
|
|
_mesa_copy_instructions(newInst, prog->Instructions, start);
|
|
|
|
|
|
|
|
/* Copy the remaining/tail instructions to new inst buffer */
|
|
|
|
_mesa_copy_instructions(newInst + start,
|
|
|
|
prog->Instructions + start + count,
|
|
|
|
newLen - start);
|
|
|
|
|
|
|
|
/* free old instructions */
|
|
|
|
_mesa_free_instructions(prog->Instructions, origLen);
|
|
|
|
|
|
|
|
/* install new instructions */
|
|
|
|
prog->Instructions = newInst;
|
|
|
|
prog->NumInstructions = newLen;
|
|
|
|
|
|
|
|
return GL_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-14 12:53:03 -06:00
|
|
|
/**
|
|
|
|
* Search instructions for registers that match (oldFile, oldIndex),
|
|
|
|
* replacing them with (newFile, newIndex).
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
replace_registers(struct prog_instruction *inst, GLuint numInst,
|
|
|
|
GLuint oldFile, GLuint oldIndex,
|
|
|
|
GLuint newFile, GLuint newIndex)
|
|
|
|
{
|
|
|
|
GLuint i, j;
|
|
|
|
for (i = 0; i < numInst; i++) {
|
2008-05-18 15:46:26 -06:00
|
|
|
/* src regs */
|
2010-04-09 10:03:31 -06:00
|
|
|
for (j = 0; j < _mesa_num_inst_src_regs(inst[i].Opcode); j++) {
|
2008-05-14 12:53:03 -06:00
|
|
|
if (inst[i].SrcReg[j].File == oldFile &&
|
|
|
|
inst[i].SrcReg[j].Index == oldIndex) {
|
|
|
|
inst[i].SrcReg[j].File = newFile;
|
|
|
|
inst[i].SrcReg[j].Index = newIndex;
|
|
|
|
}
|
|
|
|
}
|
2008-05-18 15:46:26 -06:00
|
|
|
/* dst reg */
|
|
|
|
if (inst[i].DstReg.File == oldFile && inst[i].DstReg.Index == oldIndex) {
|
|
|
|
inst[i].DstReg.File = newFile;
|
|
|
|
inst[i].DstReg.Index = newIndex;
|
|
|
|
}
|
2008-05-14 12:53:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search instructions for references to program parameters. When found,
|
|
|
|
* increment the parameter index by 'offset'.
|
|
|
|
* Used when combining programs.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
adjust_param_indexes(struct prog_instruction *inst, GLuint numInst,
|
|
|
|
GLuint offset)
|
|
|
|
{
|
|
|
|
GLuint i, j;
|
|
|
|
for (i = 0; i < numInst; i++) {
|
2010-04-09 10:03:31 -06:00
|
|
|
for (j = 0; j < _mesa_num_inst_src_regs(inst[i].Opcode); j++) {
|
2008-05-14 12:53:03 -06:00
|
|
|
GLuint f = inst[i].SrcReg[j].File;
|
|
|
|
if (f == PROGRAM_CONSTANT ||
|
|
|
|
f == PROGRAM_UNIFORM ||
|
|
|
|
f == PROGRAM_STATE_VAR) {
|
|
|
|
inst[i].SrcReg[j].Index += offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Combine two programs into one. Fix instructions so the outputs of
|
|
|
|
* the first program go to the inputs of the second program.
|
|
|
|
*/
|
|
|
|
struct gl_program *
|
2010-10-12 12:26:10 -04:00
|
|
|
_mesa_combine_programs(struct gl_context *ctx,
|
2008-05-18 15:46:26 -06:00
|
|
|
const struct gl_program *progA,
|
|
|
|
const struct gl_program *progB)
|
2008-05-14 12:53:03 -06:00
|
|
|
{
|
|
|
|
struct prog_instruction *newInst;
|
|
|
|
struct gl_program *newProg;
|
|
|
|
const GLuint lenA = progA->NumInstructions - 1; /* omit END instr */
|
|
|
|
const GLuint lenB = progB->NumInstructions;
|
|
|
|
const GLuint numParamsA = _mesa_num_parameters(progA->Parameters);
|
|
|
|
const GLuint newLength = lenA + lenB;
|
2010-02-01 18:00:12 -07:00
|
|
|
GLboolean usedTemps[MAX_PROGRAM_TEMPS];
|
|
|
|
GLuint firstTemp = 0;
|
2013-01-22 17:44:02 -07:00
|
|
|
GLbitfield64 inputsB;
|
2008-05-14 12:53:03 -06:00
|
|
|
GLuint i;
|
|
|
|
|
2015-02-20 20:18:47 -08:00
|
|
|
assert(progA->Target == progB->Target);
|
2008-05-14 12:53:03 -06:00
|
|
|
|
|
|
|
newInst = _mesa_alloc_instructions(newLength);
|
|
|
|
if (!newInst)
|
|
|
|
return GL_FALSE;
|
|
|
|
|
|
|
|
_mesa_copy_instructions(newInst, progA->Instructions, lenA);
|
|
|
|
_mesa_copy_instructions(newInst + lenA, progB->Instructions, lenB);
|
|
|
|
|
|
|
|
/* adjust branch / instruction addresses for B's instructions */
|
|
|
|
for (i = 0; i < lenB; i++) {
|
|
|
|
newInst[lenA + i].BranchTarget += lenA;
|
|
|
|
}
|
|
|
|
|
|
|
|
newProg = ctx->Driver.NewProgram(ctx, progA->Target, 0);
|
|
|
|
newProg->Instructions = newInst;
|
|
|
|
newProg->NumInstructions = newLength;
|
|
|
|
|
2010-02-01 18:00:12 -07:00
|
|
|
/* find used temp regs (we may need new temps below) */
|
|
|
|
_mesa_find_used_registers(newProg, PROGRAM_TEMPORARY,
|
|
|
|
usedTemps, MAX_PROGRAM_TEMPS);
|
|
|
|
|
2008-05-14 12:53:03 -06:00
|
|
|
if (newProg->Target == GL_FRAGMENT_PROGRAM_ARB) {
|
2012-07-18 15:33:11 -06:00
|
|
|
const struct gl_fragment_program *fprogA, *fprogB;
|
|
|
|
struct gl_fragment_program *newFprog;
|
2013-01-22 17:44:02 -07:00
|
|
|
GLbitfield64 progB_inputsRead = progB->InputsRead;
|
2008-10-08 14:02:24 -06:00
|
|
|
GLint progB_colorFile, progB_colorIndex;
|
|
|
|
|
2012-07-18 15:33:11 -06:00
|
|
|
fprogA = gl_fragment_program_const(progA);
|
|
|
|
fprogB = gl_fragment_program_const(progB);
|
|
|
|
newFprog = gl_fragment_program(newProg);
|
2008-05-18 15:46:26 -06:00
|
|
|
|
|
|
|
newFprog->UsesKill = fprogA->UsesKill || fprogB->UsesKill;
|
2012-06-20 12:31:46 -07:00
|
|
|
newFprog->UsesDFdy = fprogA->UsesDFdy || fprogB->UsesDFdy;
|
2008-05-18 15:46:26 -06:00
|
|
|
|
2008-10-08 14:02:24 -06:00
|
|
|
/* We'll do a search and replace for instances
|
|
|
|
* of progB_colorFile/progB_colorIndex below...
|
|
|
|
*/
|
|
|
|
progB_colorFile = PROGRAM_INPUT;
|
2013-02-23 09:00:58 -08:00
|
|
|
progB_colorIndex = VARYING_SLOT_COL0;
|
2008-10-08 14:02:24 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The fragment program may get color from a state var rather than
|
|
|
|
* a fragment input (vertex output) if it's constant.
|
|
|
|
* See the texenvprogram.c code.
|
|
|
|
* So, search the program's parameter list now to see if the program
|
|
|
|
* gets color from a state var instead of a conventional fragment
|
|
|
|
* input register.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < progB->Parameters->NumParameters; i++) {
|
|
|
|
struct gl_program_parameter *p = &progB->Parameters->Parameters[i];
|
|
|
|
if (p->Type == PROGRAM_STATE_VAR &&
|
|
|
|
p->StateIndexes[0] == STATE_INTERNAL &&
|
|
|
|
p->StateIndexes[1] == STATE_CURRENT_ATTRIB &&
|
2010-10-25 09:10:32 -06:00
|
|
|
(int) p->StateIndexes[2] == (int) VERT_ATTRIB_COLOR0) {
|
2013-02-23 09:00:58 -08:00
|
|
|
progB_inputsRead |= VARYING_BIT_COL0;
|
2008-10-08 14:02:24 -06:00
|
|
|
progB_colorFile = PROGRAM_STATE_VAR;
|
|
|
|
progB_colorIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-18 15:46:26 -06:00
|
|
|
/* Connect color outputs of fprogA to color inputs of fprogB, via a
|
|
|
|
* new temporary register.
|
|
|
|
*/
|
2011-03-15 09:48:26 -06:00
|
|
|
if ((progA->OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_COLOR)) &&
|
2013-02-23 09:00:58 -08:00
|
|
|
(progB_inputsRead & VARYING_BIT_COL0)) {
|
2010-02-01 18:00:12 -07:00
|
|
|
GLint tempReg = _mesa_find_free_register(usedTemps, MAX_PROGRAM_TEMPS,
|
|
|
|
firstTemp);
|
2008-05-19 16:03:43 -06:00
|
|
|
if (tempReg < 0) {
|
2008-05-18 15:46:26 -06:00
|
|
|
_mesa_problem(ctx, "No free temp regs found in "
|
|
|
|
"_mesa_combine_programs(), using 31");
|
|
|
|
tempReg = 31;
|
|
|
|
}
|
2010-02-01 18:00:12 -07:00
|
|
|
firstTemp = tempReg + 1;
|
|
|
|
|
2008-05-18 15:46:26 -06:00
|
|
|
/* replace writes to result.color[0] with tempReg */
|
|
|
|
replace_registers(newInst, lenA,
|
2009-02-28 11:49:46 -07:00
|
|
|
PROGRAM_OUTPUT, FRAG_RESULT_COLOR,
|
2008-05-18 15:46:26 -06:00
|
|
|
PROGRAM_TEMPORARY, tempReg);
|
2008-10-08 14:02:24 -06:00
|
|
|
/* replace reads from the input color with tempReg */
|
2008-05-14 12:53:03 -06:00
|
|
|
replace_registers(newInst + lenA, lenB,
|
2008-10-08 14:02:24 -06:00
|
|
|
progB_colorFile, progB_colorIndex, /* search for */
|
|
|
|
PROGRAM_TEMPORARY, tempReg /* replace with */ );
|
2008-05-14 12:53:03 -06:00
|
|
|
}
|
|
|
|
|
2008-10-08 14:02:24 -06:00
|
|
|
/* compute combined program's InputsRead */
|
|
|
|
inputsB = progB_inputsRead;
|
2011-03-15 09:48:26 -06:00
|
|
|
if (progA->OutputsWritten & BITFIELD64_BIT(FRAG_RESULT_COLOR)) {
|
2013-02-23 09:00:58 -08:00
|
|
|
inputsB &= ~(1 << VARYING_SLOT_COL0);
|
2008-05-18 15:46:26 -06:00
|
|
|
}
|
|
|
|
newProg->InputsRead = progA->InputsRead | inputsB;
|
2008-05-14 12:53:03 -06:00
|
|
|
newProg->OutputsWritten = progB->OutputsWritten;
|
2008-05-18 15:46:26 -06:00
|
|
|
newProg->SamplersUsed = progA->SamplersUsed | progB->SamplersUsed;
|
2008-05-14 12:53:03 -06:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* vertex program */
|
|
|
|
assert(0); /* XXX todo */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Merge parameters (uniforms, constants, etc)
|
|
|
|
*/
|
|
|
|
newProg->Parameters = _mesa_combine_parameter_lists(progA->Parameters,
|
|
|
|
progB->Parameters);
|
|
|
|
|
|
|
|
adjust_param_indexes(newInst + lenA, lenB, numParamsA);
|
|
|
|
|
|
|
|
|
|
|
|
return newProg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2010-02-01 18:00:12 -07:00
|
|
|
* Populate the 'used' array with flags indicating which registers (TEMPs,
|
|
|
|
* INPUTs, OUTPUTs, etc, are used by the given program.
|
|
|
|
* \param file type of register to scan for
|
|
|
|
* \param used returns true/false flags for in use / free
|
|
|
|
* \param usedSize size of the 'used' array
|
2008-05-14 12:53:03 -06:00
|
|
|
*/
|
2010-02-01 18:00:12 -07:00
|
|
|
void
|
|
|
|
_mesa_find_used_registers(const struct gl_program *prog,
|
|
|
|
gl_register_file file,
|
|
|
|
GLboolean used[], GLuint usedSize)
|
2008-05-14 12:53:03 -06:00
|
|
|
{
|
2010-02-01 18:00:12 -07:00
|
|
|
GLuint i, j;
|
2008-05-14 12:53:03 -06:00
|
|
|
|
2010-02-18 23:51:00 -08:00
|
|
|
memset(used, 0, usedSize);
|
2008-05-14 12:53:03 -06:00
|
|
|
|
|
|
|
for (i = 0; i < prog->NumInstructions; i++) {
|
|
|
|
const struct prog_instruction *inst = prog->Instructions + i;
|
|
|
|
const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
|
|
|
|
|
2010-02-01 18:00:12 -07:00
|
|
|
if (inst->DstReg.File == file) {
|
2015-02-20 20:18:47 -08:00
|
|
|
assert(inst->DstReg.Index < usedSize);
|
2010-09-05 17:57:12 +02:00
|
|
|
if(inst->DstReg.Index < usedSize)
|
|
|
|
used[inst->DstReg.Index] = GL_TRUE;
|
2009-12-11 09:16:25 -07:00
|
|
|
}
|
2010-02-01 18:00:12 -07:00
|
|
|
|
|
|
|
for (j = 0; j < n; j++) {
|
|
|
|
if (inst->SrcReg[j].File == file) {
|
2015-02-20 20:18:47 -08:00
|
|
|
assert(inst->SrcReg[j].Index < (GLint) usedSize);
|
2013-01-22 17:19:30 -07:00
|
|
|
if (inst->SrcReg[j].Index < (GLint) usedSize)
|
2010-09-05 17:57:12 +02:00
|
|
|
used[inst->SrcReg[j].Index] = GL_TRUE;
|
2008-05-14 12:53:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-02-01 18:00:12 -07:00
|
|
|
}
|
2008-05-14 12:53:03 -06:00
|
|
|
|
2010-02-01 18:00:12 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Scan the given 'used' register flag array for the first entry
|
|
|
|
* that's >= firstReg.
|
|
|
|
* \param used vector of flags indicating registers in use (as returned
|
|
|
|
* by _mesa_find_used_registers())
|
|
|
|
* \param usedSize size of the 'used' array
|
|
|
|
* \param firstReg first register to start searching at
|
|
|
|
* \return index of unused register, or -1 if none.
|
|
|
|
*/
|
|
|
|
GLint
|
|
|
|
_mesa_find_free_register(const GLboolean used[],
|
|
|
|
GLuint usedSize, GLuint firstReg)
|
|
|
|
{
|
|
|
|
GLuint i;
|
|
|
|
|
|
|
|
assert(firstReg < usedSize);
|
|
|
|
|
|
|
|
for (i = firstReg; i < usedSize; i++)
|
2008-05-14 12:53:03 -06:00
|
|
|
if (!used[i])
|
|
|
|
return i;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2009-06-17 07:42:49 -06:00
|
|
|
|
|
|
|
|
2010-11-23 10:12:55 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the given register index is valid (doesn't exceed implementation-
|
|
|
|
* dependent limits).
|
|
|
|
* \return GL_TRUE if OK, GL_FALSE if bad index
|
|
|
|
*/
|
|
|
|
GLboolean
|
|
|
|
_mesa_valid_register_index(const struct gl_context *ctx,
|
2014-01-07 10:11:39 -08:00
|
|
|
gl_shader_stage shaderType,
|
2010-11-23 10:12:55 -07:00
|
|
|
gl_register_file file, GLint index)
|
|
|
|
{
|
|
|
|
const struct gl_program_constants *c;
|
|
|
|
|
2014-01-08 10:17:01 -08:00
|
|
|
assert(0 <= shaderType && shaderType < MESA_SHADER_STAGES);
|
|
|
|
c = &ctx->Const.Program[shaderType];
|
2010-11-23 10:12:55 -07:00
|
|
|
|
|
|
|
switch (file) {
|
|
|
|
case PROGRAM_UNDEFINED:
|
|
|
|
return GL_TRUE; /* XXX or maybe false? */
|
|
|
|
|
|
|
|
case PROGRAM_TEMPORARY:
|
2013-01-22 17:19:30 -07:00
|
|
|
return index >= 0 && index < (GLint) c->MaxTemps;
|
2010-11-23 10:12:55 -07:00
|
|
|
|
|
|
|
case PROGRAM_UNIFORM:
|
|
|
|
case PROGRAM_STATE_VAR:
|
|
|
|
/* aka constant buffer */
|
2013-01-22 17:19:30 -07:00
|
|
|
return index >= 0 && index < (GLint) c->MaxUniformComponents / 4;
|
2010-11-23 10:12:55 -07:00
|
|
|
|
|
|
|
case PROGRAM_CONSTANT:
|
|
|
|
/* constant buffer w/ possible relative negative addressing */
|
|
|
|
return (index > (int) c->MaxUniformComponents / -4 &&
|
2013-01-22 17:19:30 -07:00
|
|
|
index < (int) c->MaxUniformComponents / 4);
|
2010-11-23 10:12:55 -07:00
|
|
|
|
|
|
|
case PROGRAM_INPUT:
|
|
|
|
if (index < 0)
|
|
|
|
return GL_FALSE;
|
|
|
|
|
|
|
|
switch (shaderType) {
|
|
|
|
case MESA_SHADER_VERTEX:
|
2013-01-22 17:19:30 -07:00
|
|
|
return index < VERT_ATTRIB_GENERIC0 + (GLint) c->MaxAttribs;
|
2010-11-23 10:12:55 -07:00
|
|
|
case MESA_SHADER_FRAGMENT:
|
2013-02-23 09:00:58 -08:00
|
|
|
return index < VARYING_SLOT_VAR0 + (GLint) ctx->Const.MaxVarying;
|
2010-11-23 10:12:55 -07:00
|
|
|
case MESA_SHADER_GEOMETRY:
|
2013-02-23 07:34:06 -08:00
|
|
|
return index < VARYING_SLOT_VAR0 + (GLint) ctx->Const.MaxVarying;
|
2010-11-23 10:12:55 -07:00
|
|
|
default:
|
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PROGRAM_OUTPUT:
|
|
|
|
if (index < 0)
|
|
|
|
return GL_FALSE;
|
|
|
|
|
|
|
|
switch (shaderType) {
|
|
|
|
case MESA_SHADER_VERTEX:
|
2013-02-23 07:22:01 -08:00
|
|
|
return index < VARYING_SLOT_VAR0 + (GLint) ctx->Const.MaxVarying;
|
2010-11-23 10:12:55 -07:00
|
|
|
case MESA_SHADER_FRAGMENT:
|
2013-01-22 17:19:30 -07:00
|
|
|
return index < FRAG_RESULT_DATA0 + (GLint) ctx->Const.MaxDrawBuffers;
|
2010-11-23 10:12:55 -07:00
|
|
|
case MESA_SHADER_GEOMETRY:
|
2013-02-23 07:49:04 -08:00
|
|
|
return index < VARYING_SLOT_VAR0 + (GLint) ctx->Const.MaxVarying;
|
2010-11-23 10:12:55 -07:00
|
|
|
default:
|
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PROGRAM_ADDRESS:
|
2013-01-22 17:19:30 -07:00
|
|
|
return index >= 0 && index < (GLint) c->MaxAddressRegs;
|
2010-11-23 10:12:55 -07:00
|
|
|
|
|
|
|
default:
|
|
|
|
_mesa_problem(ctx,
|
|
|
|
"unexpected register file in _mesa_valid_register_index()");
|
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-06-17 07:42:49 -06:00
|
|
|
/**
|
|
|
|
* "Post-process" a GPU program. This is intended to be used for debugging.
|
|
|
|
* Example actions include no-op'ing instructions or changing instruction
|
|
|
|
* behaviour.
|
|
|
|
*/
|
|
|
|
void
|
2010-10-12 12:26:10 -04:00
|
|
|
_mesa_postprocess_program(struct gl_context *ctx, struct gl_program *prog)
|
2009-06-17 07:42:49 -06:00
|
|
|
{
|
|
|
|
static const GLfloat white[4] = { 0.5, 0.5, 0.5, 0.5 };
|
|
|
|
GLuint i;
|
|
|
|
GLuint whiteSwizzle;
|
|
|
|
GLint whiteIndex = _mesa_add_unnamed_constant(prog->Parameters,
|
2011-05-17 17:13:20 -05:00
|
|
|
(gl_constant_value *) white,
|
|
|
|
4, &whiteSwizzle);
|
2009-06-17 07:42:49 -06:00
|
|
|
|
2009-06-17 07:43:44 -06:00
|
|
|
(void) whiteIndex;
|
|
|
|
|
2009-06-17 07:42:49 -06:00
|
|
|
for (i = 0; i < prog->NumInstructions; i++) {
|
|
|
|
struct prog_instruction *inst = prog->Instructions + i;
|
|
|
|
const GLuint n = _mesa_num_inst_src_regs(inst->Opcode);
|
|
|
|
|
|
|
|
(void) n;
|
|
|
|
|
|
|
|
if (_mesa_is_tex_instruction(inst->Opcode)) {
|
|
|
|
#if 0
|
|
|
|
/* replace TEX/TXP/TXB with MOV */
|
|
|
|
inst->Opcode = OPCODE_MOV;
|
|
|
|
inst->DstReg.WriteMask = WRITEMASK_XYZW;
|
|
|
|
inst->SrcReg[0].Swizzle = SWIZZLE_XYZW;
|
|
|
|
inst->SrcReg[0].Negate = NEGATE_NONE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* disable shadow texture mode */
|
|
|
|
inst->TexShadow = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inst->Opcode == OPCODE_TXP) {
|
|
|
|
#if 0
|
|
|
|
inst->Opcode = OPCODE_MOV;
|
|
|
|
inst->DstReg.WriteMask = WRITEMASK_XYZW;
|
|
|
|
inst->SrcReg[0].File = PROGRAM_CONSTANT;
|
|
|
|
inst->SrcReg[0].Index = whiteIndex;
|
|
|
|
inst->SrcReg[0].Swizzle = SWIZZLE_XYZW;
|
|
|
|
inst->SrcReg[0].Negate = NEGATE_NONE;
|
|
|
|
#endif
|
|
|
|
#if 0
|
|
|
|
inst->TexShadow = 0;
|
|
|
|
#endif
|
|
|
|
#if 0
|
|
|
|
inst->Opcode = OPCODE_TEX;
|
|
|
|
inst->TexShadow = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2013-10-16 17:22:18 -07:00
|
|
|
|
|
|
|
/* Gets the minimum number of shader invocations per fragment.
|
|
|
|
* This function is useful to determine if we need to do per
|
|
|
|
* sample shading or per fragment shading.
|
|
|
|
*/
|
|
|
|
GLint
|
|
|
|
_mesa_get_min_invocations_per_fragment(struct gl_context *ctx,
|
2014-01-13 12:26:55 -08:00
|
|
|
const struct gl_fragment_program *prog,
|
|
|
|
bool ignore_sample_qualifier)
|
2013-10-16 17:22:18 -07:00
|
|
|
{
|
|
|
|
/* From ARB_sample_shading specification:
|
|
|
|
* "Using gl_SampleID in a fragment shader causes the entire shader
|
|
|
|
* to be evaluated per-sample."
|
|
|
|
*
|
|
|
|
* "Using gl_SamplePosition in a fragment shader causes the entire
|
|
|
|
* shader to be evaluated per-sample."
|
|
|
|
*
|
|
|
|
* "If MULTISAMPLE or SAMPLE_SHADING_ARB is disabled, sample shading
|
|
|
|
* has no effect."
|
|
|
|
*/
|
|
|
|
if (ctx->Multisample.Enabled) {
|
2013-11-29 21:44:13 +13:00
|
|
|
/* The ARB_gpu_shader5 specification says:
|
|
|
|
*
|
|
|
|
* "Use of the "sample" qualifier on a fragment shader input
|
|
|
|
* forces per-sample shading"
|
|
|
|
*/
|
2014-01-13 12:26:55 -08:00
|
|
|
if (prog->IsSample && !ignore_sample_qualifier)
|
2013-11-29 21:44:13 +13:00
|
|
|
return MAX2(ctx->DrawBuffer->Visual.samples, 1);
|
|
|
|
|
2013-10-16 17:22:18 -07:00
|
|
|
if (prog->Base.SystemValuesRead & (SYSTEM_BIT_SAMPLE_ID |
|
|
|
|
SYSTEM_BIT_SAMPLE_POS))
|
|
|
|
return MAX2(ctx->DrawBuffer->Visual.samples, 1);
|
|
|
|
else if (ctx->Multisample.SampleShading)
|
|
|
|
return MAX2(ceil(ctx->Multisample.MinSampleShadingValue *
|
|
|
|
ctx->DrawBuffer->Visual.samples), 1);
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|