i965g: work in progress on fragment shaders
[mesa.git] / src / gallium / drivers / i965 / brw_pipe_shader.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 "brw_context.h"
33 #include "brw_util.h"
34 #include "brw_wm.h"
35
36
37 /**
38 * Determine if the given fragment program uses GLSL features such
39 * as flow conditionals, loops, subroutines.
40 * Some GLSL shaders may use these features, others might not.
41 */
42 GLboolean brw_wm_has_flow_control(const struct brw_fragment_shader *fp)
43 {
44 return (fp->info.insn_count[TGSI_OPCODE_ARL] > 0 ||
45 fp->info.insn_count[TGSI_OPCODE_IF] > 0 ||
46 fp->info.insn_count[TGSI_OPCODE_ENDIF] > 0 || /* redundant - IF */
47 fp->info.insn_count[TGSI_OPCODE_CAL] > 0 ||
48 fp->info.insn_count[TGSI_OPCODE_BRK] > 0 || /* redundant - BGNLOOP */
49 fp->info.insn_count[TGSI_OPCODE_RET] > 0 || /* redundant - CAL */
50 fp->info.insn_count[TGSI_OPCODE_BGNLOOP] > 0);
51 }
52
53
54
55 static void brwBindProgram( struct brw_context *brw,
56 GLenum target,
57 struct gl_program *prog )
58 {
59 struct brw_context *brw = brw_context(ctx);
60
61 switch (target) {
62 case GL_VERTEX_PROGRAM_ARB:
63 brw->state.dirty.brw |= BRW_NEW_VERTEX_PROGRAM;
64 break;
65 case GL_FRAGMENT_PROGRAM_ARB:
66 brw->state.dirty.brw |= BRW_NEW_FRAGMENT_PROGRAM;
67 break;
68 }
69 }
70
71 static struct gl_program *brwNewProgram( structg brw_context *brw,
72 GLenum target,
73 GLuint id )
74 {
75 struct brw_context *brw = brw_context(ctx);
76
77 switch (target) {
78 case GL_VERTEX_PROGRAM_ARB: {
79 struct brw_vertex_program *prog = CALLOC_STRUCT(brw_vertex_program);
80 if (prog) {
81 prog->id = brw->program_id++;
82
83 return _mesa_init_vertex_program( ctx, &prog->program,
84 target, id );
85 }
86 else
87 return NULL;
88 }
89
90 case GL_FRAGMENT_PROGRAM_ARB: {
91 struct brw_fragment_program *prog = CALLOC_STRUCT(brw_fragment_program);
92 if (prog) {
93 prog->id = brw->program_id++;
94
95 return _mesa_init_fragment_program( ctx, &prog->program,
96 target, id );
97 }
98 else
99 return NULL;
100 }
101
102 default:
103 return _mesa_new_program(ctx, target, id);
104 }
105 }
106
107 static void brwDeleteProgram( struct brw_context *brw,
108 struct gl_program *prog )
109 {
110 if (prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
111 struct gl_fragment_program *fprog = (struct gl_fragment_program *) prog;
112 struct brw_fragment_program *brw_fprog = brw_fragment_program(fprog);
113 brw->sws->bo_unreference(brw_fprog->const_buffer);
114 }
115
116 _mesa_delete_program( ctx, prog );
117 }
118
119
120 static GLboolean brwIsProgramNative( struct brw_context *brw,
121 GLenum target,
122 struct gl_program *prog )
123 {
124 return GL_TRUE;
125 }
126
127 static void brwProgramStringNotify( struct brw_context *brw,
128 GLenum target,
129 struct gl_program *prog )
130 {
131 struct brw_context *brw = brw_context(ctx);
132
133 if (target == GL_FRAGMENT_PROGRAM_ARB) {
134 struct gl_fragment_program *fprog = (struct gl_fragment_program *) prog;
135 struct brw_fragment_program *newFP = brw_fragment_program(fprog);
136 const struct brw_fragment_program *curFP =
137 brw_fragment_program_const(brw->fragment_program);
138
139 if (fprog->FogOption) {
140 _mesa_append_fog_code(ctx, fprog);
141 fprog->FogOption = GL_NONE;
142 }
143
144 if (newFP == curFP)
145 brw->state.dirty.brw |= BRW_NEW_FRAGMENT_PROGRAM;
146 newFP->id = brw->program_id++;
147 newFP->has_flow_control = brw_wm_has_flow_control(fprog);
148 }
149 else if (target == GL_VERTEX_PROGRAM_ARB) {
150 struct gl_vertex_program *vprog = (struct gl_vertex_program *) prog;
151 struct brw_vertex_program *newVP = brw_vertex_program(vprog);
152 const struct brw_vertex_program *curVP =
153 brw_vertex_program_const(brw->vertex_program);
154
155 if (newVP == curVP)
156 brw->state.dirty.brw |= BRW_NEW_VERTEX_PROGRAM;
157 if (newVP->program.IsPositionInvariant) {
158 _mesa_insert_mvp_code(ctx, &newVP->program);
159 }
160 newVP->id = brw->program_id++;
161
162 /* Also tell tnl about it:
163 */
164 _tnl_program_string(ctx, target, prog);
165 }
166 }
167
168 void brwInitFragProgFuncs( struct dd_function_table *functions )
169 {
170 assert(functions->ProgramStringNotify == _tnl_program_string);
171
172 functions->BindProgram = brwBindProgram;
173 functions->NewProgram = brwNewProgram;
174 functions->DeleteProgram = brwDeleteProgram;
175 functions->IsProgramNative = brwIsProgramNative;
176 functions->ProgramStringNotify = brwProgramStringNotify;
177 }
178