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.NumAluInstructions = prog.arb.NumAluInstructions;
100 program->arb.NumTexInstructions = prog.arb.NumTexInstructions;
101 program->arb.NumTexIndirections = prog.arb.NumTexIndirections;
102 program->info.inputs_read = prog.info.inputs_read;
103 program->info.outputs_written = prog.info.outputs_written;
104 program->arb.IndirectRegisterFiles = prog.arb.IndirectRegisterFiles;
105 for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++) {
106 program->TexturesUsed[i] = prog.TexturesUsed[i];
107 if (prog.TexturesUsed[i])
108 program->SamplersUsed |= (1 << i);
109 }
110 program->ShadowSamplers = prog.ShadowSamplers;
111 program->info.fs.origin_upper_left = state.option.OriginUpperLeft;
112 program->info.fs.pixel_center_integer = state.option.PixelCenterInteger;
113
114 program->info.fs.uses_discard = state.fragment.UsesKill;
115 program->arb.Fog = state.option.Fog;
116
117 ralloc_free(program->arb.Instructions);
118 program->arb.Instructions = prog.arb.Instructions;
119
120 if (program->Parameters)
121 _mesa_free_parameter_list(program->Parameters);
122 program->Parameters = prog.Parameters;
123
124 #if DEBUG_FP
125 printf("____________Fragment program %u ________\n", program->Id);
126 _mesa_print_program(program);
127 #endif
128 }
129
130
131
132 /**
133 * Parse the vertex program string. If success, update the given
134 * vertex_program object with the new program. Else, leave the vertex_program
135 * object unchanged.
136 */
137 void
_mesa_parse_arb_vertex_program(struct gl_context * ctx,GLenum target,const GLvoid * str,GLsizei len,struct gl_program * program)138 _mesa_parse_arb_vertex_program(struct gl_context *ctx, GLenum target,
139 const GLvoid *str, GLsizei len,
140 struct gl_program *program)
141 {
142 struct gl_program prog;
143 struct asm_parser_state state;
144
145 assert(target == GL_VERTEX_PROGRAM_ARB);
146
147 memset(&prog, 0, sizeof(prog));
148 memset(&state, 0, sizeof(state));
149 state.prog = &prog;
150 state.mem_ctx = program;
151
152 if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len,
153 &state)) {
154 _mesa_error(ctx, GL_INVALID_OPERATION, "glProgramString(bad program)");
155 return;
156 }
157
158 ralloc_free(program->String);
159
160 /* Copy the relevant contents of the arb_program struct into the
161 * vertex_program struct.
162 */
163 program->String = prog.String;
164 program->arb.NumInstructions = prog.arb.NumInstructions;
165 program->arb.NumTemporaries = prog.arb.NumTemporaries;
166 program->arb.NumParameters = prog.arb.NumParameters;
167 program->arb.NumAttributes = prog.arb.NumAttributes;
168 program->arb.NumAddressRegs = prog.arb.NumAddressRegs;
169 program->info.inputs_read = prog.info.inputs_read;
170 program->info.outputs_written = prog.info.outputs_written;
171 program->arb.IndirectRegisterFiles = prog.arb.IndirectRegisterFiles;
172 program->arb.IsPositionInvariant = (state.option.PositionInvariant)
173 ? GL_TRUE : GL_FALSE;
174
175 ralloc_free(program->arb.Instructions);
176 program->arb.Instructions = prog.arb.Instructions;
177
178 if (program->Parameters)
179 _mesa_free_parameter_list(program->Parameters);
180 program->Parameters = prog.Parameters;
181
182 #if DEBUG_VP
183 printf("____________Vertex program %u __________\n", program->Id);
184 _mesa_print_program(program);
185 #endif
186 }
187