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 "glsl/ralloc.h"
40 
41 #include "brw_context.h"
42 #include "brw_wm.h"
43 
brwBindProgram(struct gl_context * ctx,GLenum target,struct gl_program * prog)44 static void brwBindProgram( struct gl_context *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 
brwNewProgram(struct gl_context * ctx,GLenum target,GLuint id)60 static struct gl_program *brwNewProgram( struct gl_context *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 
brwDeleteProgram(struct gl_context * ctx,struct gl_program * prog)96 static void brwDeleteProgram( struct gl_context *ctx,
97 			      struct gl_program *prog )
98 {
99    _mesa_delete_program( ctx, prog );
100 }
101 
102 
103 static GLboolean
brwIsProgramNative(struct gl_context * ctx,GLenum target,struct gl_program * prog)104 brwIsProgramNative(struct gl_context *ctx,
105 		   GLenum target,
106 		   struct gl_program *prog)
107 {
108    return true;
109 }
110 
111 static void
shader_error(struct gl_context * ctx,struct gl_program * prog,const char * msg)112 shader_error(struct gl_context *ctx, struct gl_program *prog, const char *msg)
113 {
114    struct gl_shader_program *shader;
115 
116    shader = _mesa_lookup_shader_program(ctx, prog->Id);
117 
118    if (shader) {
119       ralloc_strcat(&shader->InfoLog, msg);
120       shader->LinkStatus = false;
121    }
122 }
123 
124 static GLboolean
brwProgramStringNotify(struct gl_context * ctx,GLenum target,struct gl_program * prog)125 brwProgramStringNotify(struct gl_context *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       struct gl_shader_program *shader_program;
138 
139       if (newFP == curFP)
140 	 brw->state.dirty.brw |= BRW_NEW_FRAGMENT_PROGRAM;
141       newFP->id = brw->program_id++;
142 
143       /* Don't reject fragment shaders for their Mesa IR state when we're
144        * using the new FS backend.
145        */
146       shader_program = _mesa_lookup_shader_program(ctx, prog->Id);
147       if (shader_program
148 	  && shader_program->_LinkedShaders[MESA_SHADER_FRAGMENT]) {
149 	 return true;
150       }
151    }
152    else if (target == GL_VERTEX_PROGRAM_ARB) {
153       struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
154       struct brw_vertex_program *newVP = brw_vertex_program(vprog);
155       const struct brw_vertex_program *curVP =
156          brw_vertex_program_const(brw->vertex_program);
157 
158       if (newVP == curVP)
159 	 brw->state.dirty.brw |= BRW_NEW_VERTEX_PROGRAM;
160       if (newVP->program.IsPositionInvariant) {
161 	 _mesa_insert_mvp_code(ctx, &newVP->program);
162       }
163       newVP->id = brw->program_id++;
164 
165       /* Also tell tnl about it:
166        */
167       _tnl_program_string(ctx, target, prog);
168    }
169 
170    /* Reject programs with subroutines, which are totally broken at the moment
171     * (all program flows return when any program flow returns, and
172     * the VS also hangs if a function call calls a function.
173     *
174     * See piglit glsl-{vs,fs}-functions-[23] tests.
175     */
176    for (i = 0; i < prog->NumInstructions; i++) {
177       struct prog_instruction *inst = prog->Instructions + i;
178       int r;
179 
180       if (prog->Instructions[i].Opcode == OPCODE_CAL) {
181 	 shader_error(ctx, prog,
182 		      "i965 driver doesn't yet support uninlined function "
183 		      "calls.  Move to using a single return statement at "
184 		      "the end of the function to work around it.\n");
185 	 return false;
186       }
187 
188       if (prog->Instructions[i].Opcode == OPCODE_RET) {
189 	 shader_error(ctx, prog,
190 		      "i965 driver doesn't yet support \"return\" "
191 		      "from main().\n");
192 	 return false;
193       }
194 
195       for (r = 0; r < _mesa_num_inst_src_regs(inst->Opcode); r++) {
196 	 if (prog->Instructions[i].SrcReg[r].RelAddr &&
197 	     prog->Instructions[i].SrcReg[r].File == PROGRAM_INPUT) {
198 	    shader_error(ctx, prog,
199 			 "Variable indexing of shader inputs unsupported\n");
200 	    return false;
201 	 }
202       }
203 
204       if (target == GL_FRAGMENT_PROGRAM_ARB &&
205 	  prog->Instructions[i].DstReg.RelAddr &&
206 	  prog->Instructions[i].DstReg.File == PROGRAM_OUTPUT) {
207 	 shader_error(ctx, prog,
208 		      "Variable indexing of FS outputs unsupported\n");
209 	 return false;
210       }
211       if (target == GL_FRAGMENT_PROGRAM_ARB) {
212 	 if ((prog->Instructions[i].DstReg.RelAddr &&
213 	      prog->Instructions[i].DstReg.File == PROGRAM_TEMPORARY) ||
214 	     (prog->Instructions[i].SrcReg[0].RelAddr &&
215 	      prog->Instructions[i].SrcReg[0].File == PROGRAM_TEMPORARY) ||
216 	     (prog->Instructions[i].SrcReg[1].RelAddr &&
217 	      prog->Instructions[i].SrcReg[1].File == PROGRAM_TEMPORARY) ||
218 	     (prog->Instructions[i].SrcReg[2].RelAddr &&
219 	      prog->Instructions[i].SrcReg[2].File == PROGRAM_TEMPORARY)) {
220 	    shader_error(ctx, prog,
221 			 "Variable indexing of variable arrays in the FS "
222 			 "unsupported\n");
223 	    return false;
224 	 }
225       }
226    }
227 
228    return true;
229 }
230 
231 /* Per-thread scratch space is a power-of-two multiple of 1KB. */
232 int
brw_get_scratch_size(int size)233 brw_get_scratch_size(int size)
234 {
235    int i;
236 
237    for (i = 1024; i < size; i *= 2)
238       ;
239 
240    return i;
241 }
242 
243 void
brw_get_scratch_bo(struct intel_context * intel,drm_intel_bo ** scratch_bo,int size)244 brw_get_scratch_bo(struct intel_context *intel,
245 		   drm_intel_bo **scratch_bo, int size)
246 {
247    drm_intel_bo *old_bo = *scratch_bo;
248 
249    if (old_bo && old_bo->size < size) {
250       drm_intel_bo_unreference(old_bo);
251       old_bo = NULL;
252    }
253 
254    if (!old_bo) {
255       *scratch_bo = drm_intel_bo_alloc(intel->bufmgr, "scratch bo", size, 4096);
256    }
257 }
258 
brwInitFragProgFuncs(struct dd_function_table * functions)259 void brwInitFragProgFuncs( struct dd_function_table *functions )
260 {
261    assert(functions->ProgramStringNotify == _tnl_program_string);
262 
263    functions->BindProgram = brwBindProgram;
264    functions->NewProgram = brwNewProgram;
265    functions->DeleteProgram = brwDeleteProgram;
266    functions->IsProgramNative = brwIsProgramNative;
267    functions->ProgramStringNotify = brwProgramStringNotify;
268 
269    functions->NewShader = brw_new_shader;
270    functions->NewShaderProgram = brw_new_shader_program;
271    functions->LinkShader = brw_link_shader;
272 }
273 
274