Merge branch 'master' of git+ssh://znh@git.freedesktop.org/git/mesa/mesa into 965...
[mesa.git] / src / mesa / drivers / dri / i965 / brw_context.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
33 #include "brw_context.h"
34 #include "brw_aub.h"
35 #include "brw_defines.h"
36 #include "brw_draw.h"
37 #include "brw_vs.h"
38 #include "imports.h"
39 #include "intel_tex.h"
40 #include "intel_blit.h"
41 #include "intel_batchbuffer.h"
42
43 #include "utils.h"
44 #include "api_noop.h"
45 #include "vtxfmt.h"
46
47 #include "shader/shader_api.h"
48
49 /***************************************
50 * Mesa's Driver Functions
51 ***************************************/
52
53 static const struct dri_extension brw_extensions[] =
54 {
55 { "GL_ARB_depth_texture", NULL },
56 { "GL_ARB_fragment_program", NULL },
57 { "GL_ARB_shadow", NULL },
58 { "GL_EXT_shadow_funcs", NULL },
59 /* ARB extn won't work if not enabled */
60 { "GL_SGIX_depth_texture", NULL },
61 { "GL_ARB_texture_env_crossbar", NULL },
62 { NULL, NULL }
63 };
64
65 static void brwLinkProgram(GLcontext *ctx, GLuint program)
66 {
67 struct brw_context *brw = brw_context(ctx);
68 struct brw_vertex_program *vert_prog;
69 struct brw_fragment_program *frag_prog;
70 struct gl_shader_program *sh_prog;
71 _mesa_link_program(ctx, program);
72
73 sh_prog = _mesa_lookup_shader_program(ctx, program);
74 if (sh_prog) {
75 sh_prog->FragmentProgram =
76 _mesa_realloc(sh_prog->FragmentProgram,
77 sizeof(struct gl_fragment_program),
78 sizeof(struct brw_fragment_program));
79 frag_prog = (struct brw_fragment_program *)sh_prog->FragmentProgram;
80 frag_prog->id = brw->program_id++;
81 sh_prog->VertexProgram = _mesa_realloc(sh_prog->VertexProgram,
82 sizeof(struct gl_vertex_program),
83 sizeof(struct brw_vertex_program));
84 vert_prog = (struct brw_vertex_program *)sh_prog->VertexProgram;
85 vert_prog->id = brw->program_id++;
86 }
87 }
88
89 static void brwUseProgram(GLcontext *ctx, GLuint program)
90 {
91 struct brw_context *brw = brw_context(ctx);
92 struct gl_shader_program *sh_prog;
93 _mesa_use_program(ctx, program);
94 sh_prog = ctx->Shader.CurrentProgram;
95 if (sh_prog) {
96 ctx->VertexProgram.Enabled = GL_TRUE;
97 ctx->FragmentProgram.Enabled = GL_TRUE;
98 brw->attribs.VertexProgram->Current = sh_prog->VertexProgram;
99 brw->attribs.FragmentProgram->Current = sh_prog->FragmentProgram;
100 brw->state.dirty.brw |= BRW_NEW_VERTEX_PROGRAM;
101 brw->state.dirty.brw |= BRW_NEW_FRAGMENT_PROGRAM;
102 }
103 }
104
105 static void brwInitProgFuncs( struct dd_function_table *functions )
106 {
107 functions->UseProgram = brwUseProgram;
108 functions->LinkProgram = brwLinkProgram;
109 }
110 static void brwInitDriverFunctions( struct dd_function_table *functions )
111 {
112 intelInitDriverFunctions( functions );
113 brwInitTextureFuncs( functions );
114 brwInitFragProgFuncs( functions );
115 brwInitProgFuncs( functions );
116 }
117
118
119 static void brw_init_attribs( struct brw_context *brw )
120 {
121 GLcontext *ctx = &brw->intel.ctx;
122
123 brw->attribs.Color = &ctx->Color;
124 brw->attribs.Depth = &ctx->Depth;
125 brw->attribs.Fog = &ctx->Fog;
126 brw->attribs.Hint = &ctx->Hint;
127 brw->attribs.Light = &ctx->Light;
128 brw->attribs.Line = &ctx->Line;
129 brw->attribs.Point = &ctx->Point;
130 brw->attribs.Polygon = &ctx->Polygon;
131 brw->attribs.Scissor = &ctx->Scissor;
132 brw->attribs.Stencil = &ctx->Stencil;
133 brw->attribs.Texture = &ctx->Texture;
134 brw->attribs.Transform = &ctx->Transform;
135 brw->attribs.Viewport = &ctx->Viewport;
136 brw->attribs.VertexProgram = &ctx->VertexProgram;
137 brw->attribs.FragmentProgram = &ctx->FragmentProgram;
138 brw->attribs.PolygonStipple = &ctx->PolygonStipple[0];
139 }
140
141
142 GLboolean brwCreateContext( const __GLcontextModes *mesaVis,
143 __DRIcontextPrivate *driContextPriv,
144 void *sharedContextPrivate)
145 {
146 struct dd_function_table functions;
147 struct brw_context *brw = (struct brw_context *) CALLOC_STRUCT(brw_context);
148 struct intel_context *intel = &brw->intel;
149 GLcontext *ctx = &intel->ctx;
150
151 if (!brw) {
152 _mesa_printf("%s: failed to alloc context\n", __FUNCTION__);
153 return GL_FALSE;
154 }
155
156 brwInitVtbl( brw );
157 brwInitDriverFunctions( &functions );
158
159 if (!intelInitContext( intel, mesaVis, driContextPriv,
160 sharedContextPrivate, &functions )) {
161 _mesa_printf("%s: failed to init intel context\n", __FUNCTION__);
162 FREE(brw);
163 return GL_FALSE;
164 }
165
166 ctx->Const.MaxTextureUnits = BRW_MAX_TEX_UNIT;
167 ctx->Const.MaxTextureImageUnits = BRW_MAX_TEX_UNIT;
168 ctx->Const.MaxTextureCoordUnits = BRW_MAX_TEX_UNIT;
169
170
171 /* Advertise the full hardware capabilities. The new memory
172 * manager should cope much better with overload situations:
173 */
174 ctx->Const.MaxTextureLevels = 12;
175 ctx->Const.Max3DTextureLevels = 9;
176 ctx->Const.MaxCubeTextureLevels = 12;
177 ctx->Const.MaxTextureRectSize = (1<<11);
178 ctx->Const.MaxTextureUnits = BRW_MAX_TEX_UNIT;
179
180 /* ctx->Const.MaxNativeVertexProgramTemps = 32; */
181
182
183 driInitExtensions( ctx, brw_extensions, GL_FALSE );
184
185 brw_aub_init( brw );
186
187 brw_init_attribs( brw );
188 brw_init_metaops( brw );
189 brw_init_state( brw );
190
191 brw->state.dirty.mesa = ~0;
192 brw->state.dirty.brw = ~0;
193
194 memset(&brw->wm.bind, ~0, sizeof(brw->wm.bind));
195
196 brw->emit_state_always = 0;
197
198 ctx->FragmentProgram._MaintainTexEnvProgram = 1;
199
200 brw_draw_init( brw );
201
202 brw_ProgramCacheInit( ctx );
203
204 brw_FrameBufferTexInit( brw );
205
206 {
207 const char *filename = getenv("INTEL_REPLAY");
208 if (filename) {
209 brw_playback_aubfile(brw, filename);
210 exit(0);
211 }
212 }
213
214 return GL_TRUE;
215 }
216