i965: Mostly fix glsl-max-varyings.
[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
40 #include "brw_context.h"
41 #include "brw_wm.h"
42
43 static void brwBindProgram( GLcontext *ctx,
44 GLenum target,
45 struct gl_program *prog )
46 {
47 struct brw_context *brw = brw_context(ctx);
48
49 switch (target) {
50 case GL_VERTEX_PROGRAM_ARB:
51 brw->state.dirty.brw |= BRW_NEW_VERTEX_PROGRAM;
52 break;
53 case GL_FRAGMENT_PROGRAM_ARB:
54 brw->state.dirty.brw |= BRW_NEW_FRAGMENT_PROGRAM;
55 break;
56 }
57 }
58
59 static struct gl_program *brwNewProgram( GLcontext *ctx,
60 GLenum target,
61 GLuint id )
62 {
63 struct brw_context *brw = brw_context(ctx);
64
65 switch (target) {
66 case GL_VERTEX_PROGRAM_ARB: {
67 struct brw_vertex_program *prog = CALLOC_STRUCT(brw_vertex_program);
68 if (prog) {
69 prog->id = brw->program_id++;
70
71 return _mesa_init_vertex_program( ctx, &prog->program,
72 target, id );
73 }
74 else
75 return NULL;
76 }
77
78 case GL_FRAGMENT_PROGRAM_ARB: {
79 struct brw_fragment_program *prog = CALLOC_STRUCT(brw_fragment_program);
80 if (prog) {
81 prog->id = brw->program_id++;
82
83 return _mesa_init_fragment_program( ctx, &prog->program,
84 target, id );
85 }
86 else
87 return NULL;
88 }
89
90 default:
91 return _mesa_new_program(ctx, target, id);
92 }
93 }
94
95 static void brwDeleteProgram( GLcontext *ctx,
96 struct gl_program *prog )
97 {
98 _mesa_delete_program( ctx, prog );
99 }
100
101
102 static GLboolean brwIsProgramNative( GLcontext *ctx,
103 GLenum target,
104 struct gl_program *prog )
105 {
106 return GL_TRUE;
107 }
108
109 static void
110 shader_error(GLcontext *ctx, struct gl_program *prog, const char *msg)
111 {
112 struct gl_shader_program *shader;
113
114 shader = _mesa_lookup_shader_program(ctx, prog->Id);
115
116 if (shader) {
117 if (shader->InfoLog) {
118 free(shader->InfoLog);
119 }
120 shader->InfoLog = _mesa_strdup(msg);
121 shader->LinkStatus = GL_FALSE;
122 }
123 }
124
125 static GLboolean brwProgramStringNotify( GLcontext *ctx,
126 GLenum target,
127 struct gl_program *prog )
128 {
129 struct brw_context *brw = brw_context(ctx);
130 int i;
131
132 if (target == GL_FRAGMENT_PROGRAM_ARB) {
133 struct gl_fragment_program *fprog = (struct gl_fragment_program *) prog;
134 struct brw_fragment_program *newFP = brw_fragment_program(fprog);
135 const struct brw_fragment_program *curFP =
136 brw_fragment_program_const(brw->fragment_program);
137
138 if (fprog->FogOption) {
139 _mesa_append_fog_code(ctx, fprog);
140 fprog->FogOption = GL_NONE;
141 }
142
143 if (newFP == curFP)
144 brw->state.dirty.brw |= BRW_NEW_FRAGMENT_PROGRAM;
145 newFP->id = brw->program_id++;
146 newFP->isGLSL = brw_wm_is_glsl(fprog);
147 }
148 else if (target == GL_VERTEX_PROGRAM_ARB) {
149 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
150 struct brw_vertex_program *newVP = brw_vertex_program(vprog);
151 const struct brw_vertex_program *curVP =
152 brw_vertex_program_const(brw->vertex_program);
153
154 if (newVP == curVP)
155 brw->state.dirty.brw |= BRW_NEW_VERTEX_PROGRAM;
156 if (newVP->program.IsPositionInvariant) {
157 _mesa_insert_mvp_code(ctx, &newVP->program);
158 }
159 newVP->id = brw->program_id++;
160
161 /* Also tell tnl about it:
162 */
163 _tnl_program_string(ctx, target, prog);
164 }
165
166 /* Reject programs with subroutines, which are totally broken at the moment
167 * (all program flows return when any program flow returns, and
168 * the VS also hangs if a function call calls a function.
169 *
170 * See piglit glsl-{vs,fs}-functions-[23] tests.
171 */
172 for (i = 0; i < prog->NumInstructions; i++) {
173 if (prog->Instructions[i].Opcode == OPCODE_CAL) {
174 shader_error(ctx, prog,
175 "i965 driver doesn't yet support uninlined function "
176 "calls. Move to using a single return statement at "
177 "the end of the function to work around it.");
178 return GL_FALSE;
179 }
180 }
181
182 return GL_TRUE;
183 }
184
185 void brwInitFragProgFuncs( struct dd_function_table *functions )
186 {
187 assert(functions->ProgramStringNotify == _tnl_program_string);
188
189 functions->BindProgram = brwBindProgram;
190 functions->NewProgram = brwNewProgram;
191 functions->DeleteProgram = brwDeleteProgram;
192 functions->IsProgramNative = brwIsProgramNative;
193 functions->ProgramStringNotify = brwProgramStringNotify;
194 }
195