1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #define DEBUG_PARSING 0
26 #define DEBUG_VP 0
27 #define DEBUG_FP 0
28
29 /**
30 * \file arbprogparse.c
31 * ARB_*_program parser core
32 * \author Karl Rasche
33 */
34
35 /**
36 Notes on program parameters, etc.
37
38 The instructions we emit will use six kinds of source registers:
39
40 PROGRAM_INPUT - input registers
41 PROGRAM_TEMPORARY - temp registers
42 PROGRAM_ADDRESS - address/indirect register
43 PROGRAM_CONSTANT - indexes into program->Parameters, a known constant/literal
44 PROGRAM_STATE_VAR - indexes into program->Parameters, and may actually be:
45 + a state variable, like "state.fog.color", or
46 + a pointer to a "program.local[k]" parameter, or
47 + a pointer to a "program.env[k]" parameter
48
49 Basically, all the program.local[] and program.env[] values will get mapped
50 into the unified gl_program->Parameters array. This solves the problem of
51 having three separate program parameter arrays.
52 */
53
54
55 #include "util/glheader.h"
56
57 #include "main/context.h"
58 #include "arbprogparse.h"
59 #include "prog_parameter.h"
60 #include "prog_statevars.h"
61 #include "prog_instruction.h"
62 #include "prog_print.h"
63 #include "program_parser.h"
64
65
66 void
_mesa_parse_arb_fragment_program(struct gl_context * ctx,GLenum target,const GLvoid * str,GLsizei len,struct gl_program * program)67 _mesa_parse_arb_fragment_program(struct gl_context* ctx, GLenum target,
68 const GLvoid *str, GLsizei len,
69 struct gl_program *program)
70 {
71 struct gl_program prog;
72 struct asm_parser_state state;
73 GLuint i;
74
75 assert(target == GL_FRAGMENT_PROGRAM_ARB);
76
77 memset(&prog, 0, sizeof(prog));
78 memset(&state, 0, sizeof(state));
79 state.prog = &prog;
80 state.mem_ctx = program;
81
82 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len,
83 &state)) {
84 /* Error in the program. Just return. */
85 return;
86 }
87
88 ralloc_free(program->String);
89
90 /* Copy the relevant contents of the arb_program struct into the
91 * fragment_program struct.
92 */
93 program->String = prog.String;
94 program->arb.NumInstructions = prog.arb.NumInstructions;
95 program->arb.NumTemporaries = prog.arb.NumTemporaries;
96 program->arb.NumParameters = prog.arb.NumParameters;
97 program->arb.NumAttributes = prog.arb.NumAttributes;
98 program->arb.NumAddressRegs = prog.arb.NumAddressRegs;
99 program->arb.NumNativeInstructions = prog.arb.NumNativeInstructions;
100 program->arb.NumNativeTemporaries = prog.arb.NumNativeTemporaries;
101 program->arb.NumNativeParameters = prog.arb.NumNativeParameters;
102 program->arb.NumNativeAttributes = prog.arb.NumNativeAttributes;
103 program->arb.NumNativeAddressRegs = prog.arb.NumNativeAddressRegs;
104 program->arb.NumAluInstructions = prog.arb.NumAluInstructions;
105 program->arb.NumTexInstructions = prog.arb.NumTexInstructions;
106 program->arb.NumTexIndirections = prog.arb.NumTexIndirections;
107 program->arb.NumNativeAluInstructions = prog.arb.NumAluInstructions;
108 program->arb.NumNativeTexInstructions = prog.arb.NumTexInstructions;
109 program->arb.NumNativeTexIndirections = prog.arb.NumTexIndirections;
110 program->info.inputs_read = prog.info.inputs_read;
111 program->info.outputs_written = prog.info.outputs_written;
112 program->arb.IndirectRegisterFiles = prog.arb.IndirectRegisterFiles;
113 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++) {
114 program->TexturesUsed[i] = prog.TexturesUsed[i];
115 if (prog.TexturesUsed[i])
116 program->SamplersUsed |= (1 << i);
117 }
118 program->ShadowSamplers = prog.ShadowSamplers;
119 program->info.fs.origin_upper_left = state.option.OriginUpperLeft;
120 program->info.fs.pixel_center_integer = state.option.PixelCenterInteger;
121
122 program->info.fs.uses_discard = state.fragment.UsesKill;
123 program->arb.Fog = state.option.Fog;
124
125 ralloc_free(program->arb.Instructions);
126 program->arb.Instructions = prog.arb.Instructions;
127
128 if (program->Parameters)
129 _mesa_free_parameter_list(program->Parameters);
130 program->Parameters = prog.Parameters;
131
132 #if DEBUG_FP
133 printf("____________Fragment program %u ________\n", program->Id);
134 _mesa_print_program(program);
135 #endif
136 }
137
138
139
140 /**
141 * Parse the vertex program string. If success, update the given
142 * vertex_program object with the new program. Else, leave the vertex_program
143 * object unchanged.
144 */
145 void
_mesa_parse_arb_vertex_program(struct gl_context * ctx,GLenum target,const GLvoid * str,GLsizei len,struct gl_program * program)146 _mesa_parse_arb_vertex_program(struct gl_context *ctx, GLenum target,
147 const GLvoid *str, GLsizei len,
148 struct gl_program *program)
149 {
150 struct gl_program prog;
151 struct asm_parser_state state;
152
153 assert(target == GL_VERTEX_PROGRAM_ARB);
154
155 memset(&prog, 0, sizeof(prog));
156 memset(&state, 0, sizeof(state));
157 state.prog = &prog;
158 state.mem_ctx = program;
159
160 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len,
161 &state)) {
162 _mesa_error(ctx, GL_INVALID_OPERATION, "glProgramString(bad program)");
163 return;
164 }
165
166 ralloc_free(program->String);
167
168 /* Copy the relevant contents of the arb_program struct into the
169 * vertex_program struct.
170 */
171 program->String = prog.String;
172 program->arb.NumInstructions = prog.arb.NumInstructions;
173 program->arb.NumTemporaries = prog.arb.NumTemporaries;
174 program->arb.NumParameters = prog.arb.NumParameters;
175 program->arb.NumAttributes = prog.arb.NumAttributes;
176 program->arb.NumAddressRegs = prog.arb.NumAddressRegs;
177 program->arb.NumNativeInstructions = prog.arb.NumNativeInstructions;
178 program->arb.NumNativeTemporaries = prog.arb.NumNativeTemporaries;
179 program->arb.NumNativeParameters = prog.arb.NumNativeParameters;
180 program->arb.NumNativeAttributes = prog.arb.NumNativeAttributes;
181 program->arb.NumNativeAddressRegs = prog.arb.NumNativeAddressRegs;
182 program->info.inputs_read = prog.info.inputs_read;
183 program->info.outputs_written = prog.info.outputs_written;
184 program->arb.IndirectRegisterFiles = prog.arb.IndirectRegisterFiles;
185 program->arb.IsPositionInvariant = (state.option.PositionInvariant)
186 ? GL_TRUE : GL_FALSE;
187
188 ralloc_free(program->arb.Instructions);
189 program->arb.Instructions = prog.arb.Instructions;
190
191 if (program->Parameters)
192 _mesa_free_parameter_list(program->Parameters);
193 program->Parameters = prog.Parameters;
194
195 #if DEBUG_VP
196 printf("____________Vertex program %u __________\n", program->Id);
197 _mesa_print_program(program);
198 #endif
199 }
200