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>
49 * \todo Add immediate implementation back? Perhaps this is useful if there are
60 #include "simple_list.h"
61 #include "api_arrayelt.h"
62 #include "swrast/swrast.h"
63 #include "swrast_setup/swrast_setup.h"
66 #include "tnl/t_vp_build.h"
67 #include "radeon_reg.h"
68 #include "radeon_macros.h"
69 #include "radeon_ioctl.h"
70 #include "radeon_state.h"
71 #include "r300_context.h"
72 #include "r300_ioctl.h"
73 #include "r300_state.h"
76 #include "r300_emit.h"
77 extern int future_hw_tcl_on
;
80 * \brief Convert a OpenGL primitive type into a R300 primitive type.
82 int r300PrimitiveType(r300ContextPtr rmesa
, int prim
)
84 switch (prim
& PRIM_MODE_MASK
) {
86 return R300_VAP_VF_CNTL__PRIM_POINTS
;
89 return R300_VAP_VF_CNTL__PRIM_LINES
;
92 return R300_VAP_VF_CNTL__PRIM_LINE_STRIP
;
95 return R300_VAP_VF_CNTL__PRIM_LINE_LOOP
;
98 return R300_VAP_VF_CNTL__PRIM_TRIANGLES
;
100 case GL_TRIANGLE_STRIP
:
101 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP
;
103 case GL_TRIANGLE_FAN
:
104 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN
;
107 return R300_VAP_VF_CNTL__PRIM_QUADS
;
110 return R300_VAP_VF_CNTL__PRIM_QUAD_STRIP
;
113 return R300_VAP_VF_CNTL__PRIM_POLYGON
;
122 int r300NumVerts(r300ContextPtr rmesa
, int num_verts
, int prim
)
126 switch (prim
& PRIM_MODE_MASK
) {
131 verts_off
= num_verts
% 2;
135 verts_off
= num_verts
;
139 verts_off
= num_verts
;
142 verts_off
= num_verts
% 3;
144 case GL_TRIANGLE_STRIP
:
146 verts_off
= num_verts
;
148 case GL_TRIANGLE_FAN
:
150 verts_off
= num_verts
;
153 verts_off
= num_verts
% 4;
157 verts_off
= num_verts
;
159 verts_off
= num_verts
% 2;
163 verts_off
= num_verts
;
171 return num_verts
- verts_off
;
174 static void r300EmitElts(GLcontext
* ctx
, void *elts
, unsigned long n_elts
)
176 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
177 struct r300_dma_region
*rvb
= &rmesa
->state
.elt_dma
;
180 if (r300IsGartMemory(rmesa
, elts
, n_elts
* 4)) {
181 rvb
->address
= rmesa
->radeon
.radeonScreen
->gartTextures
.map
;
182 rvb
->start
= ((char *)elts
) - rvb
->address
;
184 rmesa
->radeon
.radeonScreen
->gart_texture_offset
+
187 } else if (r300IsGartMemory(rmesa
, elts
, 1)) {
188 WARN_ONCE("Pointer not within GART memory!\n");
192 r300AllocDmaRegion(rmesa
, rvb
, n_elts
* 4, 4);
193 rvb
->aos_offset
= GET_START(rvb
);
195 out
= rvb
->address
+ rvb
->start
;
196 memcpy(out
, elts
, n_elts
* 4);
199 static void r300FireEB(r300ContextPtr rmesa
, unsigned long addr
,
200 int vertex_count
, int type
)
202 int cmd_reserved
= 0;
204 drm_radeon_cmd_header_t
*cmd
= NULL
;
206 start_packet3(CP_PACKET3(R300_PACKET3_3D_DRAW_INDX_2
, 0), 0);
207 e32(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (vertex_count
<< 16) | type
| R300_VAP_VF_CNTL__INDEX_SIZE_32bit
);
209 start_packet3(CP_PACKET3(R300_PACKET3_INDX_BUFFER
, 2), 2);
210 e32(R300_EB_UNK1
| (0 << 16) | R300_EB_UNK2
);
215 static void r300EmitAOS(r300ContextPtr rmesa
, GLuint nr
, GLuint offset
)
217 int sz
= 1 + (nr
>> 1) * 3 + (nr
& 1) * 2;
219 int cmd_reserved
= 0;
221 drm_radeon_cmd_header_t
*cmd
= NULL
;
223 if (RADEON_DEBUG
& DEBUG_VERTS
)
224 fprintf(stderr
, "%s: nr=%d, ofs=0x%08x\n", __FUNCTION__
, nr
,
227 start_packet3(CP_PACKET3(R300_PACKET3_3D_LOAD_VBPNTR
, sz
- 1), sz
- 1);
230 for (i
= 0; i
+ 1 < nr
; i
+= 2) {
231 e32((rmesa
->state
.aos
[i
].aos_size
<< 0) |
232 (rmesa
->state
.aos
[i
].aos_stride
<< 8) |
233 (rmesa
->state
.aos
[i
+ 1].aos_size
<< 16) |
234 (rmesa
->state
.aos
[i
+ 1].aos_stride
<< 24));
236 e32(rmesa
->state
.aos
[i
].aos_offset
+ offset
* 4 * rmesa
->state
.aos
[i
].aos_stride
);
237 e32(rmesa
->state
.aos
[i
+ 1].aos_offset
+ offset
* 4 * rmesa
->state
.aos
[i
+ 1].aos_stride
);
241 e32((rmesa
->state
.aos
[nr
- 1].aos_size
<< 0) |
242 (rmesa
->state
.aos
[nr
- 1].aos_stride
<< 8));
243 e32(rmesa
->state
.aos
[nr
- 1].aos_offset
+ offset
* 4 * rmesa
->state
.aos
[nr
- 1].aos_stride
);
247 static void r300FireAOS(r300ContextPtr rmesa
, int vertex_count
, int type
)
249 int cmd_reserved
= 0;
251 drm_radeon_cmd_header_t
*cmd
= NULL
;
253 start_packet3(CP_PACKET3(R300_PACKET3_3D_DRAW_VBUF_2
, 0), 0);
254 e32(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST
| (vertex_count
<< 16) | type
);
257 static void r300RunRenderPrimitive(r300ContextPtr rmesa
, GLcontext
* ctx
,
258 int start
, int end
, int prim
)
261 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
262 struct vertex_buffer
*vb
= &tnl
->vb
;
264 type
= r300PrimitiveType(rmesa
, prim
);
265 num_verts
= r300NumVerts(rmesa
, end
- start
, prim
);
267 if (type
< 0 || num_verts
<= 0)
271 r300EmitAOS(rmesa
, rmesa
->state
.aos_count
, start
);
272 if (num_verts
> 65535) {
273 /* not implemented yet */
274 WARN_ONCE("Too many elts\n");
277 r300EmitElts(ctx
, vb
->Elts
, num_verts
);
278 r300FireEB(rmesa
, rmesa
->state
.elt_dma
.aos_offset
, num_verts
, type
);
280 r300EmitAOS(rmesa
, rmesa
->state
.aos_count
, start
);
281 r300FireAOS(rmesa
, num_verts
, type
);
285 static GLboolean
r300RunRender(GLcontext
* ctx
,
286 struct tnl_pipeline_stage
*stage
)
288 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
290 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
291 struct vertex_buffer
*vb
= &tnl
->vb
;
294 if (RADEON_DEBUG
& DEBUG_PRIMS
)
295 fprintf(stderr
, "%s\n", __FUNCTION__
);
297 r300UpdateShaders(rmesa
);
298 if (r300EmitArrays(ctx
))
301 r300UpdateShaderStates(rmesa
);
303 r300EmitCacheFlush(rmesa
);
304 r300EmitState(rmesa
);
306 for (i
= 0; i
< vb
->PrimitiveCount
; i
++) {
307 GLuint prim
= _tnl_translate_prim(&vb
->Primitive
[i
]);
308 GLuint start
= vb
->Primitive
[i
].start
;
309 GLuint end
= vb
->Primitive
[i
].start
+ vb
->Primitive
[i
].count
;
310 r300RunRenderPrimitive(rmesa
, ctx
, start
, end
, prim
);
313 r300EmitCacheFlush(rmesa
);
319 r300ReleaseArrays(ctx
);
324 #define FALLBACK_IF(expr) \
327 if (1 || RADEON_DEBUG & DEBUG_FALLBACKS) \
328 WARN_ONCE("Software fallback:%s\n", \
330 return R300_FALLBACK_RAST; \
334 static int r300Fallback(GLcontext
* ctx
)
336 r300ContextPtr r300
= R300_CONTEXT(ctx
);
337 struct r300_fragment_program
*fp
= (struct r300_fragment_program
*)
338 (char *)ctx
->FragmentProgram
._Current
;
342 r300TranslateFragmentShader(r300
, fp
);
343 FALLBACK_IF(!fp
->translated
);
346 FALLBACK_IF(ctx
->RenderMode
!= GL_RENDER
);
348 FALLBACK_IF(ctx
->Stencil
._TestTwoSide
349 && (ctx
->Stencil
.Ref
[0] != ctx
->Stencil
.Ref
[1]
350 || ctx
->Stencil
.ValueMask
[0] !=
351 ctx
->Stencil
.ValueMask
[1]
352 || ctx
->Stencil
.WriteMask
[0] !=
353 ctx
->Stencil
.WriteMask
[1]));
355 FALLBACK_IF(ctx
->Color
.ColorLogicOpEnabled
);
357 if (ctx
->Extensions
.NV_point_sprite
|| ctx
->Extensions
.ARB_point_sprite
)
358 FALLBACK_IF(ctx
->Point
.PointSprite
);
360 if (!r300
->disable_lowimpact_fallback
) {
361 FALLBACK_IF(ctx
->Polygon
.StippleFlag
);
362 FALLBACK_IF(ctx
->Multisample
.Enabled
);
363 FALLBACK_IF(ctx
->Line
.StippleFlag
);
364 FALLBACK_IF(ctx
->Line
.SmoothFlag
);
365 FALLBACK_IF(ctx
->Point
.SmoothFlag
);
368 return R300_FALLBACK_NONE
;
371 static GLboolean
r300RunNonTCLRender(GLcontext
* ctx
,
372 struct tnl_pipeline_stage
*stage
)
374 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
376 if (RADEON_DEBUG
& DEBUG_PRIMS
)
377 fprintf(stderr
, "%s\n", __FUNCTION__
);
379 if (r300Fallback(ctx
) >= R300_FALLBACK_RAST
)
382 if (!(rmesa
->radeon
.radeonScreen
->chip_flags
& RADEON_CHIPSET_TCL
))
385 return r300RunRender(ctx
, stage
);
388 static GLboolean
r300RunTCLRender(GLcontext
* ctx
,
389 struct tnl_pipeline_stage
*stage
)
391 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
392 struct r300_vertex_program
*vp
;
394 hw_tcl_on
= future_hw_tcl_on
;
396 if (RADEON_DEBUG
& DEBUG_PRIMS
)
397 fprintf(stderr
, "%s\n", __FUNCTION__
);
399 if (hw_tcl_on
== GL_FALSE
)
402 if (r300Fallback(ctx
) >= R300_FALLBACK_TCL
) {
403 hw_tcl_on
= GL_FALSE
;
407 r300UpdateShaders(rmesa
);
409 vp
= (struct r300_vertex_program
*)CURRENT_VERTEX_SHADER(ctx
);
410 if (vp
->native
== GL_FALSE
) {
411 hw_tcl_on
= GL_FALSE
;
415 return r300RunRender(ctx
, stage
);
418 const struct tnl_pipeline_stage _r300_render_stage
= {
419 "r300 Hardware Rasterization",
427 const struct tnl_pipeline_stage _r300_tcl_stage
= {
428 "r300 Hardware Transform, Clipping and Lighting",