summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBrian Paul <[email protected]>2006-08-24 22:11:40 +0000
committerBrian Paul <[email protected]>2006-08-24 22:11:40 +0000
commitc6511ab950e2865e606ff13ce87f0ffc782c57ad (patch)
tree1fe378b6c1176fe3d9ec713b5db8a71437d7dc1b /src
parentbeb32c4a5905d2a5347f49d2941e8340d6959206 (diff)
Functions for vertex/fragment program transformations, optimizations, etc.
Diffstat (limited to 'src')
-rw-r--r--src/mesa/shader/programopt.c146
-rw-r--r--src/mesa/shader/programopt.h37
-rw-r--r--src/mesa/sources1
3 files changed, 184 insertions, 0 deletions
diff --git a/src/mesa/shader/programopt.c b/src/mesa/shader/programopt.c
new file mode 100644
index 00000000000..015d01a1d84
--- /dev/null
+++ b/src/mesa/shader/programopt.c
@@ -0,0 +1,146 @@
+/*
+ * Mesa 3-D graphics library
+ * Version: 6.5.1
+ *
+ * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
+ *
+ * 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
+ * BRIAN PAUL 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.
+ */
+
+/**
+ * \file programopt.c
+ * Vertex/Fragment program optimizations and transformations for program
+ * options, etc.
+ *
+ * \author Brian Paul
+ */
+
+
+#include "glheader.h"
+#include "context.h"
+#include "imports.h"
+#include "mtypes.h"
+#include "program.h"
+#include "programopt.h"
+#include "program_instruction.h"
+
+
+/**
+ * This is used for helping with position-invariant vertex programs.
+ * It appends extra instructions to the given program to do the
+ * vertex position transformation (multiply by the MVP matrix).
+ */
+void
+_mesa_append_modelview_code(GLcontext *ctx, struct gl_vertex_program *vprog)
+{
+ struct prog_instruction newInst[5];
+ const GLuint origLen = vprog->Base.NumInstructions;
+ GLuint i;
+
+ /*
+ * Setup state references for the modelview/projection matrix.
+ * XXX we should check if these state vars are already declared.
+ */
+ static const GLint mvpState[4][5] = {
+ { STATE_MATRIX, STATE_MVP, 0, 0, 0 }, /* state.matrix.mvp.row[0] */
+ { STATE_MATRIX, STATE_MVP, 0, 1, 1 }, /* state.matrix.mvp.row[1] */
+ { STATE_MATRIX, STATE_MVP, 0, 2, 2 }, /* state.matrix.mvp.row[2] */
+ { STATE_MATRIX, STATE_MVP, 0, 3, 3 }, /* state.matrix.mvp.row[3] */
+ };
+ GLint mvpRef[4];
+
+ for (i = 0; i < 4; i++) {
+ mvpRef[i] = _mesa_add_state_reference(vprog->Base.Parameters,
+ mvpState[i]);
+ }
+
+ /*
+ * Generated instructions:
+ * newInst[0] = DP4 result.position.x, mvp.row[0], vertex.position;
+ * newInst[1] = DP4 result.position.y, mvp.row[1], vertex.position;
+ * newInst[2] = DP4 result.position.z, mvp.row[2], vertex.position;
+ * newInst[3] = DP4 result.position.w, mvp.row[3], vertex.position;
+ * newInst[4] = END;
+ */
+ for (i = 0; i < 4; i++) {
+ _mesa_init_instruction(newInst + i);
+ newInst[i].Opcode = OPCODE_DP4;
+ newInst[i].DstReg.File = PROGRAM_OUTPUT;
+ newInst[i].DstReg.Index = VERT_RESULT_HPOS;
+ newInst[i].DstReg.WriteMask = (WRITEMASK_X << i);
+ newInst[i].SrcReg[0].File = PROGRAM_STATE_VAR;
+ newInst[i].SrcReg[0].Index = mvpRef[i];
+ newInst[i].SrcReg[0].Swizzle = SWIZZLE_NOOP;
+ newInst[i].SrcReg[1].File = PROGRAM_INPUT;
+ newInst[i].SrcReg[1].Index = VERT_ATTRIB_POS;
+ newInst[i].SrcReg[1].Swizzle = SWIZZLE_NOOP;
+ }
+ newInst[4].Opcode = OPCODE_END;
+
+ /*
+ * Append new instructions onto program.
+ */
+ vprog->Base.Instructions
+ = _mesa_realloc_instructions(vprog->Base.Instructions,
+ origLen, origLen + 4);
+ if (!vprog->Base.Instructions) {
+ _mesa_error(ctx, GL_OUT_OF_MEMORY,
+ "glProgramString(generating position transformation code)");
+ return;
+ }
+ /* subtract one to overwrite original program's END instruction */
+ _mesa_memcpy(vprog->Base.Instructions + origLen - 1,
+ newInst, sizeof(newInst));
+
+ vprog->Base.NumInstructions = origLen + 4;
+ vprog->Base.InputsRead |= VERT_BIT_POS;
+ vprog->Base.OutputsWritten |= (1 << VERT_RESULT_HPOS);
+}
+
+
+
+/**
+ * Append extra instructions onto the given fragment program to implement
+ * the fog mode specified by program->FogOption.
+ * XXX incomplete.
+ */
+void
+_mesa_append_fog_code(GLcontext *ctx, struct gl_fragment_program *fprog)
+{
+ struct prog_instruction newInst[10];
+
+ switch (fprog->FogOption) {
+ case GL_LINEAR:
+ /* lerp */
+ break;
+ case GL_EXP:
+ break;
+ case GL_EXP2:
+ break;
+ case GL_NONE:
+ /* no-op */
+ return;
+ default:
+ _mesa_problem(ctx, "Invalid fog option");
+ return;
+ }
+
+
+ (void) newInst;
+
+}
diff --git a/src/mesa/shader/programopt.h b/src/mesa/shader/programopt.h
new file mode 100644
index 00000000000..cad43c2538c
--- /dev/null
+++ b/src/mesa/shader/programopt.h
@@ -0,0 +1,37 @@
+/*
+ * Mesa 3-D graphics library
+ * Version: 6.5.1
+ *
+ * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
+ *
+ * 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
+ * BRIAN PAUL 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.
+ */
+
+
+#ifndef PROGRAMOPT_H
+#define PROGRAMOPT_H 1
+
+
+extern void
+_mesa_append_modelview_code(GLcontext *ctx, struct gl_vertex_program *vprog);
+
+extern void
+_mesa_append_fog_code(GLcontext *ctx, struct gl_fragment_program *fprog);
+
+
+#endif /* PROGRAMOPT_H */
diff --git a/src/mesa/sources b/src/mesa/sources
index c02ba503401..b9c72c0c1bc 100644
--- a/src/mesa/sources
+++ b/src/mesa/sources
@@ -160,6 +160,7 @@ SHADER_SOURCES = \
shader/nvvertexec.c \
shader/nvvertparse.c \
shader/program.c \
+ shader/programopt.c \
shader/shaderobjects.c \
shader/shaderobjects_3dlabs.c