2 * Copyright (C) 2009-2010 Francisco Jerez.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include "tnl/t_context.h"
28 #include "tnl/t_pipeline.h"
29 #include "tnl/t_vertex.h"
31 static enum tnl_attr_format
32 swtnl_get_format(int type
, int fields
) {
47 case GL_UNSIGNED_BYTE
:
50 return EMIT_4UB_4F_RGBA
;
59 static struct swtnl_attr_info
{
62 } swtnl_attrs
[VERT_ATTRIB_MAX
] = {
67 [VERT_ATTRIB_NORMAL
] = {
71 [VERT_ATTRIB_COLOR0
] = {
72 .type
= GL_UNSIGNED_BYTE
,
75 [VERT_ATTRIB_COLOR1
] = {
76 .type
= GL_UNSIGNED_BYTE
,
83 [VERT_ATTRIB_TEX0
] = {
87 [VERT_ATTRIB_TEX1
] = {
91 [VERT_ATTRIB_TEX2
] = {
95 [VERT_ATTRIB_TEX3
] = {
102 swtnl_choose_attrs(GLcontext
*ctx
)
104 struct nouveau_render_state
*render
= to_render_state(ctx
);
105 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
106 struct tnl_clipspace
*vtx
= &tnl
->clipspace
;
107 static struct tnl_attr_map map
[NUM_VERTEX_ATTRS
];
108 int fields
, i
, n
= 0;
111 render
->attr_count
= NUM_VERTEX_ATTRS
;
113 /* We always want non Ndc coords format */
114 tnl
->vb
.AttribPtr
[VERT_ATTRIB_POS
] = tnl
->vb
.ClipPtr
;
116 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
117 struct nouveau_attr_info
*ha
= &TAG(vertex_attrs
)[i
];
118 struct swtnl_attr_info
*sa
= &swtnl_attrs
[i
];
119 struct nouveau_array_state
*a
= &render
->attrs
[i
];
122 continue; /* Unsupported attribute. */
124 if (RENDERINPUTS_TEST(tnl
->render_inputs_bitset
, i
)) {
128 fields
= tnl
->vb
.AttribPtr
[i
]->size
;
130 map
[n
++] = (struct tnl_attr_map
) {
132 .format
= swtnl_get_format(sa
->type
, fields
),
135 render
->map
[ha
->vbo_index
] = i
;
142 _tnl_install_attrs(ctx
, map
, n
, NULL
, 0);
144 for (i
= 0; i
< vtx
->attr_count
; i
++) {
145 struct tnl_clipspace_attr
*ta
= &vtx
->attr
[i
];
146 struct nouveau_array_state
*a
= &render
->attrs
[ta
->attrib
];
148 a
->stride
= vtx
->vertex_size
;
149 a
->offset
= ta
->vertoffset
;
152 TAG(render_set_format
)(ctx
);
156 swtnl_alloc_vertices(GLcontext
*ctx
)
158 struct nouveau_swtnl_state
*swtnl
= &to_render_state(ctx
)->swtnl
;
160 nouveau_bo_ref(NULL
, &swtnl
->vbo
);
161 swtnl
->buf
= get_scratch_vbo(ctx
, RENDER_SCRATCH_SIZE
,
163 swtnl
->vertex_count
= 0;
167 swtnl_bind_vertices(GLcontext
*ctx
)
169 struct nouveau_render_state
*render
= to_render_state(ctx
);
170 struct nouveau_swtnl_state
*swtnl
= &render
->swtnl
;
173 for (i
= 0; i
< render
->attr_count
; i
++) {
174 int attr
= render
->map
[i
];
177 nouveau_bo_ref(swtnl
->vbo
,
178 &render
->attrs
[attr
].bo
);
181 TAG(render_bind_vertices
)(ctx
);
185 swtnl_unbind_vertices(GLcontext
*ctx
)
187 struct nouveau_render_state
*render
= to_render_state(ctx
);
190 for (i
= 0; i
< render
->attr_count
; i
++) {
191 int *attr
= &render
->map
[i
];
194 nouveau_bo_ref(NULL
, &render
->attrs
[*attr
].bo
);
199 render
->attr_count
= 0;
203 swtnl_flush_vertices(GLcontext
*ctx
)
205 struct nouveau_channel
*chan
= context_chan(ctx
);
206 struct nouveau_swtnl_state
*swtnl
= &to_render_state(ctx
)->swtnl
;
207 unsigned push
, start
= 0, count
= swtnl
->vertex_count
;
210 swtnl_bind_vertices(ctx
);
213 push
= get_max_vertices(ctx
, NULL
, chan
->pushbuf
->remaining
);
214 push
= MIN2(push
/ 12 * 12, count
);
222 BATCH_BEGIN(nvgl_primitive(swtnl
->primitive
));
223 EMIT_VBO(L
, ctx
, start
, 0, push
);
229 swtnl_alloc_vertices(ctx
);
232 /* TnL renderer entry points */
235 swtnl_start(GLcontext
*ctx
)
237 swtnl_choose_attrs(ctx
);
241 swtnl_finish(GLcontext
*ctx
)
243 swtnl_flush_vertices(ctx
);
244 swtnl_unbind_vertices(ctx
);
248 swtnl_primitive(GLcontext
*ctx
, GLenum mode
)
253 swtnl_reset_stipple(GLcontext
*ctx
)
257 /* Primitive rendering */
259 #define BEGIN_PRIMITIVE(p, n) \
260 struct nouveau_swtnl_state *swtnl = &to_render_state(ctx)->swtnl; \
261 int vertex_len = TNL_CONTEXT(ctx)->clipspace.vertex_size; \
263 if (swtnl->vertex_count + (n) > swtnl->vbo->size/vertex_len \
264 || (swtnl->vertex_count && swtnl->primitive != p)) \
265 swtnl_flush_vertices(ctx); \
267 swtnl->primitive = p;
269 #define OUT_VERTEX(i) do { \
270 memcpy(swtnl->buf + swtnl->vertex_count * vertex_len, \
271 _tnl_get_vertex(ctx, (i)), vertex_len); \
272 swtnl->vertex_count++; \
276 swtnl_points(GLcontext
*ctx
, GLuint first
, GLuint last
)
280 while (first
< last
) {
281 BEGIN_PRIMITIVE(GL_POINTS
, last
- first
);
283 count
= MIN2(swtnl
->vbo
->size
/ vertex_len
, last
- first
);
284 for (i
= 0; i
< count
; i
++)
285 OUT_VERTEX(first
+ i
);
292 swtnl_line(GLcontext
*ctx
, GLuint v1
, GLuint v2
)
294 BEGIN_PRIMITIVE(GL_LINES
, 2);
300 swtnl_triangle(GLcontext
*ctx
, GLuint v1
, GLuint v2
, GLuint v3
)
302 BEGIN_PRIMITIVE(GL_TRIANGLES
, 3);
309 swtnl_quad(GLcontext
*ctx
, GLuint v1
, GLuint v2
, GLuint v3
, GLuint v4
)
311 BEGIN_PRIMITIVE(GL_QUADS
, 4);
318 /* TnL initialization. */
320 TAG(swtnl_init
)(GLcontext
*ctx
)
322 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
324 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
325 tnl
->Driver
.Render
.Interp
= _tnl_interp
;
326 tnl
->Driver
.Render
.CopyPV
= _tnl_copy_pv
;
327 tnl
->Driver
.Render
.ClippedPolygon
= _tnl_RenderClippedPolygon
;
328 tnl
->Driver
.Render
.ClippedLine
= _tnl_RenderClippedLine
;
329 tnl
->Driver
.Render
.BuildVertices
= _tnl_build_vertices
;
331 tnl
->Driver
.Render
.Start
= swtnl_start
;
332 tnl
->Driver
.Render
.Finish
= swtnl_finish
;
333 tnl
->Driver
.Render
.PrimitiveNotify
= swtnl_primitive
;
334 tnl
->Driver
.Render
.ResetLineStipple
= swtnl_reset_stipple
;
336 tnl
->Driver
.Render
.Points
= swtnl_points
;
337 tnl
->Driver
.Render
.Line
= swtnl_line
;
338 tnl
->Driver
.Render
.Triangle
= swtnl_triangle
;
339 tnl
->Driver
.Render
.Quad
= swtnl_quad
;
341 _tnl_init_vertices(ctx
, tnl
->vb
.Size
,
342 NUM_VERTEX_ATTRS
* 4 * sizeof(GLfloat
));
343 _tnl_need_projected_coords(ctx
, GL_FALSE
);
344 _tnl_allow_vertex_fog(ctx
, GL_FALSE
);
347 swtnl_alloc_vertices(ctx
);
351 TAG(swtnl_destroy
)(GLcontext
*ctx
)
353 nouveau_bo_ref(NULL
, &to_render_state(ctx
)->swtnl
.vbo
);