OSDN Git Service

Merge branch 'master' into asm-shader-rework-1
[android-x86/external-mesa.git] / src / mesa / shader / arbprogparse.c
1 /*
2  * Mesa 3-D graphics library
3  * Version:  7.1
4  *
5  * Copyright (C) 1999-2008  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 #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/macros.h"
58 #include "main/mtypes.h"
59 #include "shader/grammar/grammar_mesa.h"
60 #include "arbprogparse.h"
61 #include "program.h"
62 #include "programopt.h"
63 #include "prog_parameter.h"
64 #include "prog_statevars.h"
65 #include "prog_instruction.h"
66 #include "program_parser.h"
67
68
69 void
70 _mesa_parse_arb_fragment_program(GLcontext* ctx, GLenum target,
71                                  const GLvoid *str, GLsizei len,
72                                  struct gl_fragment_program *program)
73 {
74    struct gl_program prog;
75    struct asm_parser_state state;
76    GLuint i;
77
78    ASSERT(target == GL_FRAGMENT_PROGRAM_ARB);
79
80    memset(&prog, 0, sizeof(prog));
81    memset(&state, 0, sizeof(state));
82    state.prog = &prog;
83
84    if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len,
85                                 &state)) {
86       /* Error in the program. Just return. */
87       return;
88    }
89
90    /* Copy the relevant contents of the arb_program struct into the
91     * fragment_program struct.
92     */
93    program->Base.String          = prog.String;
94    program->Base.NumInstructions = prog.NumInstructions;
95    program->Base.NumTemporaries  = prog.NumTemporaries;
96    program->Base.NumParameters   = prog.NumParameters;
97    program->Base.NumAttributes   = prog.NumAttributes;
98    program->Base.NumAddressRegs  = prog.NumAddressRegs;
99    program->Base.NumNativeInstructions = prog.NumNativeInstructions;
100    program->Base.NumNativeTemporaries = prog.NumNativeTemporaries;
101    program->Base.NumNativeParameters = prog.NumNativeParameters;
102    program->Base.NumNativeAttributes = prog.NumNativeAttributes;
103    program->Base.NumNativeAddressRegs = prog.NumNativeAddressRegs;
104    program->Base.NumAluInstructions   = prog.NumAluInstructions;
105    program->Base.NumTexInstructions   = prog.NumTexInstructions;
106    program->Base.NumTexIndirections   = prog.NumTexIndirections;
107    program->Base.NumNativeAluInstructions = prog.NumAluInstructions;
108    program->Base.NumNativeTexInstructions = prog.NumTexInstructions;
109    program->Base.NumNativeTexIndirections = prog.NumTexIndirections;
110    program->Base.InputsRead      = prog.InputsRead;
111    program->Base.OutputsWritten  = prog.OutputsWritten;
112    for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++) {
113       program->Base.TexturesUsed[i] = prog.TexturesUsed[i];
114       if (prog.TexturesUsed[i])
115          program->Base.SamplersUsed |= (1 << i);
116    }
117    program->Base.ShadowSamplers = prog.ShadowSamplers;
118    switch (state.option.Fog) {
119    case OPTION_FOG_EXP:    program->FogOption = GL_EXP;    break;
120    case OPTION_FOG_EXP2:   program->FogOption = GL_EXP2;   break;
121    case OPTION_FOG_LINEAR: program->FogOption = GL_LINEAR; break;
122    default:                program->FogOption = GL_NONE;   break;
123    }
124
125    program->UsesKill            = state.fragment.UsesKill;
126
127    if (program->FogOption)
128       program->Base.InputsRead |= FRAG_BIT_FOGC;
129
130    if (program->Base.Instructions)
131       _mesa_free(program->Base.Instructions);
132    program->Base.Instructions = prog.Instructions;
133
134    if (program->Base.Parameters)
135       _mesa_free_parameter_list(program->Base.Parameters);
136    program->Base.Parameters    = prog.Parameters;
137
138    /* Append fog instructions now if the program has "OPTION ARB_fog_exp"
139     * or similar.  We used to leave this up to drivers, but it appears
140     * there's no hardware that wants to do fog in a discrete stage separate
141     * from the fragment shader.
142     */
143    if (program->FogOption != GL_NONE) {
144       _mesa_append_fog_code(ctx, program);
145       program->FogOption = GL_NONE;
146    }
147
148 #if DEBUG_FP
149    _mesa_printf("____________Fragment program %u ________\n", program->Base.Id);
150    _mesa_print_program(&program->Base);
151 #endif
152 }
153
154
155
156 /**
157  * Parse the vertex program string.  If success, update the given
158  * vertex_program object with the new program.  Else, leave the vertex_program
159  * object unchanged.
160  */
161 void
162 _mesa_parse_arb_vertex_program(GLcontext *ctx, GLenum target,
163                                const GLvoid *str, GLsizei len,
164                                struct gl_vertex_program *program)
165 {
166    struct gl_program prog;
167    struct asm_parser_state state;
168
169    ASSERT(target == GL_VERTEX_PROGRAM_ARB);
170
171    memset(&prog, 0, sizeof(prog));
172    memset(&state, 0, sizeof(state));
173    state.prog = &prog;
174
175    if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len,
176                                 &state)) {
177       _mesa_error(ctx, GL_INVALID_OPERATION, "glProgramString(bad program)");
178       return;
179    }
180
181    /* Copy the relevant contents of the arb_program struct into the 
182     * vertex_program struct.
183     */
184    program->Base.String          = prog.String;
185    program->Base.NumInstructions = prog.NumInstructions;
186    program->Base.NumTemporaries  = prog.NumTemporaries;
187    program->Base.NumParameters   = prog.NumParameters;
188    program->Base.NumAttributes   = prog.NumAttributes;
189    program->Base.NumAddressRegs  = prog.NumAddressRegs;
190    program->Base.NumNativeInstructions = prog.NumNativeInstructions;
191    program->Base.NumNativeTemporaries = prog.NumNativeTemporaries;
192    program->Base.NumNativeParameters = prog.NumNativeParameters;
193    program->Base.NumNativeAttributes = prog.NumNativeAttributes;
194    program->Base.NumNativeAddressRegs = prog.NumNativeAddressRegs;
195    program->Base.InputsRead     = prog.InputsRead;
196    program->Base.OutputsWritten = prog.OutputsWritten;
197    program->IsPositionInvariant = (state.option.PositionInvariant)
198       ? GL_TRUE : GL_FALSE;
199
200    if (program->Base.Instructions)
201       _mesa_free(program->Base.Instructions);
202    program->Base.Instructions = prog.Instructions;
203
204    if (program->Base.Parameters)
205       _mesa_free_parameter_list(program->Base.Parameters);
206    program->Base.Parameters = prog.Parameters; 
207
208 #if DEBUG_VP
209    _mesa_printf("____________Vertex program %u __________\n", program->Base.Id);
210    _mesa_print_program(&program->Base);
211 #endif
212 }