Brian Paul | c6511ab | 2006-08-24 22:11:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mesa 3-D graphics library |
| 3 | * Version: 6.5.1 |
| 4 | * |
| 5 | * Copyright (C) 1999-2006 Brian Paul All Rights Reserved. |
| 6 | * |
| 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 programopt.c |
| 27 | * Vertex/Fragment program optimizations and transformations for program |
| 28 | * options, etc. |
| 29 | * |
| 30 | * \author Brian Paul |
| 31 | */ |
| 32 | |
| 33 | |
| 34 | #include "glheader.h" |
| 35 | #include "context.h" |
| 36 | #include "imports.h" |
| 37 | #include "mtypes.h" |
| 38 | #include "program.h" |
| 39 | #include "programopt.h" |
| 40 | #include "program_instruction.h" |
| 41 | |
| 42 | |
| 43 | /** |
Brian Paul | 3c54e83 | 2006-08-25 15:15:24 +0000 | [diff] [blame] | 44 | * This function inserts instructions for coordinate modelview * projection |
| 45 | * into a vertex program. |
| 46 | * May be used to implement the position_invariant option. |
Brian Paul | c6511ab | 2006-08-24 22:11:40 +0000 | [diff] [blame] | 47 | */ |
| 48 | void |
Brian Paul | 3c54e83 | 2006-08-25 15:15:24 +0000 | [diff] [blame] | 49 | _mesa_insert_mvp_code(GLcontext *ctx, struct gl_vertex_program *vprog) |
Brian Paul | c6511ab | 2006-08-24 22:11:40 +0000 | [diff] [blame] | 50 | { |
Brian Paul | 3c54e83 | 2006-08-25 15:15:24 +0000 | [diff] [blame] | 51 | struct prog_instruction *newInst; |
Brian Paul | c6511ab | 2006-08-24 22:11:40 +0000 | [diff] [blame] | 52 | const GLuint origLen = vprog->Base.NumInstructions; |
Brian Paul | 3c54e83 | 2006-08-25 15:15:24 +0000 | [diff] [blame] | 53 | const GLuint newLen = origLen + 4; |
Brian Paul | c6511ab | 2006-08-24 22:11:40 +0000 | [diff] [blame] | 54 | GLuint i; |
| 55 | |
| 56 | /* |
| 57 | * Setup state references for the modelview/projection matrix. |
| 58 | * XXX we should check if these state vars are already declared. |
| 59 | */ |
| 60 | static const GLint mvpState[4][5] = { |
| 61 | { STATE_MATRIX, STATE_MVP, 0, 0, 0 }, /* state.matrix.mvp.row[0] */ |
| 62 | { STATE_MATRIX, STATE_MVP, 0, 1, 1 }, /* state.matrix.mvp.row[1] */ |
| 63 | { STATE_MATRIX, STATE_MVP, 0, 2, 2 }, /* state.matrix.mvp.row[2] */ |
| 64 | { STATE_MATRIX, STATE_MVP, 0, 3, 3 }, /* state.matrix.mvp.row[3] */ |
| 65 | }; |
| 66 | GLint mvpRef[4]; |
| 67 | |
| 68 | for (i = 0; i < 4; i++) { |
| 69 | mvpRef[i] = _mesa_add_state_reference(vprog->Base.Parameters, |
| 70 | mvpState[i]); |
| 71 | } |
| 72 | |
Brian Paul | 3c54e83 | 2006-08-25 15:15:24 +0000 | [diff] [blame] | 73 | /* Alloc storage for new instructions */ |
| 74 | newInst = _mesa_alloc_instructions(newLen); |
| 75 | if (!newInst) { |
| 76 | _mesa_error(ctx, GL_OUT_OF_MEMORY, |
| 77 | "glProgramString(inserting position_invariant code)"); |
| 78 | return; |
| 79 | } |
| 80 | |
Brian Paul | c6511ab | 2006-08-24 22:11:40 +0000 | [diff] [blame] | 81 | /* |
| 82 | * Generated instructions: |
| 83 | * newInst[0] = DP4 result.position.x, mvp.row[0], vertex.position; |
| 84 | * newInst[1] = DP4 result.position.y, mvp.row[1], vertex.position; |
| 85 | * newInst[2] = DP4 result.position.z, mvp.row[2], vertex.position; |
| 86 | * newInst[3] = DP4 result.position.w, mvp.row[3], vertex.position; |
Brian Paul | c6511ab | 2006-08-24 22:11:40 +0000 | [diff] [blame] | 87 | */ |
| 88 | for (i = 0; i < 4; i++) { |
| 89 | _mesa_init_instruction(newInst + i); |
| 90 | newInst[i].Opcode = OPCODE_DP4; |
| 91 | newInst[i].DstReg.File = PROGRAM_OUTPUT; |
| 92 | newInst[i].DstReg.Index = VERT_RESULT_HPOS; |
| 93 | newInst[i].DstReg.WriteMask = (WRITEMASK_X << i); |
| 94 | newInst[i].SrcReg[0].File = PROGRAM_STATE_VAR; |
| 95 | newInst[i].SrcReg[0].Index = mvpRef[i]; |
| 96 | newInst[i].SrcReg[0].Swizzle = SWIZZLE_NOOP; |
| 97 | newInst[i].SrcReg[1].File = PROGRAM_INPUT; |
| 98 | newInst[i].SrcReg[1].Index = VERT_ATTRIB_POS; |
| 99 | newInst[i].SrcReg[1].Swizzle = SWIZZLE_NOOP; |
| 100 | } |
Brian Paul | c6511ab | 2006-08-24 22:11:40 +0000 | [diff] [blame] | 101 | |
Brian Paul | 3c54e83 | 2006-08-25 15:15:24 +0000 | [diff] [blame] | 102 | /* Append original instructions after new instructions */ |
| 103 | _mesa_memcpy(newInst + 4, vprog->Base.Instructions, |
| 104 | origLen * sizeof(struct prog_instruction)); |
Brian Paul | c6511ab | 2006-08-24 22:11:40 +0000 | [diff] [blame] | 105 | |
Brian Paul | 3c54e83 | 2006-08-25 15:15:24 +0000 | [diff] [blame] | 106 | /* free old instructions */ |
| 107 | _mesa_free(vprog->Base.Instructions); |
| 108 | |
| 109 | /* install new instructions */ |
| 110 | vprog->Base.Instructions = newInst; |
| 111 | vprog->Base.NumInstructions = newLen; |
Brian Paul | c6511ab | 2006-08-24 22:11:40 +0000 | [diff] [blame] | 112 | vprog->Base.InputsRead |= VERT_BIT_POS; |
| 113 | vprog->Base.OutputsWritten |= (1 << VERT_RESULT_HPOS); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | |
| 118 | /** |
| 119 | * Append extra instructions onto the given fragment program to implement |
| 120 | * the fog mode specified by program->FogOption. |
| 121 | * XXX incomplete. |
| 122 | */ |
| 123 | void |
| 124 | _mesa_append_fog_code(GLcontext *ctx, struct gl_fragment_program *fprog) |
| 125 | { |
| 126 | struct prog_instruction newInst[10]; |
| 127 | |
| 128 | switch (fprog->FogOption) { |
| 129 | case GL_LINEAR: |
| 130 | /* lerp */ |
| 131 | break; |
| 132 | case GL_EXP: |
| 133 | break; |
| 134 | case GL_EXP2: |
| 135 | break; |
| 136 | case GL_NONE: |
| 137 | /* no-op */ |
| 138 | return; |
| 139 | default: |
| 140 | _mesa_problem(ctx, "Invalid fog option"); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | (void) newInst; |
| 146 | |
| 147 | } |