1 /**************************************************************************
3 Copyright (C) 2004 Nicolai Haehnle.
7 Permission is hereby granted, free of charge, to any person obtaining a
8 copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 on the rights to use, copy, modify, merge, publish, distribute, sub
11 license, and/or sell copies of the Software, and to permit persons to whom
12 the Software is furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice (including the next
15 paragraph) shall be included in all copies or substantial portions of the
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 ATI, VA LINUX SYSTEMS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
31 * \brief R300 Render (Vertex Buffer Implementation)
33 * The immediate implementation has been removed from CVS in favor of the vertex
34 * buffer implementation.
36 * The render functions are called by the pipeline manager to render a batch of
37 * primitives. They return TRUE to pass on to the next stage (i.e. software
38 * rasterization) or FALSE to indicate that the pipeline has finished after
39 * rendering something.
41 * When falling back to software TCL still attempt to use hardware
44 * I am not sure that the cache related registers are setup correctly, but
45 * obviously this does work... Further investigation is needed.
47 * \author Nicolai Haehnle <prefect_@gmx.net>
57 #include "simple_list.h"
58 #include "api_arrayelt.h"
59 #include "swrast/swrast.h"
60 #include "swrast_setup/swrast_setup.h"
63 #include "tnl/t_vp_build.h"
64 #include "radeon_reg.h"
65 #include "radeon_macros.h"
66 #include "radeon_ioctl.h"
67 #include "radeon_state.h"
68 #include "r300_context.h"
69 #include "r300_ioctl.h"
70 #include "r300_state.h"
73 #include "r300_maos.h"
74 #include "r300_emit.h"
75 extern int future_hw_tcl_on
;
78 * \brief Convert a OpenGL primitive type into a R300 primitive type.
80 static int r300PrimitiveType(r300ContextPtr rmesa
, GLcontext
* ctx
, int prim
)
84 switch (prim
& PRIM_MODE_MASK
) {
86 type
= R300_VAP_VF_CNTL__PRIM_POINTS
;
89 type
= R300_VAP_VF_CNTL__PRIM_LINES
;
92 type
= R300_VAP_VF_CNTL__PRIM_LINE_STRIP
;
95 type
= R300_VAP_VF_CNTL__PRIM_LINE_LOOP
;
98 type
= R300_VAP_VF_CNTL__PRIM_TRIANGLES
;
100 case GL_TRIANGLE_STRIP
:
101 type
= R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP
;
103 case GL_TRIANGLE_FAN
:
104 type
= R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN
;
107 type
= R300_VAP_VF_CNTL__PRIM_QUADS
;
110 type
= R300_VAP_VF_CNTL__PRIM_QUAD_STRIP
;
113 type
= R300_VAP_VF_CNTL__PRIM_POLYGON
;
117 "%s:%s Do not know how to handle primitive 0x%04x - help me !\n",
118 __FILE__
, __FUNCTION__
, prim
& PRIM_MODE_MASK
);
125 static int r300NumVerts(r300ContextPtr rmesa
, int num_verts
, int prim
)
129 switch (prim
& PRIM_MODE_MASK
) {
134 verts_off
= num_verts
% 2;
138 verts_off
= num_verts
;
142 verts_off
= num_verts
;
145 verts_off
= num_verts
% 3;
147 case GL_TRIANGLE_STRIP
:
149 verts_off
= num_verts
;
151 case GL_TRIANGLE_FAN
:
153 verts_off
= num_verts
;
156 verts_off
= num_verts
% 4;
160 verts_off
= num_verts
;
162 verts_off
= num_verts
% 2;
166 verts_off
= num_verts
;
170 "%s:%s Do not know how to handle primitive 0x%04x - help me !\n",
171 __FILE__
, __FUNCTION__
, prim
& PRIM_MODE_MASK
);
176 if (RADEON_DEBUG
& DEBUG_VERTS
) {
177 if (num_verts
- verts_off
== 0) {
179 ("user error: Need more than %d vertices to draw primitive 0x%04x !\n",
180 num_verts
, prim
& PRIM_MODE_MASK
);
186 ("user error: %d is not a valid number of vertices for primitive 0x%04x !\n",
187 num_verts
, prim
& PRIM_MODE_MASK
);
191 return num_verts
- verts_off
;
194 static void inline r300FireEB(r300ContextPtr rmesa
, unsigned long addr
,
195 int vertex_count
, int type
, int elt_size
)
197 int cmd_reserved
= 0;
199 drm_radeon_cmd_header_t
*cmd
= NULL
;
200 unsigned long t_addr
;
201 unsigned long magic_1
, magic_2
;
203 assert(elt_size
== 2 || elt_size
== 4);
205 if (addr
& (elt_size
- 1)) {
206 WARN_ONCE("Badly aligned buffer\n");
210 magic_1
= (addr
% 32) / 4;
211 t_addr
= addr
& ~0x1d;
212 magic_2
= (vertex_count
+ 1 + (t_addr
& 0x2)) / 2 + magic_1
;
214 start_packet3(RADEON_CP_PACKET3_3D_DRAW_INDX_2
, 0);
216 e32(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
|
217 (vertex_count
<< 16) | type
|
218 R300_VAP_VF_CNTL__INDEX_SIZE_32bit
);
220 e32(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
|
221 (vertex_count
<< 16) | type
);
224 start_packet3(RADEON_CP_PACKET3_INDX_BUFFER
, 2);
227 e32(R300_EB_UNK1
| (0 << 16) | R300_EB_UNK2
);
230 e32(R300_EB_UNK1
| (magic_1
<< 16) | R300_EB_UNK2
);
234 e32(R300_EB_UNK1
| (0 << 16) | R300_EB_UNK2
);
244 e32((vertex_count
+ 1) / 2);
249 static void r300RunRenderPrimitive(r300ContextPtr rmesa
, GLcontext
* ctx
,
250 int start
, int end
, int prim
)
254 type
= r300PrimitiveType(rmesa
, ctx
, prim
);
255 num_verts
= r300NumVerts(rmesa
, end
- start
, prim
);
257 if (type
< 0 || num_verts
<= 0)
260 if (rmesa
->state
.VB
.Elts
) {
261 r300EmitAOS(rmesa
, rmesa
->state
.aos_count
, start
);
262 if (num_verts
> 65535) {
263 /* not implemented yet */
264 WARN_ONCE("Too many elts\n");
267 r300EmitElts(ctx
, rmesa
->state
.VB
.Elts
, num_verts
,
268 rmesa
->state
.VB
.elt_size
);
269 r300FireEB(rmesa
, rmesa
->state
.elt_dma
.aos_offset
,
270 num_verts
, type
, rmesa
->state
.VB
.elt_size
);
272 r300EmitAOS(rmesa
, rmesa
->state
.aos_count
, start
);
273 fire_AOS(rmesa
, num_verts
, type
);
277 #define CONV_VB(a, b) rvb->AttribPtr[(a)].size = vb->b->size, \
278 rvb->AttribPtr[(a)].type = GL_FLOAT, \
279 rvb->AttribPtr[(a)].stride = vb->b->stride, \
280 rvb->AttribPtr[(a)].data = vb->b->data
282 static void radeon_vb_to_rvb(r300ContextPtr rmesa
,
283 struct radeon_vertex_buffer
*rvb
,
284 struct vertex_buffer
*vb
)
288 ctx
= rmesa
->radeon
.glCtx
;
290 memset(rvb
, 0, sizeof(*rvb
));
292 rvb
->Elts
= vb
->Elts
;
295 rvb
->elt_max
= vb
->Count
;
297 rvb
->Count
= vb
->Count
;
300 CONV_VB(VERT_ATTRIB_POS
, ObjPtr
);
303 CONV_VB(VERT_ATTRIB_POS
, ClipPtr
);
306 CONV_VB(VERT_ATTRIB_NORMAL
, NormalPtr
);
307 CONV_VB(VERT_ATTRIB_COLOR0
, ColorPtr
[0]);
308 CONV_VB(VERT_ATTRIB_COLOR1
, SecondaryColorPtr
[0]);
309 CONV_VB(VERT_ATTRIB_FOG
, FogCoordPtr
);
311 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++)
312 CONV_VB(VERT_ATTRIB_TEX0
+ i
, TexCoordPtr
[i
]);
314 for (i
= 0; i
< MAX_VERTEX_PROGRAM_ATTRIBS
; i
++)
315 CONV_VB(VERT_ATTRIB_GENERIC0
+ i
,
316 AttribPtr
[VERT_ATTRIB_GENERIC0
+ i
]);
318 rvb
->Primitive
= vb
->Primitive
;
319 rvb
->PrimitiveCount
= vb
->PrimitiveCount
;
320 rvb
->LockFirst
= rvb
->LockCount
= 0;
321 rvb
->lock_uptodate
= GL_FALSE
;
324 static GLboolean
r300RunRender(GLcontext
* ctx
,
325 struct tnl_pipeline_stage
*stage
)
327 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
328 struct radeon_vertex_buffer
*VB
= &rmesa
->state
.VB
;
330 int cmd_reserved
= 0;
332 drm_radeon_cmd_header_t
*cmd
= NULL
;
334 if (RADEON_DEBUG
& DEBUG_PRIMS
)
335 fprintf(stderr
, "%s\n", __FUNCTION__
);
338 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
339 radeon_vb_to_rvb(rmesa
, VB
, &tnl
->vb
);
342 r300UpdateShaders(rmesa
);
343 if (r300EmitArrays(ctx
))
346 r300UpdateShaderStates(rmesa
);
348 reg_start(R300_RB3D_DSTCACHE_CTLSTAT
, 0);
349 e32(R300_RB3D_DSTCACHE_UNKNOWN_0A
);
351 reg_start(R300_RB3D_ZCACHE_CTLSTAT
, 0);
352 e32(R300_RB3D_ZCACHE_UNKNOWN_03
);
354 r300EmitState(rmesa
);
356 for (i
= 0; i
< VB
->PrimitiveCount
; i
++) {
357 GLuint prim
= _tnl_translate_prim(&VB
->Primitive
[i
]);
358 GLuint start
= VB
->Primitive
[i
].start
;
359 GLuint length
= VB
->Primitive
[i
].count
;
360 GLuint end
= VB
->Primitive
[i
].start
+ VB
->Primitive
[i
].count
;
361 r300RunRenderPrimitive(rmesa
, ctx
, start
, end
, prim
);
364 reg_start(R300_RB3D_DSTCACHE_CTLSTAT
, 0);
365 e32(R300_RB3D_DSTCACHE_UNKNOWN_0A
);
367 reg_start(R300_RB3D_ZCACHE_CTLSTAT
, 0);
368 e32(R300_RB3D_ZCACHE_UNKNOWN_03
);
374 r300ReleaseArrays(ctx
);
379 #define FALLBACK_IF(expr) \
382 if (1 || RADEON_DEBUG & DEBUG_FALLBACKS) \
383 WARN_ONCE("Software fallback:%s\n", \
385 return R300_FALLBACK_RAST; \
389 static int r300Fallback(GLcontext
* ctx
)
391 r300ContextPtr r300
= R300_CONTEXT(ctx
);
392 struct r300_fragment_program
*fp
= (struct r300_fragment_program
*)
393 (char *)ctx
->FragmentProgram
._Current
;
397 r300TranslateFragmentShader(r300
, fp
);
398 FALLBACK_IF(!fp
->translated
);
401 FALLBACK_IF(ctx
->RenderMode
!= GL_RENDER
);
403 FALLBACK_IF(ctx
->Stencil
._TestTwoSide
404 && (ctx
->Stencil
.Ref
[0] != ctx
->Stencil
.Ref
[1]
405 || ctx
->Stencil
.ValueMask
[0] !=
406 ctx
->Stencil
.ValueMask
[1]
407 || ctx
->Stencil
.WriteMask
[0] !=
408 ctx
->Stencil
.WriteMask
[1]));
410 FALLBACK_IF(ctx
->Color
.ColorLogicOpEnabled
);
412 if (ctx
->Extensions
.NV_point_sprite
|| ctx
->Extensions
.ARB_point_sprite
)
413 FALLBACK_IF(ctx
->Point
.PointSprite
);
415 if (!r300
->disable_lowimpact_fallback
) {
416 FALLBACK_IF(ctx
->Polygon
.OffsetPoint
);
417 FALLBACK_IF(ctx
->Polygon
.OffsetLine
);
418 FALLBACK_IF(ctx
->Polygon
.StippleFlag
);
419 FALLBACK_IF(ctx
->Multisample
.Enabled
);
420 FALLBACK_IF(ctx
->Line
.StippleFlag
);
421 FALLBACK_IF(ctx
->Line
.SmoothFlag
);
422 FALLBACK_IF(ctx
->Point
.SmoothFlag
);
425 return R300_FALLBACK_NONE
;
428 static GLboolean
r300RunNonTNLRender(GLcontext
* ctx
,
429 struct tnl_pipeline_stage
*stage
)
431 if (RADEON_DEBUG
& DEBUG_PRIMS
)
432 fprintf(stderr
, "%s\n", __FUNCTION__
);
434 if (r300Fallback(ctx
) >= R300_FALLBACK_RAST
)
437 return r300RunRender(ctx
, stage
);
440 static GLboolean
r300RunTCLRender(GLcontext
* ctx
,
441 struct tnl_pipeline_stage
*stage
)
443 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
444 struct r300_vertex_program
*vp
;
446 hw_tcl_on
= future_hw_tcl_on
;
448 if (RADEON_DEBUG
& DEBUG_PRIMS
)
449 fprintf(stderr
, "%s\n", __FUNCTION__
);
451 if (hw_tcl_on
== GL_FALSE
)
454 if (r300Fallback(ctx
) >= R300_FALLBACK_TCL
) {
455 hw_tcl_on
= GL_FALSE
;
459 r300UpdateShaders(rmesa
);
461 vp
= (struct r300_vertex_program
*)CURRENT_VERTEX_SHADER(ctx
);
462 if (vp
->native
== GL_FALSE
) {
463 hw_tcl_on
= GL_FALSE
;
467 return r300RunRender(ctx
, stage
);
470 const struct tnl_pipeline_stage _r300_render_stage
= {
471 "r300 Hardware Rasterization",
479 const struct tnl_pipeline_stage _r300_tcl_stage
= {
480 "r300 Hardware Transform, Clipping and Lighting",