• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SAMPLER    - texture sampler
42   PROGRAM_CONSTANT   - indexes into program->Parameters, a known constant/literal
43   PROGRAM_STATE_VAR  - indexes into program->Parameters, and may actually be:
44                        + a state variable, like "state.fog.color", or
45                        + a pointer to a "program.local[k]" parameter, or
46                        + a pointer to a "program.env[k]" parameter
47 
48 Basically, all the program.local[] and program.env[] values will get mapped
49 into the unified gl_program->Parameters array.  This solves the problem of
50 having three separate program parameter arrays.
51 */
52 
53 
54 #include "main/glheader.h"
55 #include "main/imports.h"
56 #include "main/context.h"
57 #include "main/mtypes.h"
58 #include "arbprogparse.h"
59 #include "programopt.h"
60 #include "prog_parameter.h"
61 #include "prog_statevars.h"
62 #include "prog_instruction.h"
63 #include "prog_optimize.h"
64 #include "program_parser.h"
65 
66 
67 void
_mesa_parse_arb_fragment_program(struct gl_context * ctx,GLenum target,const GLvoid * str,GLsizei len,struct gl_program * program)68 _mesa_parse_arb_fragment_program(struct gl_context* ctx, GLenum target,
69                                  const GLvoid *str, GLsizei len,
70                                  struct gl_program *program)
71 {
72    struct gl_program prog;
73    struct asm_parser_state state;
74    GLuint i;
75 
76    assert(target == GL_FRAGMENT_PROGRAM_ARB);
77 
78    memset(&prog, 0, sizeof(prog));
79    memset(&state, 0, sizeof(state));
80    state.prog = &prog;
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->OriginUpperLeft = state.option.OriginUpperLeft;
120    program->PixelCenterInteger = state.option.PixelCenterInteger;
121 
122    program->info.fs.uses_discard = state.fragment.UsesKill;
123 
124    ralloc_free(program->arb.Instructions);
125    program->arb.Instructions = prog.arb.Instructions;
126 
127    if (program->Parameters)
128       _mesa_free_parameter_list(program->Parameters);
129    program->Parameters    = prog.Parameters;
130 
131    /* Append fog instructions now if the program has "OPTION ARB_fog_exp"
132     * or similar.  We used to leave this up to drivers, but it appears
133     * there's no hardware that wants to do fog in a discrete stage separate
134     * from the fragment shader.
135     */
136    if (state.option.Fog != OPTION_NONE) {
137       static const GLenum fog_modes[4] = {
138 	 GL_NONE, GL_EXP, GL_EXP2, GL_LINEAR
139       };
140 
141       /* XXX: we should somehow recompile this to remove clamping if disabled
142        * On the ATI driver, this is unclampled if fragment clamping is disabled
143        */
144       _mesa_append_fog_code(ctx, program, fog_modes[state.option.Fog], GL_TRUE);
145    }
146 
147 #if DEBUG_FP
148    printf("____________Fragment program %u ________\n", program->Id);
149    _mesa_print_program(&program->Base);
150 #endif
151 }
152 
153 
154 
155 /**
156  * Parse the vertex program string.  If success, update the given
157  * vertex_program object with the new program.  Else, leave the vertex_program
158  * object unchanged.
159  */
160 void
_mesa_parse_arb_vertex_program(struct gl_context * ctx,GLenum target,const GLvoid * str,GLsizei len,struct gl_program * program)161 _mesa_parse_arb_vertex_program(struct gl_context *ctx, GLenum target,
162 			       const GLvoid *str, GLsizei len,
163 			       struct gl_program *program)
164 {
165    struct gl_program prog;
166    struct asm_parser_state state;
167 
168    assert(target == GL_VERTEX_PROGRAM_ARB);
169 
170    memset(&prog, 0, sizeof(prog));
171    memset(&state, 0, sizeof(state));
172    state.prog = &prog;
173    state.mem_ctx = program;
174 
175    if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len,
176 				&state)) {
177       ralloc_free(prog.arb.Instructions);
178       ralloc_free(prog.String);
179       _mesa_error(ctx, GL_INVALID_OPERATION, "glProgramString(bad program)");
180       return;
181    }
182 
183    if ((ctx->_Shader->Flags & GLSL_NO_OPT) == 0)
184       _mesa_optimize_program(ctx, &prog, program);
185 
186    ralloc_free(program->String);
187 
188    /* Copy the relevant contents of the arb_program struct into the
189     * vertex_program struct.
190     */
191    program->String          = prog.String;
192    program->arb.NumInstructions = prog.arb.NumInstructions;
193    program->arb.NumTemporaries  = prog.arb.NumTemporaries;
194    program->arb.NumParameters   = prog.arb.NumParameters;
195    program->arb.NumAttributes   = prog.arb.NumAttributes;
196    program->arb.NumAddressRegs  = prog.arb.NumAddressRegs;
197    program->arb.NumNativeInstructions = prog.arb.NumNativeInstructions;
198    program->arb.NumNativeTemporaries = prog.arb.NumNativeTemporaries;
199    program->arb.NumNativeParameters = prog.arb.NumNativeParameters;
200    program->arb.NumNativeAttributes = prog.arb.NumNativeAttributes;
201    program->arb.NumNativeAddressRegs = prog.arb.NumNativeAddressRegs;
202    program->info.inputs_read     = prog.info.inputs_read;
203    program->info.outputs_written = prog.info.outputs_written;
204    program->arb.IndirectRegisterFiles = prog.arb.IndirectRegisterFiles;
205    program->arb.IsPositionInvariant = (state.option.PositionInvariant)
206       ? GL_TRUE : GL_FALSE;
207 
208    ralloc_free(program->arb.Instructions);
209    program->arb.Instructions = prog.arb.Instructions;
210 
211    if (program->Parameters)
212       _mesa_free_parameter_list(program->Parameters);
213    program->Parameters = prog.Parameters;
214 
215 #if DEBUG_VP
216    printf("____________Vertex program %u __________\n", program->Id);
217    _mesa_print_program(program);
218 #endif
219 }
220