aboutsummaryrefslogtreecommitdiff
path: root/mesalib/src/glsl
diff options
context:
space:
mode:
authormarha <marha@users.sourceforge.net>2011-10-27 08:37:52 +0200
committermarha <marha@users.sourceforge.net>2011-10-27 08:37:52 +0200
commit2a9be4af293f20fa33cc34fbc3b72e2235d91090 (patch)
treed41608bda1d56be1aa96857dee20e988b53760a3 /mesalib/src/glsl
parent9d53da0fbb9ae6df9a38ad40df4f53cd28287235 (diff)
parentd662d461634660f5c0f3998b5eb7d7ed3bd5a25f (diff)
downloadvcxsrv-2a9be4af293f20fa33cc34fbc3b72e2235d91090.tar.gz
vcxsrv-2a9be4af293f20fa33cc34fbc3b72e2235d91090.tar.bz2
vcxsrv-2a9be4af293f20fa33cc34fbc3b72e2235d91090.zip
Merge remote-tracking branch 'origin/released'
Diffstat (limited to 'mesalib/src/glsl')
-rw-r--r--mesalib/src/glsl/Makefile1
-rw-r--r--mesalib/src/glsl/SConscript1
-rw-r--r--mesalib/src/glsl/builtin_variables.h6
-rw-r--r--mesalib/src/glsl/glsl_parser_extras.cpp23
-rw-r--r--mesalib/src/glsl/ir_constant_expression.cpp29
-rw-r--r--mesalib/src/glsl/ir_optimization.h6
-rw-r--r--mesalib/src/glsl/ir_variable.cpp20
-rw-r--r--mesalib/src/glsl/link_uniforms.cpp85
-rw-r--r--mesalib/src/glsl/linker.cpp28
-rw-r--r--mesalib/src/glsl/linker.h43
-rw-r--r--mesalib/src/glsl/lower_if_to_cond_assign.cpp3
-rw-r--r--mesalib/src/glsl/main.cpp2
-rw-r--r--mesalib/src/glsl/opt_dead_code.cpp14
-rw-r--r--mesalib/src/glsl/ralloc.c28
-rw-r--r--mesalib/src/glsl/ralloc.h57
-rw-r--r--mesalib/src/glsl/test_optpass.cpp4
16 files changed, 318 insertions, 32 deletions
diff --git a/mesalib/src/glsl/Makefile b/mesalib/src/glsl/Makefile
index b2efb2abc..504f1fb43 100644
--- a/mesalib/src/glsl/Makefile
+++ b/mesalib/src/glsl/Makefile
@@ -53,6 +53,7 @@ CXX_SOURCES = \
ir_variable_refcount.cpp \
linker.cpp \
link_functions.cpp \
+ link_uniforms.cpp \
loop_analysis.cpp \
loop_controls.cpp \
loop_unroll.cpp \
diff --git a/mesalib/src/glsl/SConscript b/mesalib/src/glsl/SConscript
index b4786c5e5..09c7edbc4 100644
--- a/mesalib/src/glsl/SConscript
+++ b/mesalib/src/glsl/SConscript
@@ -64,6 +64,7 @@ glsl_sources = [
'ir_variable_refcount.cpp',
'linker.cpp',
'link_functions.cpp',
+ 'link_uniforms.cpp',
'loop_analysis.cpp',
'loop_controls.cpp',
'loop_unroll.cpp',
diff --git a/mesalib/src/glsl/builtin_variables.h b/mesalib/src/glsl/builtin_variables.h
index d25bbf42a..237b361d8 100644
--- a/mesalib/src/glsl/builtin_variables.h
+++ b/mesalib/src/glsl/builtin_variables.h
@@ -103,8 +103,8 @@ static const builtin_variable builtin_110_deprecated_uniforms[] = {
{ ir_var_uniform, -1, "gl_LightModelParameters", "gl_LightModel"},
/* Mesa-internal ATI_envmap_bumpmap state. */
- { ir_var_uniform, -1, "vec2", "gl_MESABumpRotMatrix0"},
- { ir_var_uniform, -1, "vec2", "gl_MESABumpRotMatrix1"},
- { ir_var_uniform, -1, "vec4", "gl_MESAFogParamsOptimized"},
+ { ir_var_uniform, -1, "vec2", "gl_BumpRotMatrix0MESA"},
+ { ir_var_uniform, -1, "vec2", "gl_BumpRotMatrix1MESA"},
+ { ir_var_uniform, -1, "vec4", "gl_FogParamsOptimizedMESA"},
};
diff --git a/mesalib/src/glsl/glsl_parser_extras.cpp b/mesalib/src/glsl/glsl_parser_extras.cpp
index a9075b2b1..e2112fe6d 100644
--- a/mesalib/src/glsl/glsl_parser_extras.cpp
+++ b/mesalib/src/glsl/glsl_parser_extras.cpp
@@ -883,8 +883,27 @@ ast_struct_specifier::ast_struct_specifier(char *identifier,
this->declarations.push_degenerate_list_at_head(&declarator_list->link);
}
+/**
+ * Do the set of common optimizations passes
+ *
+ * \param ir List of instructions to be optimized
+ * \param linked Is the shader linked? This enables
+ * optimizations passes that remove code at
+ * global scope and could cause linking to
+ * fail.
+ * \param uniform_locations_assigned Have locations already been assigned for
+ * uniforms? This prevents the declarations
+ * of unused uniforms from being removed.
+ * The setting of this flag only matters if
+ * \c linked is \c true.
+ * \param max_unroll_iterations Maximum number of loop iterations to be
+ * unrolled. Setting to 0 forces all loops
+ * to be unrolled.
+ */
bool
-do_common_optimization(exec_list *ir, bool linked, unsigned max_unroll_iterations)
+do_common_optimization(exec_list *ir, bool linked,
+ bool uniform_locations_assigned,
+ unsigned max_unroll_iterations)
{
GLboolean progress = GL_FALSE;
@@ -900,7 +919,7 @@ do_common_optimization(exec_list *ir, bool linked, unsigned max_unroll_iteration
progress = do_copy_propagation(ir) || progress;
progress = do_copy_propagation_elements(ir) || progress;
if (linked)
- progress = do_dead_code(ir) || progress;
+ progress = do_dead_code(ir, uniform_locations_assigned) || progress;
else
progress = do_dead_code_unlinked(ir) || progress;
progress = do_dead_code_local(ir) || progress;
diff --git a/mesalib/src/glsl/ir_constant_expression.cpp b/mesalib/src/glsl/ir_constant_expression.cpp
index b3fe6cf96..83f084d88 100644
--- a/mesalib/src/glsl/ir_constant_expression.cpp
+++ b/mesalib/src/glsl/ir_constant_expression.cpp
@@ -39,6 +39,25 @@
#include "ir_visitor.h"
#include "glsl_types.h"
+/* Using C99 rounding functions for roundToEven() implementation is
+ * difficult, because round(), rint, and nearbyint() are affected by
+ * fesetenv(), which the application may have done for its own
+ * purposes. Mesa's IROUND macro is close to what we want, but it
+ * rounds away from 0 on n + 0.5.
+ */
+static int
+round_to_even(float val)
+{
+ int rounded = IROUND(val);
+
+ if (val - floor(val) == 0.5) {
+ if (rounded % 2 != 0)
+ rounded += val > 0 ? -1 : 1;
+ }
+
+ return rounded;
+}
+
static float
dot(ir_constant *op0, ir_constant *op1)
{
@@ -196,6 +215,13 @@ ir_expression::constant_expression_value()
}
break;
+ case ir_unop_round_even:
+ assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
+ for (unsigned c = 0; c < op[0]->type->components(); c++) {
+ data.f[c] = round_to_even(op[0]->value.f[c]);
+ }
+ break;
+
case ir_unop_ceil:
assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
for (unsigned c = 0; c < op[0]->type->components(); c++) {
@@ -1324,6 +1350,9 @@ ir_call::constant_expression_value()
* op[1]->value.f[c];
}
}
+ } else if (strcmp(callee, "round") == 0 ||
+ strcmp(callee, "roundEven") == 0) {
+ expr = new(mem_ctx) ir_expression(ir_unop_round_even, op[0]);
} else if (strcmp(callee, "sign") == 0) {
expr = new(mem_ctx) ir_expression(ir_unop_sign, type, op[0], NULL);
} else if (strcmp(callee, "sin") == 0) {
diff --git a/mesalib/src/glsl/ir_optimization.h b/mesalib/src/glsl/ir_optimization.h
index af80e26b9..7b32e84f0 100644
--- a/mesalib/src/glsl/ir_optimization.h
+++ b/mesalib/src/glsl/ir_optimization.h
@@ -37,7 +37,9 @@
#define MOD_TO_FRACT 0x20
#define INT_DIV_TO_MUL_RCP 0x40
-bool do_common_optimization(exec_list *ir, bool linked, unsigned max_unroll_iterations);
+bool do_common_optimization(exec_list *ir, bool linked,
+ bool uniform_locations_assigned,
+ unsigned max_unroll_iterations);
bool do_algebraic(exec_list *instructions);
bool do_constant_folding(exec_list *instructions);
@@ -46,7 +48,7 @@ bool do_constant_variable_unlinked(exec_list *instructions);
bool do_copy_propagation(exec_list *instructions);
bool do_copy_propagation_elements(exec_list *instructions);
bool do_constant_propagation(exec_list *instructions);
-bool do_dead_code(exec_list *instructions);
+bool do_dead_code(exec_list *instructions, bool uniform_locations_assigned);
bool do_dead_code_local(exec_list *instructions);
bool do_dead_code_unlinked(exec_list *instructions);
bool do_dead_functions(exec_list *instructions);
diff --git a/mesalib/src/glsl/ir_variable.cpp b/mesalib/src/glsl/ir_variable.cpp
index 6ae3b1f9e..1ee84d219 100644
--- a/mesalib/src/glsl/ir_variable.cpp
+++ b/mesalib/src/glsl/ir_variable.cpp
@@ -166,15 +166,15 @@ static struct gl_builtin_uniform_element gl_NormalScale_elements[] = {
{NULL, {STATE_NORMAL_SCALE}, SWIZZLE_XXXX},
};
-static struct gl_builtin_uniform_element gl_MESABumpRotMatrix0_elements[] = {
+static struct gl_builtin_uniform_element gl_BumpRotMatrix0MESA_elements[] = {
{NULL, {STATE_INTERNAL, STATE_ROT_MATRIX_0}, SWIZZLE_XYZW},
};
-static struct gl_builtin_uniform_element gl_MESABumpRotMatrix1_elements[] = {
+static struct gl_builtin_uniform_element gl_BumpRotMatrix1MESA_elements[] = {
{NULL, {STATE_INTERNAL, STATE_ROT_MATRIX_1}, SWIZZLE_XYZW},
};
-static struct gl_builtin_uniform_element gl_MESAFogParamsOptimized_elements[] = {
+static struct gl_builtin_uniform_element gl_FogParamsOptimizedMESA_elements[] = {
{NULL, {STATE_INTERNAL, STATE_FOG_PARAMS_OPTIMIZED}, SWIZZLE_XYZW},
};
@@ -232,11 +232,11 @@ MATRIX(gl_TextureMatrixInverseTranspose,
static struct gl_builtin_uniform_element gl_NormalMatrix_elements[] = {
{ NULL, { STATE_MODELVIEW_MATRIX, 0, 0, 0, STATE_MATRIX_INVERSE},
- SWIZZLE_XYZW },
+ MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
{ NULL, { STATE_MODELVIEW_MATRIX, 0, 1, 1, STATE_MATRIX_INVERSE},
- SWIZZLE_XYZW },
+ MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
{ NULL, { STATE_MODELVIEW_MATRIX, 0, 2, 2, STATE_MATRIX_INVERSE},
- SWIZZLE_XYZW },
+ MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_Z) },
};
#undef MATRIX
@@ -289,9 +289,9 @@ const struct gl_builtin_uniform_desc _mesa_builtin_uniform_desc[] = {
STATEVAR(gl_NormalMatrix),
STATEVAR(gl_NormalScale),
- STATEVAR(gl_MESABumpRotMatrix0),
- STATEVAR(gl_MESABumpRotMatrix1),
- STATEVAR(gl_MESAFogParamsOptimized),
+ STATEVAR(gl_BumpRotMatrix0MESA),
+ STATEVAR(gl_BumpRotMatrix1MESA),
+ STATEVAR(gl_FogParamsOptimizedMESA),
STATEVAR(gl_CurrentAttribVertMESA),
STATEVAR(gl_CurrentAttribFragMESA),
@@ -615,6 +615,8 @@ generate_130_uniforms(exec_list *instructions,
add_builtin_constant(instructions, symtab, "gl_MaxClipDistances",
state->Const.MaxClipPlanes);
+ add_builtin_constant(instructions, symtab, "gl_MaxVaryingComponents",
+ state->Const.MaxVaryingFloats);
}
diff --git a/mesalib/src/glsl/link_uniforms.cpp b/mesalib/src/glsl/link_uniforms.cpp
new file mode 100644
index 000000000..6dd1f5354
--- /dev/null
+++ b/mesalib/src/glsl/link_uniforms.cpp
@@ -0,0 +1,85 @@
+/*
+ * Copyright © 2011 Intel Corporation
+ *
+ * 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 (including the next
+ * paragraph) 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
+ * 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.
+ */
+
+#include "main/core.h"
+#include "ir.h"
+#include "linker.h"
+#include "glsl_symbol_table.h"
+#include "program/hash_table.h"
+
+/**
+ * \file link_uniforms.cpp
+ * Assign locations for GLSL uniforms.
+ *
+ * \author Ian Romanick <ian.d.romanick@intel.com>
+ */
+
+void
+uniform_field_visitor::process(ir_variable *var)
+{
+ const glsl_type *t = var->type;
+
+ /* Only strdup the name if we actually will need to modify it. */
+ if (t->is_record() || (t->is_array() && t->fields.array->is_record())) {
+ char *name = ralloc_strdup(NULL, var->name);
+ recursion(var->type, &name, strlen(name));
+ ralloc_free(name);
+ } else {
+ this->visit_field(t, var->name);
+ }
+}
+
+void
+uniform_field_visitor::recursion(const glsl_type *t, char **name,
+ unsigned name_length)
+{
+ /* Records need to have each field processed individually.
+ *
+ * Arrays of records need to have each array element processed
+ * individually, then each field of the resulting array elements processed
+ * individually.
+ */
+ if (t->is_record()) {
+ for (unsigned i = 0; i < t->length; i++) {
+ const char *field = t->fields.structure[i].name;
+
+ /* Append '.field' to the current uniform name. */
+ ralloc_asprintf_rewrite_tail(name, name_length, ".%s", field);
+
+ recursion(t->fields.structure[i].type, name,
+ name_length + 1 + strlen(field));
+ }
+ } else if (t->is_array() && t->fields.array->is_record()) {
+ for (unsigned i = 0; i < t->length; i++) {
+ char subscript[13];
+
+ /* Append the subscript to the current uniform name */
+ const unsigned subscript_length = snprintf(subscript, 13, "[%u]", i);
+ ralloc_asprintf_rewrite_tail(name, name_length, "%s", subscript);
+
+ recursion(t->fields.array, name, name_length + subscript_length);
+ }
+ } else {
+ this->visit_field(t, *name);
+ }
+}
diff --git a/mesalib/src/glsl/linker.cpp b/mesalib/src/glsl/linker.cpp
index a7c38a342..beadec6f6 100644
--- a/mesalib/src/glsl/linker.cpp
+++ b/mesalib/src/glsl/linker.cpp
@@ -1742,14 +1742,10 @@ link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
if (ctx->ShaderCompilerOptions[i].LowerClipDistance)
lower_clip_distance(prog->_LinkedShaders[i]->ir);
- while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, 32))
+ while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, 32))
;
}
- update_array_sizes(prog);
-
- assign_uniform_locations(prog);
-
/* FINISHME: The value of the max_attribute_index parameter is
* FINISHME: implementation dependent based on the value of
* FINISHME: GL_MAX_VERTEX_ATTRIBS. GL_MAX_VERTEX_ATTRIBS must be
@@ -1785,6 +1781,12 @@ link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
if (prog->_LinkedShaders[MESA_SHADER_VERTEX] != NULL) {
demote_shader_inputs_and_outputs(prog->_LinkedShaders[MESA_SHADER_VERTEX],
ir_var_out);
+
+ /* Eliminate code that is now dead due to unused vertex outputs being
+ * demoted.
+ */
+ while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_VERTEX]->ir, false))
+ ;
}
if (prog->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
@@ -1793,14 +1795,30 @@ link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
demote_shader_inputs_and_outputs(sh, ir_var_in);
demote_shader_inputs_and_outputs(sh, ir_var_inout);
demote_shader_inputs_and_outputs(sh, ir_var_out);
+
+ /* Eliminate code that is now dead due to unused geometry outputs being
+ * demoted.
+ */
+ while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_GEOMETRY]->ir, false))
+ ;
}
if (prog->_LinkedShaders[MESA_SHADER_FRAGMENT] != NULL) {
gl_shader *const sh = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
demote_shader_inputs_and_outputs(sh, ir_var_in);
+
+ /* Eliminate code that is now dead due to unused fragment inputs being
+ * demoted. This shouldn't actually do anything other than remove
+ * declarations of the (now unused) global variables.
+ */
+ while (do_dead_code(prog->_LinkedShaders[MESA_SHADER_FRAGMENT]->ir, false))
+ ;
}
+ update_array_sizes(prog);
+ assign_uniform_locations(prog);
+
/* OpenGL ES requires that a vertex shader and a fragment shader both be
* present in a linked program. By checking for use of shading language
* version 1.00, we also catch the GL_ARB_ES2_compatibility case.
diff --git a/mesalib/src/glsl/linker.h b/mesalib/src/glsl/linker.h
index 769cf68b6..78c632961 100644
--- a/mesalib/src/glsl/linker.h
+++ b/mesalib/src/glsl/linker.h
@@ -1,3 +1,4 @@
+/* -*- c++ -*- */
/*
* Copyright © 2010 Intel Corporation
*
@@ -29,4 +30,46 @@ extern bool
link_function_calls(gl_shader_program *prog, gl_shader *main,
gl_shader **shader_list, unsigned num_shaders);
+/**
+ * Class for processing all of the leaf fields of an uniform
+ *
+ * Leaves are, roughly speaking, the parts of the uniform that the application
+ * could query with \c glGetUniformLocation (or that could be returned by
+ * \c glGetActiveUniforms).
+ *
+ * Classes my derive from this class to implement specific functionality.
+ * This class only provides the mechanism to iterate over the leaves. Derived
+ * classes must implement \c ::visit_field and may override \c ::process.
+ */
+class uniform_field_visitor {
+public:
+ /**
+ * Begin processing a uniform
+ *
+ * Classes that overload this function should call \c ::process from the
+ * base class to start the recursive processing of the uniform.
+ *
+ * \param var The uniform variable that is to be processed
+ *
+ * Calls \c ::visit_field for each leaf of the uniform.
+ */
+ void process(ir_variable *var);
+
+protected:
+ /**
+ * Method invoked for each leaf of the uniform
+ *
+ * \param type Type of the field.
+ * \param name Fully qualified name of the field.
+ */
+ virtual void visit_field(const glsl_type *type, const char *name) = 0;
+
+private:
+ /**
+ * \param name_length Length of the current name \b not including the
+ * terminating \c NUL character.
+ */
+ void recursion(const glsl_type *t, char **name, unsigned name_length);
+};
+
#endif /* GLSL_LINKER_H */
diff --git a/mesalib/src/glsl/lower_if_to_cond_assign.cpp b/mesalib/src/glsl/lower_if_to_cond_assign.cpp
index 7b89a1539..2c5d5612d 100644
--- a/mesalib/src/glsl/lower_if_to_cond_assign.cpp
+++ b/mesalib/src/glsl/lower_if_to_cond_assign.cpp
@@ -79,6 +79,9 @@ public:
bool
lower_if_to_cond_assign(exec_list *instructions, unsigned max_depth)
{
+ if (max_depth == UINT_MAX)
+ return false;
+
ir_if_to_cond_assign_visitor v(max_depth);
visit_list_elements(&v, instructions);
diff --git a/mesalib/src/glsl/main.cpp b/mesalib/src/glsl/main.cpp
index f0d951514..519abb5b5 100644
--- a/mesalib/src/glsl/main.cpp
+++ b/mesalib/src/glsl/main.cpp
@@ -177,7 +177,7 @@ compile_shader(struct gl_context *ctx, struct gl_shader *shader)
if (!state->error && !shader->ir->is_empty()) {
bool progress;
do {
- progress = do_common_optimization(shader->ir, false, 32);
+ progress = do_common_optimization(shader->ir, false, false, 32);
} while (progress);
validate_ir_tree(shader->ir);
diff --git a/mesalib/src/glsl/opt_dead_code.cpp b/mesalib/src/glsl/opt_dead_code.cpp
index cb500d2d1..5b9546ad4 100644
--- a/mesalib/src/glsl/opt_dead_code.cpp
+++ b/mesalib/src/glsl/opt_dead_code.cpp
@@ -42,7 +42,7 @@ static bool debug = false;
* for usage on an unlinked instruction stream.
*/
bool
-do_dead_code(exec_list *instructions)
+do_dead_code(exec_list *instructions, bool uniform_locations_assigned)
{
ir_variable_refcount_visitor v;
bool progress = false;
@@ -94,10 +94,11 @@ do_dead_code(exec_list *instructions)
*/
/* uniform initializers are precious, and could get used by another
- * stage.
+ * stage. Also, once uniform locations have been assigned, the
+ * declaration cannot be deleted.
*/
if (entry->var->mode == ir_var_uniform &&
- entry->var->constant_value)
+ (uniform_locations_assigned || entry->var->constant_value))
continue;
entry->var->remove();
@@ -132,7 +133,12 @@ do_dead_code_unlinked(exec_list *instructions)
foreach_iter(exec_list_iterator, sigiter, *f) {
ir_function_signature *sig =
(ir_function_signature *) sigiter.get();
- if (do_dead_code(&sig->body))
+ /* The setting of the uniform_locations_assigned flag here is
+ * irrelevent. If there is a uniform declaration encountered
+ * inside the body of the function, something has already gone
+ * terribly, terribly wrong.
+ */
+ if (do_dead_code(&sig->body, false))
progress = true;
}
}
diff --git a/mesalib/src/glsl/ralloc.c b/mesalib/src/glsl/ralloc.c
index fb48a91c5..f5f3934ac 100644
--- a/mesalib/src/glsl/ralloc.c
+++ b/mesalib/src/glsl/ralloc.c
@@ -439,7 +439,28 @@ ralloc_asprintf_append(char **str, const char *fmt, ...)
bool
ralloc_vasprintf_append(char **str, const char *fmt, va_list args)
{
- size_t existing_length, new_length;
+ size_t existing_length;
+ assert(str != NULL);
+ existing_length = *str ? strlen(*str) : 0;
+ return ralloc_vasprintf_rewrite_tail(str, existing_length, fmt, args);
+}
+
+bool
+ralloc_asprintf_rewrite_tail(char **str, size_t start, const char *fmt, ...)
+{
+ bool success;
+ va_list args;
+ va_start(args, fmt);
+ success = ralloc_vasprintf_rewrite_tail(str, start, fmt, args);
+ va_end(args);
+ return success;
+}
+
+bool
+ralloc_vasprintf_rewrite_tail(char **str, size_t start, const char *fmt,
+ va_list args)
+{
+ size_t new_length;
char *ptr;
assert(str != NULL);
@@ -450,14 +471,13 @@ ralloc_vasprintf_append(char **str, const char *fmt, va_list args)
return true;
}
- existing_length = strlen(*str);
new_length = printf_length(fmt, args);
- ptr = resize(*str, existing_length + new_length + 1);
+ ptr = resize(*str, start + new_length + 1);
if (unlikely(ptr == NULL))
return false;
- vsnprintf(ptr + existing_length, new_length + 1, fmt, args);
+ vsnprintf(ptr + start, new_length + 1, fmt, args);
*str = ptr;
return true;
}
diff --git a/mesalib/src/glsl/ralloc.h b/mesalib/src/glsl/ralloc.h
index d5338152f..1324f3466 100644
--- a/mesalib/src/glsl/ralloc.h
+++ b/mesalib/src/glsl/ralloc.h
@@ -314,9 +314,60 @@ char *ralloc_asprintf (const void *ctx, const char *fmt, ...);
char *ralloc_vasprintf(const void *ctx, const char *fmt, va_list args);
/**
+ * Rewrite the tail of an existing string, starting at a given index.
+ *
+ * Overwrites the contents of *str starting at \p start with newly formatted
+ * text, including a new null-terminator. Allocates more memory as necessary.
+ *
+ * This can be used to append formatted text when the length of the existing
+ * string is already known, saving a strlen() call.
+ *
+ * \sa ralloc_asprintf_append
+ *
+ * \param str The string to be updated.
+ * \param start The index to start appending new data at.
+ * \param fmt A printf-style formatting string
+ *
+ * \p str will be updated to the new pointer unless allocation fails.
+ *
+ * \return True unless allocation failed.
+ */
+bool ralloc_asprintf_rewrite_tail(char **str, size_t start,
+ const char *fmt, ...);
+
+/**
+ * Rewrite the tail of an existing string, starting at a given index.
+ *
+ * Overwrites the contents of *str starting at \p start with newly formatted
+ * text, including a new null-terminator. Allocates more memory as necessary.
+ *
+ * This can be used to append formatted text when the length of the existing
+ * string is already known, saving a strlen() call.
+ *
+ * \sa ralloc_vasprintf_append
+ *
+ * \param str The string to be updated.
+ * \param start The index to start appending new data at.
+ * \param fmt A printf-style formatting string
+ * \param args A va_list containing the data to be formatted
+ *
+ * \p str will be updated to the new pointer unless allocation fails.
+ *
+ * \return True unless allocation failed.
+ */
+bool ralloc_vasprintf_rewrite_tail(char **str, size_t start, const char *fmt,
+ va_list args);
+
+/**
* Append formatted text to the supplied string.
*
+ * This is equivalent to
+ * \code
+ * ralloc_asprintf_rewrite_tail(str, strlen(*str), fmt, ...)
+ * \endcode
+ *
* \sa ralloc_asprintf
+ * \sa ralloc_asprintf_rewrite_tail
* \sa ralloc_strcat
*
* \p str will be updated to the new pointer unless allocation fails.
@@ -328,7 +379,13 @@ bool ralloc_asprintf_append (char **str, const char *fmt, ...);
/**
* Append formatted text to the supplied string, given a va_list.
*
+ * This is equivalent to
+ * \code
+ * ralloc_vasprintf_rewrite_tail(str, strlen(*str), fmt, args)
+ * \endcode
+ *
* \sa ralloc_vasprintf
+ * \sa ralloc_vasprintf_rewrite_tail
* \sa ralloc_strcat
*
* \p str will be updated to the new pointer unless allocation fails.
diff --git a/mesalib/src/glsl/test_optpass.cpp b/mesalib/src/glsl/test_optpass.cpp
index 89b7f8338..6abafb5d3 100644
--- a/mesalib/src/glsl/test_optpass.cpp
+++ b/mesalib/src/glsl/test_optpass.cpp
@@ -64,7 +64,7 @@ do_optimization(struct exec_list *ir, const char *optimization)
if (sscanf(optimization, "do_common_optimization ( %d , %d ) ",
&int_0, &int_1) == 2) {
- return do_common_optimization(ir, int_0 != 0, int_1);
+ return do_common_optimization(ir, int_0 != 0, false, int_1);
} else if (strcmp(optimization, "do_algebraic") == 0) {
return do_algebraic(ir);
} else if (strcmp(optimization, "do_constant_folding") == 0) {
@@ -80,7 +80,7 @@ do_optimization(struct exec_list *ir, const char *optimization)
} else if (strcmp(optimization, "do_constant_propagation") == 0) {
return do_constant_propagation(ir);
} else if (strcmp(optimization, "do_dead_code") == 0) {
- return do_dead_code(ir);
+ return do_dead_code(ir, false);
} else if (strcmp(optimization, "do_dead_code_local") == 0) {
return do_dead_code_local(ir);
} else if (strcmp(optimization, "do_dead_code_unlinked") == 0) {