Merge remote branch 'origin/master' into nv50-compiler
[mesa.git] / src / mesa / drivers / dri / i965 / brw_program.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32 #include "main/imports.h"
33 #include "main/enums.h"
34 #include "main/shaderobj.h"
35 #include "program/prog_parameter.h"
36 #include "program/program.h"
37 #include "program/programopt.h"
38 #include "tnl/tnl.h"
39 #include "talloc.h"
40
41 #include "brw_context.h"
42 #include "brw_wm.h"
43
44 static void brwBindProgram( GLcontext *ctx,
45 GLenum target,
46 struct gl_program *prog )
47 {
48 struct brw_context *brw = brw_context(ctx);
49
50 switch (target) {
51 case GL_VERTEX_PROGRAM_ARB:
52 brw->state.dirty.brw |= BRW_NEW_VERTEX_PROGRAM;
53 break;
54 case GL_FRAGMENT_PROGRAM_ARB:
55 brw->state.dirty.brw |= BRW_NEW_FRAGMENT_PROGRAM;
56 break;
57 }
58 }
59
60 static struct gl_program *brwNewProgram( GLcontext *ctx,
61 GLenum target,
62 GLuint id )
63 {
64 struct brw_context *brw = brw_context(ctx);
65
66 switch (target) {
67 case GL_VERTEX_PROGRAM_ARB: {
68 struct brw_vertex_program *prog = CALLOC_STRUCT(brw_vertex_program);
69 if (prog) {
70 prog->id = brw->program_id++;
71
72 return _mesa_init_vertex_program( ctx, &prog->program,
73 target, id );
74 }
75 else
76 return NULL;
77 }
78
79 case GL_FRAGMENT_PROGRAM_ARB: {
80 struct brw_fragment_program *prog = CALLOC_STRUCT(brw_fragment_program);
81 if (prog) {
82 prog->id = brw->program_id++;
83
84 return _mesa_init_fragment_program( ctx, &prog->program,
85 target, id );
86 }
87 else
88 return NULL;
89 }
90
91 default:
92 return _mesa_new_program(ctx, target, id);
93 }
94 }
95
96 static void brwDeleteProgram( GLcontext *ctx,
97 struct gl_program *prog )
98 {
99 _mesa_delete_program( ctx, prog );
100 }
101
102
103 static GLboolean brwIsProgramNative( GLcontext *ctx,
104 GLenum target,
105 struct gl_program *prog )
106 {
107 return GL_TRUE;
108 }
109
110 static void
111 shader_error(GLcontext *ctx, struct gl_program *prog, const char *msg)
112 {
113 struct gl_shader_program *shader;
114
115 shader = _mesa_lookup_shader_program(ctx, prog->Id);
116
117 if (shader) {
118 shader->InfoLog = talloc_strdup_append(shader->InfoLog, msg);
119 shader->LinkStatus = GL_FALSE;
120 }
121 }
122
123 static GLboolean brwProgramStringNotify( GLcontext *ctx,
124 GLenum target,
125 struct gl_program *prog )
126 {
127 struct brw_context *brw = brw_context(ctx);
128 int i;
129
130 if (target == GL_FRAGMENT_PROGRAM_ARB) {
131 struct gl_fragment_program *fprog = (struct gl_fragment_program *) prog;
132 struct brw_fragment_program *newFP = brw_fragment_program(fprog);
133 const struct brw_fragment_program *curFP =
134 brw_fragment_program_const(brw->fragment_program);
135
136 if (fprog->FogOption) {
137 _mesa_append_fog_code(ctx, fprog);
138 fprog->FogOption = GL_NONE;
139 }
140
141 if (newFP == curFP)
142 brw->state.dirty.brw |= BRW_NEW_FRAGMENT_PROGRAM;
143 newFP->id = brw->program_id++;
144 newFP->isGLSL = brw_wm_is_glsl(fprog);
145 }
146 else if (target == GL_VERTEX_PROGRAM_ARB) {
147 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
148 struct brw_vertex_program *newVP = brw_vertex_program(vprog);
149 const struct brw_vertex_program *curVP =
150 brw_vertex_program_const(brw->vertex_program);
151
152 if (newVP == curVP)
153 brw->state.dirty.brw |= BRW_NEW_VERTEX_PROGRAM;
154 if (newVP->program.IsPositionInvariant) {
155 _mesa_insert_mvp_code(ctx, &newVP->program);
156 }
157 newVP->id = brw->program_id++;
158
159 /* Also tell tnl about it:
160 */
161 _tnl_program_string(ctx, target, prog);
162 }
163
164 /* Reject programs with subroutines, which are totally broken at the moment
165 * (all program flows return when any program flow returns, and
166 * the VS also hangs if a function call calls a function.
167 *
168 * See piglit glsl-{vs,fs}-functions-[23] tests.
169 */
170 for (i = 0; i < prog->NumInstructions; i++) {
171 struct prog_instruction *inst = prog->Instructions + i;
172 int r;
173
174 if (prog->Instructions[i].Opcode == OPCODE_CAL) {
175 shader_error(ctx, prog,
176 "i965 driver doesn't yet support uninlined function "
177 "calls. Move to using a single return statement at "
178 "the end of the function to work around it.\n");
179 return GL_FALSE;
180 }
181
182 if (prog->Instructions[i].Opcode == OPCODE_RET) {
183 shader_error(ctx, prog,
184 "i965 driver doesn't yet support \"return\" "
185 "from main().\n");
186 return GL_FALSE;
187 }
188
189 for (r = 0; r < _mesa_num_inst_src_regs(inst->Opcode); r++) {
190 if (prog->Instructions[i].SrcReg[r].RelAddr &&
191 prog->Instructions[i].SrcReg[r].File == PROGRAM_INPUT) {
192 shader_error(ctx, prog,
193 "Variable indexing of shader inputs unsupported\n");
194 return GL_FALSE;
195 }
196 }
197
198 if (target == GL_FRAGMENT_PROGRAM_ARB &&
199 prog->Instructions[i].DstReg.RelAddr &&
200 prog->Instructions[i].DstReg.File == PROGRAM_OUTPUT) {
201 shader_error(ctx, prog,
202 "Variable indexing of FS outputs unsupported\n");
203 return GL_FALSE;
204 }
205 if (target == GL_FRAGMENT_PROGRAM_ARB) {
206 if ((prog->Instructions[i].DstReg.RelAddr &&
207 prog->Instructions[i].DstReg.File == PROGRAM_TEMPORARY) ||
208 (prog->Instructions[i].SrcReg[0].RelAddr &&
209 prog->Instructions[i].SrcReg[0].File == PROGRAM_TEMPORARY) ||
210 (prog->Instructions[i].SrcReg[1].RelAddr &&
211 prog->Instructions[i].SrcReg[1].File == PROGRAM_TEMPORARY) ||
212 (prog->Instructions[i].SrcReg[2].RelAddr &&
213 prog->Instructions[i].SrcReg[2].File == PROGRAM_TEMPORARY)) {
214 shader_error(ctx, prog,
215 "Variable indexing of variable arrays in the FS "
216 "unsupported\n");
217 return GL_FALSE;
218 }
219 }
220 }
221
222 return GL_TRUE;
223 }
224
225 void brwInitFragProgFuncs( struct dd_function_table *functions )
226 {
227 assert(functions->ProgramStringNotify == _tnl_program_string);
228
229 functions->BindProgram = brwBindProgram;
230 functions->NewProgram = brwNewProgram;
231 functions->DeleteProgram = brwDeleteProgram;
232 functions->IsProgramNative = brwIsProgramNative;
233 functions->ProgramStringNotify = brwProgramStringNotify;
234
235 functions->NewShader = brw_new_shader;
236 functions->NewShaderProgram = brw_new_shader_program;
237 functions->CompileShader = brw_compile_shader;
238 functions->LinkShader = brw_link_shader;
239 }
240