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
27 /**
28 * \file arbprogparse.c
29 * ARB_*_program parser core
30 * \author Karl Rasche
31 */
32
33 /**
34 Notes on program parameters, etc.
35
36 The instructions we emit will use six kinds of source registers:
37
38 PROGRAM_INPUT - input registers
39 PROGRAM_TEMPORARY - temp registers
40 PROGRAM_ADDRESS - address/indirect register
41 PROGRAM_CONSTANT - indexes into program->Parameters, a known constant/literal
42 PROGRAM_STATE_VAR - indexes into program->Parameters, and may actually be:
43 + a state variable, like "state.fog.color", or
44 + a pointer to a "program.local[k]" parameter, or
45 + a pointer to a "program.env[k]" parameter
46
47 Basically, all the program.local[] and program.env[] values will get mapped
48 into the unified gl_program->Parameters array. This solves the problem of
49 having three separate program parameter arrays.
50 */
51
52
53 #include "main/glheader.h"
54
55 #include "main/context.h"
56 #include "arbprogparse.h"
57 #include "programopt.h"
58 #include "prog_parameter.h"
59 #include "prog_statevars.h"
60 #include "prog_instruction.h"
61 #include "program_parser.h"
62
63
64 void
_mesa_parse_arb_fragment_program(struct gl_context * ctx,GLenum target,const GLvoid * str,GLsizei len,struct gl_program * program)65 _mesa_parse_arb_fragment_program(struct gl_context* ctx, GLenum target,
66 const GLvoid *str, GLsizei len,
67 struct gl_program *program)
68 {
69 struct gl_program prog;
70 struct asm_parser_state state;
71 GLuint i;
72
73 assert(target == GL_FRAGMENT_PROGRAM_ARB);
74
75 memset(&prog, 0, sizeof(prog));
76 memset(&state, 0, sizeof(state));
77 state.prog = &prog;
78 state.mem_ctx = program;
79
80 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len,
81 &state)) {
82 /* Error in the program. Just return. */
83 return;
84 }
85
86 ralloc_free(program->String);
87
88 /* Copy the relevant contents of the arb_program struct into the
89 * fragment_program struct.
90 */
91 program->String = prog.String;
92 program->arb.NumInstructions = prog.arb.NumInstructions;
93 program->arb.NumTemporaries = prog.arb.NumTemporaries;
94 program->arb.NumParameters = prog.arb.NumParameters;
95 program->arb.NumAttributes = prog.arb.NumAttributes;
96 program->arb.NumAddressRegs = prog.arb.NumAddressRegs;
97 program->arb.NumNativeInstructions = prog.arb.NumNativeInstructions;
98 program->arb.NumNativeTemporaries = prog.arb.NumNativeTemporaries;
99 program->arb.NumNativeParameters = prog.arb.NumNativeParameters;
100 program->arb.NumNativeAttributes = prog.arb.NumNativeAttributes;
101 program->arb.NumNativeAddressRegs = prog.arb.NumNativeAddressRegs;
102 program->arb.NumAluInstructions = prog.arb.NumAluInstructions;
103 program->arb.NumTexInstructions = prog.arb.NumTexInstructions;
104 program->arb.NumTexIndirections = prog.arb.NumTexIndirections;
105 program->arb.NumNativeAluInstructions = prog.arb.NumAluInstructions;
106 program->arb.NumNativeTexInstructions = prog.arb.NumTexInstructions;
107 program->arb.NumNativeTexIndirections = prog.arb.NumTexIndirections;
108 program->info.inputs_read = prog.info.inputs_read;
109 program->info.outputs_written = prog.info.outputs_written;
110 program->arb.IndirectRegisterFiles = prog.arb.IndirectRegisterFiles;
111 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++) {
112 program->TexturesUsed[i] = prog.TexturesUsed[i];
113 if (prog.TexturesUsed[i])
114 program->SamplersUsed |= (1 << i);
115 }
116 program->ShadowSamplers = prog.ShadowSamplers;
117 program->info.fs.origin_upper_left = state.option.OriginUpperLeft;
118 program->info.fs.pixel_center_integer = state.option.PixelCenterInteger;
119
120 program->info.fs.uses_discard = state.fragment.UsesKill;
121
122 ralloc_free(program->arb.Instructions);
123 program->arb.Instructions = prog.arb.Instructions;
124
125 if (program->Parameters)
126 _mesa_free_parameter_list(program->Parameters);
127 program->Parameters = prog.Parameters;
128
129 /* Append fog instructions now if the program has "OPTION ARB_fog_exp"
130 * or similar. We used to leave this up to drivers, but it appears
131 * there's no hardware that wants to do fog in a discrete stage separate
132 * from the fragment shader.
133 */
134 if (state.option.Fog != OPTION_NONE) {
135 static const GLenum fog_modes[4] = {
136 GL_NONE, GL_EXP, GL_EXP2, GL_LINEAR
137 };
138
139 /* XXX: we should somehow recompile this to remove clamping if disabled
140 * On the ATI driver, this is unclampled if fragment clamping is disabled
141 */
142 _mesa_append_fog_code(ctx, program, fog_modes[state.option.Fog], GL_TRUE);
143 }
144
145 #if DEBUG_FP
146 printf("____________Fragment program %u ________\n", program->Id);
147 _mesa_print_program(&program->Base);
148 #endif
149 }
150
151
152
153 /**
154 * Parse the vertex program string. If success, update the given
155 * vertex_program object with the new program. Else, leave the vertex_program
156 * object unchanged.
157 */
158 void
_mesa_parse_arb_vertex_program(struct gl_context * ctx,GLenum target,const GLvoid * str,GLsizei len,struct gl_program * program)159 _mesa_parse_arb_vertex_program(struct gl_context *ctx, GLenum target,
160 const GLvoid *str, GLsizei len,
161 struct gl_program *program)
162 {
163 struct gl_program prog;
164 struct asm_parser_state state;
165
166 assert(target == GL_VERTEX_PROGRAM_ARB);
167
168 memset(&prog, 0, sizeof(prog));
169 memset(&state, 0, sizeof(state));
170 state.prog = &prog;
171 state.mem_ctx = program;
172
173 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len,
174 &state)) {
175 _mesa_error(ctx, GL_INVALID_OPERATION, "glProgramString(bad program)");
176 return;
177 }
178
179 ralloc_free(program->String);
180
181 /* Copy the relevant contents of the arb_program struct into the
182 * vertex_program struct.
183 */
184 program->String = prog.String;
185 program->arb.NumInstructions = prog.arb.NumInstructions;
186 program->arb.NumTemporaries = prog.arb.NumTemporaries;
187 program->arb.NumParameters = prog.arb.NumParameters;
188 program->arb.NumAttributes = prog.arb.NumAttributes;
189 program->arb.NumAddressRegs = prog.arb.NumAddressRegs;
190 program->arb.NumNativeInstructions = prog.arb.NumNativeInstructions;
191 program->arb.NumNativeTemporaries = prog.arb.NumNativeTemporaries;
192 program->arb.NumNativeParameters = prog.arb.NumNativeParameters;
193 program->arb.NumNativeAttributes = prog.arb.NumNativeAttributes;
194 program->arb.NumNativeAddressRegs = prog.arb.NumNativeAddressRegs;
195 program->info.inputs_read = prog.info.inputs_read;
196 program->info.outputs_written = prog.info.outputs_written;
197 program->arb.IndirectRegisterFiles = prog.arb.IndirectRegisterFiles;
198 program->arb.IsPositionInvariant = (state.option.PositionInvariant)
199 ? GL_TRUE : GL_FALSE;
200
201 ralloc_free(program->arb.Instructions);
202 program->arb.Instructions = prog.arb.Instructions;
203
204 if (program->Parameters)
205 _mesa_free_parameter_list(program->Parameters);
206 program->Parameters = prog.Parameters;
207
208 #if DEBUG_VP
209 printf("____________Vertex program %u __________\n", program->Id);
210 _mesa_print_program(program);
211 #endif
212 }
213