r600g: modify index buffers for sizes the hw can't deal with.
[mesa.git] / src / gallium / drivers / r600 / r600_draw.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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 * Authors:
24 * Jerome Glisse
25 * Corbin Simpson
26 */
27 #include <stdio.h>
28 #include <errno.h>
29 #include <pipe/p_screen.h>
30 #include <util/u_format.h>
31 #include <util/u_math.h>
32 #include <util/u_inlines.h>
33 #include <util/u_memory.h>
34 #include <util/u_index_modify.h>
35 #include "radeon.h"
36 #include "r600_screen.h"
37 #include "r600_context.h"
38 #include "r600_resource.h"
39 #include "r600_state_inlines.h"
40
41 static void r600_translate_index_buffer(struct r600_context *r600,
42 struct pipe_resource **index_buffer,
43 unsigned *index_size, unsigned index_offset,
44 unsigned *start, unsigned count)
45 {
46 switch (*index_size) {
47 case 1:
48 util_shorten_ubyte_elts(&r600->context, index_buffer, index_offset, *start, count);
49 *index_size = 2;
50 *start = 0;
51 break;
52
53 case 2:
54 if (*start % 2 != 0 || index_offset) {
55 util_rebuild_ushort_elts(&r600->context, index_buffer, index_offset, *start, count);
56 *start = 0;
57 }
58 break;
59
60 case 4:
61 if (index_offset) {
62 util_rebuild_uint_elts(&r600->context, index_buffer, index_offset, *start, count);
63 *start = 0;
64 }
65 break;
66 }
67 }
68
69 static int r600_draw_common(struct r600_draw *draw)
70 {
71 struct r600_context *rctx = r600_context(draw->ctx);
72 /* FIXME vs_resource */
73 struct radeon_state *vs_resource;
74 struct r600_resource *rbuffer;
75 unsigned i, j, offset, format, prim;
76 u32 vgt_dma_index_type, vgt_draw_initiator;
77 struct pipe_vertex_buffer *vertex_buffer;
78 int r;
79
80 r = r600_context_hw_states(draw->ctx);
81 if (r)
82 return r;
83 switch (draw->index_size) {
84 case 2:
85 vgt_draw_initiator = S_0287F0_SOURCE_SELECT(V_0287F0_DI_SRC_SEL_DMA);
86 vgt_dma_index_type = 0;
87 break;
88 case 4:
89 vgt_draw_initiator = S_0287F0_SOURCE_SELECT(V_0287F0_DI_SRC_SEL_DMA);
90 vgt_dma_index_type = 1;
91 break;
92 case 0:
93 vgt_draw_initiator = S_0287F0_SOURCE_SELECT(V_0287F0_DI_SRC_SEL_AUTO_INDEX);
94 vgt_dma_index_type = 0;
95 break;
96 default:
97 fprintf(stderr, "%s %d unsupported index size %d\n", __func__, __LINE__, draw->index_size);
98 return -EINVAL;
99 }
100 r = r600_conv_pipe_prim(draw->mode, &prim);
101 if (r)
102 return r;
103
104 /* rebuild vertex shader if input format changed */
105 r = r600_pipe_shader_update(draw->ctx, rctx->vs_shader);
106 if (r)
107 return r;
108 r = r600_pipe_shader_update(draw->ctx, rctx->ps_shader);
109 if (r)
110 return r;
111 radeon_draw_bind(&rctx->draw, &rctx->vs_shader->rstate[0]);
112 radeon_draw_bind(&rctx->draw, &rctx->ps_shader->rstate[0]);
113
114 for (i = 0 ; i < rctx->vs_nresource; i++) {
115 radeon_state_fini(&rctx->vs_resource[i]);
116 }
117 for (i = 0 ; i < rctx->vertex_elements->count; i++) {
118 vs_resource = &rctx->vs_resource[i];
119 j = rctx->vertex_elements->elements[i].vertex_buffer_index;
120 vertex_buffer = &rctx->vertex_buffer[j];
121 rbuffer = (struct r600_resource*)vertex_buffer->buffer;
122 offset = rctx->vertex_elements->elements[i].src_offset + vertex_buffer->buffer_offset;
123 format = r600_translate_colorformat(rctx->vertex_elements->elements[i].src_format);
124
125 rctx->vtbl->vs_resource(rctx, i, rbuffer, offset, vertex_buffer->stride, format);
126 radeon_draw_bind(&rctx->draw, vs_resource);
127 }
128 rctx->vs_nresource = rctx->vertex_elements->count;
129 /* FIXME start need to change winsys */
130 rctx->vtbl->vgt_init(draw, vgt_draw_initiator);
131 radeon_draw_bind(&rctx->draw, &draw->draw);
132
133 rctx->vtbl->vgt_prim(draw, prim, vgt_dma_index_type);
134 radeon_draw_bind(&rctx->draw, &draw->vgt);
135
136 r = radeon_ctx_set_draw(rctx->ctx, &rctx->draw);
137 if (r == -EBUSY) {
138 r600_flush(draw->ctx, 0, NULL);
139 r = radeon_ctx_set_draw(rctx->ctx, &rctx->draw);
140 }
141
142 radeon_state_fini(&draw->draw);
143
144 return r;
145 }
146
147 void r600_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *info)
148 {
149 struct r600_context *rctx = r600_context(ctx);
150 struct r600_draw draw;
151 int r;
152
153 assert(info->index_bias == 0);
154
155 memset(&draw, 0, sizeof(draw));
156
157 if (rctx->any_user_vbs) {
158 r600_upload_user_buffers(rctx);
159 rctx->any_user_vbs = false;
160 }
161
162 draw.ctx = ctx;
163 draw.mode = info->mode;
164 draw.start = info->start;
165 draw.count = info->count;
166 if (info->indexed && rctx->index_buffer.buffer) {
167 draw.start += rctx->index_buffer.offset / rctx->index_buffer.index_size;
168 draw.min_index = info->min_index;
169 draw.max_index = info->max_index;
170
171 r600_translate_index_buffer(rctx, &rctx->index_buffer.buffer,
172 &rctx->index_buffer.index_size,
173 rctx->index_buffer.offset, &draw.start,
174 info->count);
175
176 fprintf(stderr,"draw start is %d\n", draw.start);
177 draw.index_size = rctx->index_buffer.index_size;
178 draw.index_buffer = rctx->index_buffer.buffer;
179 draw.index_buffer_offset = rctx->index_buffer.offset;
180
181 r600_upload_index_buffer(rctx, &draw);
182 }
183 else {
184 draw.index_size = 0;
185 draw.index_buffer = NULL;
186 draw.min_index = 0;
187 draw.max_index = 0xffffff;
188 draw.index_buffer_offset = 0;
189 }
190 r = r600_draw_common(&draw);
191 if (r)
192 fprintf(stderr,"draw common failed %d\n", r);
193 }