r300_render.c revision 622b31925b6a68b496cd65c627b8a1ed7e811cc3
1/*
2 * Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23#include "draw/draw_pipe.h"
24#include "draw/draw_vbuf.h"
25#include "util/u_memory.h"
26
27#include "r300_cs.h"
28#include "r300_context.h"
29#include "r300_emit.h"
30#include "r300_reg.h"
31#include "r300_state_derived.h"
32
33/* r300_render: Vertex and index buffer primitive emission. */
34#define R300_MAX_VBO_SIZE  (1024 * 1024)
35
36struct r300_render {
37    /* Parent class */
38    struct vbuf_render base;
39
40    /* Pipe context */
41    struct r300_context* r300;
42
43    /* Vertex information */
44    size_t vertex_size;
45    unsigned prim;
46    unsigned hwprim;
47
48    /* VBO */
49    struct pipe_buffer* vbo;
50    size_t vbo_size;
51    size_t vbo_offset;
52    size_t vbo_max_used;
53    void * vbo_ptr;
54};
55
56static INLINE struct r300_render*
57r300_render(struct vbuf_render* render)
58{
59    return (struct r300_render*)render;
60}
61
62static const struct vertex_info*
63r300_render_get_vertex_info(struct vbuf_render* render)
64{
65    struct r300_render* r300render = r300_render(render);
66    struct r300_context* r300 = r300render->r300;
67
68    r300_update_derived_state(r300);
69
70    return &r300->vertex_info.vinfo;
71}
72
73static boolean r300_render_allocate_vertices(struct vbuf_render* render,
74                                                   ushort vertex_size,
75                                                   ushort count)
76{
77    struct r300_render* r300render = r300_render(render);
78    struct r300_context* r300 = r300render->r300;
79    struct pipe_screen* screen = r300->context.screen;
80    size_t size = (size_t)vertex_size * (size_t)count;
81
82    if (size + r300render->vbo_offset > r300render->vbo_size)
83    {
84        r300render->vbo = pipe_buffer_create(screen,
85                                             64,
86                                             PIPE_BUFFER_USAGE_VERTEX,
87                                             R300_MAX_VBO_SIZE);
88        r300render->vbo_size = R300_MAX_VBO_SIZE;
89    }
90
91    r300render->vertex_size = vertex_size;
92    r300->vbo = r300render->vbo;
93    r300->vbo_offset = r300render->vbo_offset;
94
95    return (r300render->vbo) ? TRUE : FALSE;
96}
97
98static void* r300_render_map_vertices(struct vbuf_render* render)
99{
100    struct r300_render* r300render = r300_render(render);
101    struct pipe_screen* screen = r300render->r300->context.screen;
102
103    r300render->vbo_ptr = pipe_buffer_map(screen, r300render->vbo,
104                                          PIPE_BUFFER_USAGE_CPU_WRITE);
105
106    return (r300render->vbo_ptr + r300render->vbo_offset);
107}
108
109static void r300_render_unmap_vertices(struct vbuf_render* render,
110                                             ushort min,
111                                             ushort max)
112{
113    struct r300_render* r300render = r300_render(render);
114    struct pipe_screen* screen = r300render->r300->context.screen;
115    CS_LOCALS(r300render->r300);
116    BEGIN_CS(2);
117    OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max);
118    END_CS;
119
120    r300render->vbo_max_used = MAX2(r300render->vbo_max_used,
121                                    r300render->vertex_size * (max + 1));
122    pipe_buffer_unmap(screen, r300render->vbo);
123}
124
125static void r300_render_release_vertices(struct vbuf_render* render)
126{
127    struct r300_render* r300render = r300_render(render);
128    struct r300_context* r300 = r300render->r300;
129
130    r300render->vbo_offset += r300render->vbo_max_used;
131    r300render->vbo_max_used = 0;
132    r300->vbo = NULL;
133}
134
135static boolean r300_render_set_primitive(struct vbuf_render* render,
136                                               unsigned prim)
137{
138    struct r300_render* r300render = r300_render(render);
139    r300render->prim = prim;
140
141    switch (prim) {
142        case PIPE_PRIM_POINTS:
143            r300render->hwprim = R300_VAP_VF_CNTL__PRIM_POINTS;
144            break;
145        case PIPE_PRIM_LINES:
146            r300render->hwprim = R300_VAP_VF_CNTL__PRIM_LINES;
147            break;
148        case PIPE_PRIM_LINE_LOOP:
149            r300render->hwprim = R300_VAP_VF_CNTL__PRIM_LINE_LOOP;
150            break;
151        case PIPE_PRIM_LINE_STRIP:
152            r300render->hwprim = R300_VAP_VF_CNTL__PRIM_LINE_STRIP;
153            break;
154        case PIPE_PRIM_TRIANGLES:
155            r300render->hwprim = R300_VAP_VF_CNTL__PRIM_TRIANGLES;
156            break;
157        case PIPE_PRIM_TRIANGLE_STRIP:
158            r300render->hwprim = R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP;
159            break;
160        case PIPE_PRIM_TRIANGLE_FAN:
161            r300render->hwprim = R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN;
162            break;
163        case PIPE_PRIM_QUADS:
164            r300render->hwprim = R300_VAP_VF_CNTL__PRIM_QUADS;
165            break;
166        case PIPE_PRIM_QUAD_STRIP:
167            r300render->hwprim = R300_VAP_VF_CNTL__PRIM_QUAD_STRIP;
168            break;
169        case PIPE_PRIM_POLYGON:
170            r300render->hwprim = R300_VAP_VF_CNTL__PRIM_POLYGON;
171            break;
172        default:
173            return FALSE;
174            break;
175    }
176
177    return TRUE;
178}
179
180static void r300_prepare_render(struct r300_render* render, unsigned count)
181{
182    struct r300_context* r300 = render->r300;
183
184    CS_LOCALS(r300);
185
186    r300_emit_dirty_state(r300);
187}
188
189static void r300_render_draw_arrays(struct vbuf_render* render,
190                                          unsigned start,
191                                          unsigned count)
192{
193    struct r300_render* r300render = r300_render(render);
194    struct r300_context* r300 = r300render->r300;
195
196    CS_LOCALS(r300);
197
198    r300_prepare_render(r300render, count);
199
200    DBG(r300, DBG_DRAW, "r300: Doing vbuf render, count %d\n", count);
201
202    BEGIN_CS(2);
203    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
204    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
205           r300render->hwprim);
206    END_CS;
207}
208
209static void r300_render_draw(struct vbuf_render* render,
210                                   const ushort* indices,
211                                   uint count)
212{
213    struct r300_render* r300render = r300_render(render);
214    struct r300_context* r300 = r300render->r300;
215    struct pipe_screen* screen = r300->context.screen;
216    struct pipe_buffer* index_buffer;
217    void* index_map;
218    int i;
219    uint32_t index;
220
221    CS_LOCALS(r300);
222
223    r300_prepare_render(r300render, count);
224
225    /* Send our indices into an index buffer. */
226    index_buffer = pipe_buffer_create(screen, 64, PIPE_BUFFER_USAGE_VERTEX,
227                                      count * 2);
228    if (!index_buffer) {
229        return;
230    }
231
232    BEGIN_CS(2 + (count+1)/2);
233    OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (count+1)/2);
234    OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
235           r300render->hwprim);
236    for (i = 0; i < count-1; i += 2) {
237        OUT_CS(indices[i+1] << 16 | indices[i]);
238    }
239    if (count % 2) {
240        OUT_CS(indices[count-1]);
241    }
242    END_CS;
243}
244
245static void r300_render_destroy(struct vbuf_render* render)
246{
247    FREE(render);
248}
249
250static struct vbuf_render* r300_render_create(struct r300_context* r300)
251{
252    struct r300_render* r300render = CALLOC_STRUCT(r300_render);
253
254    r300render->r300 = r300;
255
256    /* XXX find real numbers plz */
257    r300render->base.max_vertex_buffer_bytes = 128 * 1024;
258    r300render->base.max_indices = 16 * 1024;
259
260    r300render->base.get_vertex_info = r300_render_get_vertex_info;
261    r300render->base.allocate_vertices = r300_render_allocate_vertices;
262    r300render->base.map_vertices = r300_render_map_vertices;
263    r300render->base.unmap_vertices = r300_render_unmap_vertices;
264    r300render->base.set_primitive = r300_render_set_primitive;
265    r300render->base.draw = r300_render_draw;
266    r300render->base.draw_arrays = r300_render_draw_arrays;
267    r300render->base.release_vertices = r300_render_release_vertices;
268    r300render->base.destroy = r300_render_destroy;
269
270    r300render->vbo = NULL;
271    r300render->vbo_size = 0;
272    r300render->vbo_offset = 0;
273
274    return &r300render->base;
275}
276
277struct draw_stage* r300_draw_stage(struct r300_context* r300)
278{
279    struct vbuf_render* render;
280    struct draw_stage* stage;
281
282    render = r300_render_create(r300);
283
284    if (!render) {
285        return NULL;
286    }
287
288    stage = draw_vbuf_stage(r300->draw, render);
289
290    if (!stage) {
291        render->destroy(render);
292        return NULL;
293    }
294
295    draw_set_render(r300->draw, render);
296
297    return stage;
298}
299