2 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
4 The Weather Channel (TM) funded Tungsten Graphics to develop the
5 initial release of the Radeon 8500 driver under the XFree86 license.
6 This notice must be preserved.
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
16 The above copyright notice and this permission notice (including the
17 next paragraph) shall be included in all copies or substantial
18 portions of the Software.
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
24 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 **************************************************************************/
33 * \author Keith Whitwell <keith@tungstengraphics.com>
35 * \author Nicolai Haehnle <prefect_@gmx.net>
38 #include "main/glheader.h"
39 #include "main/api_arrayelt.h"
40 #include "main/context.h"
41 #include "main/simple_list.h"
42 #include "main/imports.h"
43 #include "main/extensions.h"
44 #include "main/bufferobj.h"
45 #include "main/texobj.h"
47 #include "swrast/swrast.h"
48 #include "swrast_setup/swrast_setup.h"
52 #include "tnl/t_pipeline.h"
54 #include "drivers/common/driverfuncs.h"
56 #include "radeon_debug.h"
57 #include "r600_context.h"
58 #include "radeon_common_context.h"
59 #include "radeon_buffer_objects.h"
60 #include "radeon_span.h"
61 #include "r600_cmdbuf.h"
62 #include "radeon_bocs_wrapper.h"
63 #include "radeon_queryobj.h"
64 #include "r600_blit.h"
66 #include "r700_state.h"
67 #include "r700_ioctl.h"
72 #define R600_ENABLE_GLSL_TEST 1
74 #define need_GL_VERSION_2_0
75 #define need_GL_VERSION_2_1
76 #define need_GL_ARB_draw_elements_base_vertex
77 #define need_GL_ARB_occlusion_query
78 #define need_GL_ARB_point_parameters
79 #define need_GL_ARB_vertex_program
80 #define need_GL_EXT_blend_equation_separate
81 #define need_GL_EXT_blend_func_separate
82 #define need_GL_EXT_blend_minmax
83 #define need_GL_EXT_framebuffer_object
84 #define need_GL_EXT_fog_coord
85 #define need_GL_EXT_gpu_program_parameters
86 #define need_GL_EXT_provoking_vertex
87 #define need_GL_EXT_secondary_color
88 #define need_GL_EXT_stencil_two_side
89 #define need_GL_ATI_separate_stencil
90 #define need_GL_NV_vertex_program
92 #include "main/remap_helper.h"
94 static const struct dri_extension card_extensions
[] = {
96 {"GL_ARB_depth_clamp", NULL
},
97 {"GL_ARB_depth_texture", NULL
},
98 {"GL_ARB_fragment_program", NULL
},
99 {"GL_ARB_fragment_program_shadow", NULL
},
100 {"GL_ARB_occlusion_query", GL_ARB_occlusion_query_functions
},
101 {"GL_ARB_multitexture", NULL
},
102 {"GL_ARB_point_parameters", GL_ARB_point_parameters_functions
},
103 {"GL_ARB_shadow", NULL
},
104 {"GL_ARB_shadow_ambient", NULL
},
105 {"GL_ARB_texture_border_clamp", NULL
},
106 {"GL_ARB_texture_cube_map", NULL
},
107 {"GL_ARB_texture_env_add", NULL
},
108 {"GL_ARB_texture_env_combine", NULL
},
109 {"GL_ARB_texture_env_crossbar", NULL
},
110 {"GL_ARB_texture_env_dot3", NULL
},
111 {"GL_ARB_texture_mirrored_repeat", NULL
},
112 {"GL_ARB_texture_non_power_of_two", NULL
},
113 {"GL_ARB_vertex_program", GL_ARB_vertex_program_functions
},
114 {"GL_EXT_blend_equation_separate", GL_EXT_blend_equation_separate_functions
},
115 {"GL_EXT_blend_func_separate", GL_EXT_blend_func_separate_functions
},
116 {"GL_EXT_blend_minmax", GL_EXT_blend_minmax_functions
},
117 {"GL_EXT_blend_subtract", NULL
},
118 {"GL_EXT_packed_depth_stencil", NULL
},
119 {"GL_EXT_fog_coord", GL_EXT_fog_coord_functions
},
120 {"GL_EXT_gpu_program_parameters", GL_EXT_gpu_program_parameters_functions
},
121 {"GL_EXT_provoking_vertex", GL_EXT_provoking_vertex_functions
},
122 {"GL_EXT_secondary_color", GL_EXT_secondary_color_functions
},
123 {"GL_EXT_shadow_funcs", NULL
},
124 {"GL_EXT_stencil_two_side", GL_EXT_stencil_two_side_functions
},
125 {"GL_EXT_stencil_wrap", NULL
},
126 {"GL_EXT_texture_edge_clamp", NULL
},
127 {"GL_EXT_texture_env_combine", NULL
},
128 {"GL_EXT_texture_env_dot3", NULL
},
129 {"GL_EXT_texture_filter_anisotropic", NULL
},
130 {"GL_EXT_texture_lod_bias", NULL
},
131 {"GL_EXT_texture_mirror_clamp", NULL
},
132 {"GL_EXT_texture_rectangle", NULL
},
133 {"GL_EXT_vertex_array_bgra", NULL
},
134 {"GL_EXT_texture_sRGB", NULL
},
135 {"GL_ATI_separate_stencil", GL_ATI_separate_stencil_functions
},
136 {"GL_ATI_texture_env_combine3", NULL
},
137 {"GL_ATI_texture_mirror_once", NULL
},
138 {"GL_MESA_pack_invert", NULL
},
139 {"GL_MESA_ycbcr_texture", NULL
},
140 {"GL_MESAX_texture_float", NULL
},
141 {"GL_NV_blend_square", NULL
},
142 {"GL_NV_vertex_program", GL_NV_vertex_program_functions
},
143 {"GL_SGIS_generate_mipmap", NULL
},
144 {"GL_ARB_pixel_buffer_object", NULL
},
145 {"GL_ARB_draw_elements_base_vertex", GL_ARB_draw_elements_base_vertex_functions
},
151 static const struct dri_extension mm_extensions
[] = {
152 { "GL_EXT_framebuffer_object", GL_EXT_framebuffer_object_functions
},
157 * The GL 2.0 functions are needed to make display lists work with
158 * functions added by GL_ATI_separate_stencil.
160 static const struct dri_extension gl_20_extension
[] = {
161 #ifdef R600_ENABLE_GLSL_TEST
162 {"GL_ARB_shading_language_100", GL_VERSION_2_0_functions
},
163 {"GL_ARB_shading_language_120", GL_VERSION_2_1_functions
},
165 {"GL_VERSION_2_0", GL_VERSION_2_0_functions
},
166 #endif /* R600_ENABLE_GLSL_TEST */
170 static const struct tnl_pipeline_stage
*r600_pipeline
[] = {
171 /* Catch any t&l fallbacks
173 &_tnl_vertex_transform_stage
,
174 &_tnl_normal_transform_stage
,
175 &_tnl_lighting_stage
,
176 &_tnl_fog_coordinate_stage
,
178 &_tnl_texture_transform_stage
,
179 &_tnl_point_attenuation_stage
,
180 &_tnl_vertex_program_stage
,
185 static void r600_get_lock(radeonContextPtr rmesa
)
187 drm_radeon_sarea_t
*sarea
= rmesa
->sarea
;
189 if (sarea
->ctx_owner
!= rmesa
->dri
.hwContext
) {
190 sarea
->ctx_owner
= rmesa
->dri
.hwContext
;
191 if (!rmesa
->radeonScreen
->kernel_mm
)
192 radeon_bo_legacy_texture_age(rmesa
->radeonScreen
->bom
);
196 static void r600_vtbl_emit_cs_header(struct radeon_cs
*cs
, radeonContextPtr rmesa
)
198 /* please flush pipe do all pending work */
202 static void r600_vtbl_pre_emit_atoms(radeonContextPtr radeon
)
204 r700Start3D((context_t
*)radeon
);
207 static void r600_fallback(GLcontext
*ctx
, GLuint bit
, GLboolean mode
)
209 context_t
*context
= R700_CONTEXT(ctx
);
211 context
->radeon
.Fallback
|= bit
;
213 context
->radeon
.Fallback
&= ~bit
;
216 static void r600_emit_query_finish(radeonContextPtr radeon
)
218 context_t
*context
= (context_t
*) radeon
;
219 BATCH_LOCALS(&context
->radeon
);
221 struct radeon_query_object
*query
= radeon
->query
.current
;
223 BEGIN_BATCH_NO_AUTOSTATE(4 + 2);
224 R600_OUT_BATCH(CP_PACKET3(R600_IT_EVENT_WRITE
, 2));
225 R600_OUT_BATCH(ZPASS_DONE
);
226 R600_OUT_BATCH(query
->curr_offset
+ 8); /* hw writes qwords */
227 R600_OUT_BATCH(0x00000000);
228 R600_OUT_BATCH_RELOC(VGT_EVENT_INITIATOR
, query
->bo
, 0, 0, RADEON_GEM_DOMAIN_GTT
, 0);
230 assert(query
->curr_offset
< RADEON_QUERY_PAGE_SIZE
);
231 query
->emitted_begin
= GL_FALSE
;
234 static void r600_init_vtbl(radeonContextPtr radeon
)
236 radeon
->vtbl
.get_lock
= r600_get_lock
;
237 radeon
->vtbl
.update_viewport_offset
= r700UpdateViewportOffset
;
238 radeon
->vtbl
.emit_cs_header
= r600_vtbl_emit_cs_header
;
239 radeon
->vtbl
.swtcl_flush
= NULL
;
240 radeon
->vtbl
.pre_emit_atoms
= r600_vtbl_pre_emit_atoms
;
241 radeon
->vtbl
.fallback
= r600_fallback
;
242 radeon
->vtbl
.emit_query_finish
= r600_emit_query_finish
;
243 radeon
->vtbl
.check_blit
= r600_check_blit
;
244 radeon
->vtbl
.blit
= r600_blit
;
245 radeon
->vtbl
.is_format_renderable
= r600IsFormatRenderable
;
248 static void r600InitConstValues(GLcontext
*ctx
, radeonScreenPtr screen
)
250 ctx
->Const
.MaxTextureImageUnits
= 16;
251 /* 8 per clause on r6xx, 16 on r7xx
252 * but I think mesa only supports 8 at the moment
254 ctx
->Const
.MaxTextureCoordUnits
= 8;
255 ctx
->Const
.MaxTextureUnits
=
256 MIN2(ctx
->Const
.MaxTextureImageUnits
,
257 ctx
->Const
.MaxTextureCoordUnits
);
258 ctx
->Const
.MaxCombinedTextureImageUnits
=
259 ctx
->Const
.MaxVertexTextureImageUnits
+
260 ctx
->Const
.MaxTextureImageUnits
;
262 ctx
->Const
.MaxTextureMaxAnisotropy
= 16.0;
263 ctx
->Const
.MaxTextureLodBias
= 16.0;
265 ctx
->Const
.MaxTextureLevels
= 13; /* hw support 14 */
266 ctx
->Const
.MaxTextureRectSize
= 4096; /* hw support 8192 */
268 ctx
->Const
.MinPointSize
= 0x0001 / 8.0;
269 ctx
->Const
.MinPointSizeAA
= 0x0001 / 8.0;
270 ctx
->Const
.MaxPointSize
= 0xffff / 8.0;
271 ctx
->Const
.MaxPointSizeAA
= 0xffff / 8.0;
273 ctx
->Const
.MinLineWidth
= 0x0001 / 8.0;
274 ctx
->Const
.MinLineWidthAA
= 0x0001 / 8.0;
275 ctx
->Const
.MaxLineWidth
= 0xffff / 8.0;
276 ctx
->Const
.MaxLineWidthAA
= 0xffff / 8.0;
278 ctx
->Const
.MaxDrawBuffers
= 1; /* hw supports 8 */
279 ctx
->Const
.MaxColorAttachments
= 1;
280 ctx
->Const
.MaxRenderbufferSize
= 4096;
282 /* 256 for reg-based consts, inline consts also supported */
283 ctx
->Const
.VertexProgram
.MaxInstructions
= 8192; /* in theory no limit */
284 ctx
->Const
.VertexProgram
.MaxNativeInstructions
= 8192;
285 ctx
->Const
.VertexProgram
.MaxNativeAttribs
= 160;
286 ctx
->Const
.VertexProgram
.MaxTemps
= 128;
287 ctx
->Const
.VertexProgram
.MaxNativeTemps
= 128;
288 ctx
->Const
.VertexProgram
.MaxNativeParameters
= 256;
289 ctx
->Const
.VertexProgram
.MaxNativeAddressRegs
= 1; /* ??? */
291 ctx
->Const
.FragmentProgram
.MaxNativeTemps
= 128;
292 ctx
->Const
.FragmentProgram
.MaxNativeAttribs
= 32;
293 ctx
->Const
.FragmentProgram
.MaxNativeParameters
= 256;
294 ctx
->Const
.FragmentProgram
.MaxNativeAluInstructions
= 8192;
295 /* 8 per clause on r6xx, 16 on r7xx */
296 if (screen
->chip_family
>= CHIP_FAMILY_RV770
)
297 ctx
->Const
.FragmentProgram
.MaxNativeTexInstructions
= 16;
299 ctx
->Const
.FragmentProgram
.MaxNativeTexInstructions
= 8;
300 ctx
->Const
.FragmentProgram
.MaxNativeInstructions
= 8192;
301 ctx
->Const
.FragmentProgram
.MaxNativeTexIndirections
= 8; /* ??? */
302 ctx
->Const
.FragmentProgram
.MaxNativeAddressRegs
= 0; /* and these are?? */
305 static void r600ParseOptions(context_t
*r600
, radeonScreenPtr screen
)
307 /* Parse configuration files.
308 * Do this here so that initialMaxAnisotropy is set before we create
309 * the default textures.
311 driParseConfigFiles(&r600
->radeon
.optionCache
, &screen
->optionCache
,
312 screen
->driScreen
->myNum
, "r600");
314 r600
->radeon
.initialMaxAnisotropy
= driQueryOptionf(&r600
->radeon
.optionCache
,
315 "def_max_anisotropy");
319 static void r600InitGLExtensions(GLcontext
*ctx
)
321 context_t
*r600
= R700_CONTEXT(ctx
);
323 driInitExtensions(ctx
, card_extensions
, GL_TRUE
);
324 if (r600
->radeon
.radeonScreen
->kernel_mm
)
325 driInitExtensions(ctx
, mm_extensions
, GL_FALSE
);
327 #ifdef R600_ENABLE_GLSL_TEST
328 driInitExtensions(ctx
, gl_20_extension
, GL_TRUE
);
329 _mesa_enable_2_0_extensions(ctx
);
331 /* glsl compiler has problem if this is not GL_TRUE */
332 ctx
->Shader
.EmitCondCodes
= GL_TRUE
;
333 #endif /* R600_ENABLE_GLSL_TEST */
336 (&r600
->radeon
.optionCache
, "disable_stencil_two_side"))
337 _mesa_disable_extension(ctx
, "GL_EXT_stencil_two_side");
339 if (r600
->radeon
.glCtx
->Mesa_DXTn
340 && !driQueryOptionb(&r600
->radeon
.optionCache
, "disable_s3tc")) {
341 _mesa_enable_extension(ctx
, "GL_EXT_texture_compression_s3tc");
342 _mesa_enable_extension(ctx
, "GL_S3_s3tc");
344 if (driQueryOptionb(&r600
->radeon
.optionCache
, "force_s3tc_enable"))
346 _mesa_enable_extension(ctx
, "GL_EXT_texture_compression_s3tc");
349 /* RV740 had a broken pipe config prior to drm 1.32 */
350 if (!r600
->radeon
.radeonScreen
->kernel_mm
) {
351 if ((r600
->radeon
.dri
.drmMinor
< 32) &&
352 (r600
->radeon
.radeonScreen
->chip_family
== CHIP_FAMILY_RV740
))
353 _mesa_disable_extension(ctx
, "GL_ARB_occlusion_query");
357 /* Create the device specific rendering context.
359 GLboolean
r600CreateContext(gl_api api
,
360 const __GLcontextModes
* glVisual
,
361 __DRIcontext
* driContextPriv
,
362 void *sharedContextPrivate
)
364 __DRIscreen
*sPriv
= driContextPriv
->driScreenPriv
;
365 radeonScreenPtr screen
= (radeonScreenPtr
) (sPriv
->private);
366 struct dd_function_table functions
;
371 assert(driContextPriv
);
374 /* Allocate the R600 context */
375 r600
= (context_t
*) CALLOC(sizeof(*r600
));
377 radeon_error("Failed to allocate memory for context.\n");
381 r600ParseOptions(r600
, screen
);
383 r600
->radeon
.radeonScreen
= screen
;
384 r600_init_vtbl(&r600
->radeon
);
386 /* Init default driver functions then plug in our R600-specific functions
387 * (the texture functions are especially important)
389 _mesa_init_driver_functions(&functions
);
391 r700InitStateFuncs(&r600
->radeon
, &functions
);
392 r600InitTextureFuncs(&r600
->radeon
, &functions
);
393 r700InitShaderFuncs(&functions
);
394 radeonInitQueryObjFunctions(&functions
);
395 r700InitIoctlFuncs(&functions
);
396 radeonInitBufferObjectFuncs(&functions
);
398 if (!radeonInitContext(&r600
->radeon
, &functions
,
399 glVisual
, driContextPriv
,
400 sharedContextPrivate
)) {
401 radeon_error("Initializing context failed.\n");
406 ctx
= r600
->radeon
.glCtx
;
408 ctx
->VertexProgram
._MaintainTnlProgram
= GL_TRUE
;
409 ctx
->FragmentProgram
._MaintainTexEnvProgram
= GL_TRUE
;
411 r600InitConstValues(ctx
, screen
);
413 _mesa_set_mvp_with_dp4( ctx
, GL_TRUE
);
415 /* Initialize the software rasterizer and helper modules.
417 _swrast_CreateContext(ctx
);
418 _vbo_CreateContext(ctx
);
419 _tnl_CreateContext(ctx
);
420 _swsetup_CreateContext(ctx
);
421 _swsetup_Wakeup(ctx
);
423 /* Install the customized pipeline:
425 _tnl_destroy_pipeline(ctx
);
426 _tnl_install_pipeline(ctx
, r600_pipeline
);
427 TNL_CONTEXT(ctx
)->Driver
.RunPipeline
= _tnl_run_pipeline
;
429 /* Configure swrast and TNL to match hardware characteristics:
431 _swrast_allow_pixel_fog(ctx
, GL_FALSE
);
432 _swrast_allow_vertex_fog(ctx
, GL_TRUE
);
433 _tnl_allow_pixel_fog(ctx
, GL_FALSE
);
434 _tnl_allow_vertex_fog(ctx
, GL_TRUE
);
440 radeon_fbo_init(&r600
->radeon
);
441 radeonInitSpanFuncs( ctx
);
442 r600InitCmdBuf(r600
);
443 r700InitState(r600
->radeon
.glCtx
);
445 r600InitGLExtensions(ctx
);