i915_context.c revision c1d4d4999303f9167b20f4e0674b9436e6295cf7
1/**************************************************************************
2 *
3 * Copyright 2003 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * 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, sub license, 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 portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28#include "i915_context.h"
29#include "main/api_exec.h"
30#include "main/imports.h"
31#include "main/macros.h"
32#include "main/version.h"
33#include "main/vtxfmt.h"
34#include "intel_chipset.h"
35#include "intel_tris.h"
36#include "tnl/t_context.h"
37#include "tnl/t_pipeline.h"
38#include "tnl/t_vertex.h"
39
40#include "swrast/swrast.h"
41#include "swrast_setup/swrast_setup.h"
42#include "tnl/tnl.h"
43#include "../glsl/ralloc.h"
44
45#include "i915_reg.h"
46#include "i915_program.h"
47
48/***************************************
49 * Mesa's Driver Functions
50 ***************************************/
51
52/* Override intel default.
53 */
54static void
55i915InvalidateState(struct gl_context * ctx, GLuint new_state)
56{
57   _swrast_InvalidateState(ctx, new_state);
58   _swsetup_InvalidateState(ctx, new_state);
59   _vbo_InvalidateState(ctx, new_state);
60   _tnl_InvalidateState(ctx, new_state);
61   _tnl_invalidate_vertex_state(ctx, new_state);
62   intel_context(ctx)->NewGLState |= new_state;
63
64   /* Todo: gather state values under which tracked parameters become
65    * invalidated, add callbacks for things like
66    * ProgramLocalParameters, etc.
67    */
68   {
69      struct i915_fragment_program *p =
70         (struct i915_fragment_program *) ctx->FragmentProgram._Current;
71      if (p && p->nr_params)
72         p->params_uptodate = 0;
73   }
74
75   if (new_state & (_NEW_STENCIL | _NEW_BUFFERS | _NEW_POLYGON))
76      i915_update_stencil(ctx);
77   if (new_state & (_NEW_LIGHT))
78       i915_update_provoking_vertex(ctx);
79   if (new_state & (_NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS))
80       i915_update_program(ctx);
81   if (new_state & (_NEW_PROGRAM | _NEW_POINT))
82       i915_update_sprite_point_enable(ctx);
83}
84
85
86static void
87i915InitDriverFunctions(struct dd_function_table *functions)
88{
89   intelInitDriverFunctions(functions);
90   i915InitStateFunctions(functions);
91   i915InitFragProgFuncs(functions);
92   functions->UpdateState = i915InvalidateState;
93}
94
95/* Note: this is shared with i830. */
96void
97intel_init_texture_formats(struct gl_context *ctx)
98{
99   struct intel_context *intel = intel_context(ctx);
100   struct intel_screen *intel_screen = intel->intelScreen;
101
102   ctx->TextureFormatSupported[MESA_FORMAT_B8G8R8A8_UNORM] = true;
103   if (intel_screen->deviceID != PCI_CHIP_I830_M &&
104       intel_screen->deviceID != PCI_CHIP_845_G)
105      ctx->TextureFormatSupported[MESA_FORMAT_B8G8R8X8_UNORM] = true;
106   if (intel->gen == 3)
107      ctx->TextureFormatSupported[MESA_FORMAT_B8G8R8A8_SRGB] = true;
108   ctx->TextureFormatSupported[MESA_FORMAT_B4G4R4A4_UNORM] = true;
109   ctx->TextureFormatSupported[MESA_FORMAT_B5G5R5A1_UNORM] = true;
110   ctx->TextureFormatSupported[MESA_FORMAT_B5G6R5_UNORM] = true;
111   ctx->TextureFormatSupported[MESA_FORMAT_L_UNORM8] = true;
112   ctx->TextureFormatSupported[MESA_FORMAT_A_UNORM8] = true;
113   ctx->TextureFormatSupported[MESA_FORMAT_I_UNORM8] = true;
114   ctx->TextureFormatSupported[MESA_FORMAT_L8A8_UNORM] = true;
115
116   /* Depth and stencil */
117   if (intel->gen == 3) {
118      ctx->TextureFormatSupported[MESA_FORMAT_Z24_UNORM_S8_UINT] = true;
119      ctx->TextureFormatSupported[MESA_FORMAT_Z24_UNORM_X8_UINT] = true;
120
121      /*
122       * This was disabled in initial FBO enabling to avoid combinations
123       * of depth+stencil that wouldn't work together.  We since decided
124       * that it was OK, since it's up to the app to come up with the
125       * combo that actually works, so this can probably be re-enabled.
126       */
127      /*
128      ctx->TextureFormatSupported[MESA_FORMAT_Z_UNORM16] = true;
129      ctx->TextureFormatSupported[MESA_FORMAT_Z24] = true;
130      */
131   }
132
133   /* ctx->Extensions.MESA_ycbcr_texture */
134   ctx->TextureFormatSupported[MESA_FORMAT_YCBCR] = true;
135   ctx->TextureFormatSupported[MESA_FORMAT_YCBCR_REV] = true;
136
137   /* GL_3DFX_texture_compression_FXT1 */
138   ctx->TextureFormatSupported[MESA_FORMAT_RGB_FXT1] = true;
139   ctx->TextureFormatSupported[MESA_FORMAT_RGBA_FXT1] = true;
140
141   /* GL_EXT_texture_compression_s3tc */
142   ctx->TextureFormatSupported[MESA_FORMAT_RGB_DXT1] = true;
143   ctx->TextureFormatSupported[MESA_FORMAT_RGBA_DXT1] = true;
144   ctx->TextureFormatSupported[MESA_FORMAT_RGBA_DXT3] = true;
145   ctx->TextureFormatSupported[MESA_FORMAT_RGBA_DXT5] = true;
146}
147
148extern const struct tnl_pipeline_stage *intel_pipeline[];
149
150bool
151i915CreateContext(int api,
152		  const struct gl_config * mesaVis,
153                  __DRIcontext * driContextPriv,
154                  unsigned major_version,
155                  unsigned minor_version,
156                  uint32_t flags,
157                  unsigned *error,
158                  void *sharedContextPrivate)
159{
160   struct dd_function_table functions;
161   struct i915_context *i915 = rzalloc(NULL, struct i915_context);
162   struct intel_context *intel = &i915->intel;
163   struct gl_context *ctx = &intel->ctx;
164
165   if (!i915) {
166      *error = __DRI_CTX_ERROR_NO_MEMORY;
167      return false;
168   }
169
170   i915InitVtbl(i915);
171
172   i915InitDriverFunctions(&functions);
173
174   if (!intelInitContext(intel, api, major_version, minor_version, flags,
175                         mesaVis, driContextPriv,
176                         sharedContextPrivate, &functions,
177                         error)) {
178      ralloc_free(i915);
179      return false;
180   }
181
182   intel_init_texture_formats(ctx);
183
184   _math_matrix_ctr(&intel->ViewportMatrix);
185
186   /* Initialize swrast, tnl driver tables: */
187   intelInitTriFuncs(ctx);
188
189   /* Install the customized pipeline: */
190   _tnl_destroy_pipeline(ctx);
191   _tnl_install_pipeline(ctx, intel_pipeline);
192
193   if (intel->no_rast)
194      FALLBACK(intel, INTEL_FALLBACK_USER, 1);
195
196   ctx->Const.MaxTextureUnits = I915_TEX_UNITS;
197   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits = I915_TEX_UNITS;
198   ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits = I915_TEX_UNITS;
199   ctx->Const.MaxTextureCoordUnits = I915_TEX_UNITS;
200   ctx->Const.MaxVarying = I915_TEX_UNITS;
201   ctx->Const.Program[MESA_SHADER_VERTEX].MaxOutputComponents =
202      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxInputComponents = ctx->Const.MaxVarying * 4;
203   ctx->Const.MaxCombinedTextureImageUnits =
204      ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits +
205      ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits;
206
207   /* Advertise the full hardware capabilities.  The new memory
208    * manager should cope much better with overload situations:
209    */
210   ctx->Const.MaxTextureLevels = 12;
211   ctx->Const.Max3DTextureLevels = 9;
212   ctx->Const.MaxCubeTextureLevels = 12;
213   ctx->Const.MaxTextureRectSize = (1 << 11);
214   ctx->Const.MaxTextureUnits = I915_TEX_UNITS;
215
216   ctx->Const.MaxTextureMaxAnisotropy = 4.0;
217
218   /* GL_ARB_fragment_program limits - don't think Mesa actually
219    * validates programs against these, and in any case one ARB
220    * instruction can translate to more than one HW instruction, so
221    * we'll still have to check and fallback each time.
222    */
223   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeTemps = I915_MAX_TEMPORARY;
224   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeAttribs = 11;    /* 8 tex, 2 color, fog */
225   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeParameters = I915_MAX_CONSTANT;
226   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeAluInstructions = I915_MAX_ALU_INSN;
227   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeTexInstructions = I915_MAX_TEX_INSN;
228   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeInstructions = (I915_MAX_ALU_INSN +
229                                                       I915_MAX_TEX_INSN);
230   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeTexIndirections =
231      I915_MAX_TEX_INDIRECT;
232   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeAddressRegs = 0; /* I don't think we have one */
233   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxEnvParams =
234      MIN2(ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxNativeParameters,
235	   ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxEnvParams);
236
237   /* i915 stores all values in single-precision floats.  Values aren't set
238    * for other program targets because software is used for those targets.
239    */
240   ctx->Const.Program[MESA_SHADER_FRAGMENT].MediumFloat.RangeMin = 127;
241   ctx->Const.Program[MESA_SHADER_FRAGMENT].MediumFloat.RangeMax = 127;
242   ctx->Const.Program[MESA_SHADER_FRAGMENT].MediumFloat.Precision = 23;
243   ctx->Const.Program[MESA_SHADER_FRAGMENT].LowFloat = ctx->Const.Program[MESA_SHADER_FRAGMENT].HighFloat =
244      ctx->Const.Program[MESA_SHADER_FRAGMENT].MediumFloat;
245   ctx->Const.Program[MESA_SHADER_FRAGMENT].MediumInt.RangeMin = 24;
246   ctx->Const.Program[MESA_SHADER_FRAGMENT].MediumInt.RangeMax = 24;
247   ctx->Const.Program[MESA_SHADER_FRAGMENT].MediumInt.Precision = 0;
248   ctx->Const.Program[MESA_SHADER_FRAGMENT].LowInt = ctx->Const.Program[MESA_SHADER_FRAGMENT].HighInt =
249      ctx->Const.Program[MESA_SHADER_FRAGMENT].MediumInt;
250
251   ctx->FragmentProgram._MaintainTexEnvProgram = true;
252
253   /* FINISHME: Are there other options that should be enabled for software
254    * FINISHME: vertex shaders?
255    */
256   ctx->ShaderCompilerOptions[MESA_SHADER_VERTEX].EmitCondCodes = true;
257
258   struct gl_shader_compiler_options *const fs_options =
259      & ctx->ShaderCompilerOptions[MESA_SHADER_FRAGMENT];
260   fs_options->MaxIfDepth = 0;
261   fs_options->EmitNoNoise = true;
262   fs_options->EmitNoPow = true;
263   fs_options->EmitNoMainReturn = true;
264   fs_options->EmitNoIndirectInput = true;
265   fs_options->EmitNoIndirectOutput = true;
266   fs_options->EmitNoIndirectUniform = true;
267   fs_options->EmitNoIndirectTemp = true;
268
269   ctx->Const.MaxDrawBuffers = 1;
270   ctx->Const.QueryCounterBits.SamplesPassed = 0;
271
272   _tnl_init_vertices(ctx, ctx->Const.MaxArrayLockSize + 12,
273                      36 * sizeof(GLfloat));
274
275   intel->verts = TNL_CONTEXT(ctx)->clipspace.vertex_buf;
276
277   i915InitState(i915);
278
279   /* Always enable pixel fog.  Vertex fog using fog coord will conflict
280    * with fog code appended onto fragment program.
281    */
282   _tnl_allow_vertex_fog(ctx, 0);
283   _tnl_allow_pixel_fog(ctx, 1);
284
285   _mesa_compute_version(ctx);
286
287   _mesa_initialize_dispatch_tables(ctx);
288   _mesa_initialize_vbo_vtxfmt(ctx);
289
290   return true;
291}
292