r300_render.c revision 6a448a525baf81173f92ee8c3074b98baa54397b
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_context.h" 24#include "draw/draw_vbuf.h" 25 26#include "pipe/p_inlines.h" 27 28#include "util/u_memory.h" 29 30#include "r300_cs.h" 31#include "r300_context.h" 32#include "r300_emit.h" 33#include "r300_reg.h" 34#include "r300_state_derived.h" 35 36/* r300_render: Vertex and index buffer primitive emission. */ 37#define R300_MAX_VBO_SIZE (1024 * 1024) 38 39struct r300_render { 40 /* Parent class */ 41 struct vbuf_render base; 42 43 /* Pipe context */ 44 struct r300_context* r300; 45 46 /* Vertex information */ 47 size_t vertex_size; 48 unsigned prim; 49 unsigned hwprim; 50 51 /* VBO */ 52 struct pipe_buffer* vbo; 53 size_t vbo_size; 54 size_t vbo_offset; 55 size_t vbo_max_used; 56 void * vbo_ptr; 57}; 58 59static INLINE struct r300_render* 60r300_render(struct vbuf_render* render) 61{ 62 return (struct r300_render*)render; 63} 64 65static const struct vertex_info* 66r300_render_get_vertex_info(struct vbuf_render* render) 67{ 68 struct r300_render* r300render = r300_render(render); 69 struct r300_context* r300 = r300render->r300; 70 71 r300_update_derived_state(r300); 72 73 return &r300->vertex_info->vinfo; 74} 75 76static boolean r300_render_allocate_vertices(struct vbuf_render* render, 77 ushort vertex_size, 78 ushort count) 79{ 80 struct r300_render* r300render = r300_render(render); 81 struct r300_context* r300 = r300render->r300; 82 struct pipe_screen* screen = r300->context.screen; 83 size_t size = (size_t)vertex_size * (size_t)count; 84 85 if (size + r300render->vbo_offset > r300render->vbo_size) 86 { 87 pipe_buffer_reference(&r300->vbo, NULL); 88 r300render->vbo = pipe_buffer_create(screen, 89 64, 90 PIPE_BUFFER_USAGE_VERTEX, 91 R300_MAX_VBO_SIZE); 92 r300render->vbo_offset = 0; 93 r300render->vbo_size = R300_MAX_VBO_SIZE; 94 } 95 96 r300render->vertex_size = vertex_size; 97 r300->vbo = r300render->vbo; 98 r300->vbo_offset = r300render->vbo_offset; 99 100 return (r300render->vbo) ? TRUE : FALSE; 101} 102 103static void* r300_render_map_vertices(struct vbuf_render* render) 104{ 105 struct r300_render* r300render = r300_render(render); 106 struct pipe_screen* screen = r300render->r300->context.screen; 107 108 r300render->vbo_ptr = pipe_buffer_map(screen, r300render->vbo, 109 PIPE_BUFFER_USAGE_CPU_WRITE); 110 111 return (r300render->vbo_ptr + r300render->vbo_offset); 112} 113 114static void r300_render_unmap_vertices(struct vbuf_render* render, 115 ushort min, 116 ushort max) 117{ 118 struct r300_render* r300render = r300_render(render); 119 struct pipe_screen* screen = r300render->r300->context.screen; 120 CS_LOCALS(r300render->r300); 121 BEGIN_CS(2); 122 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max); 123 END_CS; 124 125 r300render->vbo_max_used = MAX2(r300render->vbo_max_used, 126 r300render->vertex_size * (max + 1)); 127 pipe_buffer_unmap(screen, r300render->vbo); 128} 129 130static void r300_render_release_vertices(struct vbuf_render* render) 131{ 132 struct r300_render* r300render = r300_render(render); 133 134 r300render->vbo_offset += r300render->vbo_max_used; 135 r300render->vbo_max_used = 0; 136} 137 138static boolean r300_render_set_primitive(struct vbuf_render* render, 139 unsigned prim) 140{ 141 struct r300_render* r300render = r300_render(render); 142 r300render->prim = prim; 143 144 switch (prim) { 145 case PIPE_PRIM_POINTS: 146 r300render->hwprim = R300_VAP_VF_CNTL__PRIM_POINTS; 147 break; 148 case PIPE_PRIM_LINES: 149 r300render->hwprim = R300_VAP_VF_CNTL__PRIM_LINES; 150 break; 151 case PIPE_PRIM_LINE_LOOP: 152 r300render->hwprim = R300_VAP_VF_CNTL__PRIM_LINE_LOOP; 153 break; 154 case PIPE_PRIM_LINE_STRIP: 155 r300render->hwprim = R300_VAP_VF_CNTL__PRIM_LINE_STRIP; 156 break; 157 case PIPE_PRIM_TRIANGLES: 158 r300render->hwprim = R300_VAP_VF_CNTL__PRIM_TRIANGLES; 159 break; 160 case PIPE_PRIM_TRIANGLE_STRIP: 161 r300render->hwprim = R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP; 162 break; 163 case PIPE_PRIM_TRIANGLE_FAN: 164 r300render->hwprim = R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN; 165 break; 166 case PIPE_PRIM_QUADS: 167 r300render->hwprim = R300_VAP_VF_CNTL__PRIM_QUADS; 168 break; 169 case PIPE_PRIM_QUAD_STRIP: 170 r300render->hwprim = R300_VAP_VF_CNTL__PRIM_QUAD_STRIP; 171 break; 172 case PIPE_PRIM_POLYGON: 173 r300render->hwprim = R300_VAP_VF_CNTL__PRIM_POLYGON; 174 break; 175 default: 176 return FALSE; 177 break; 178 } 179 180 return TRUE; 181} 182 183static void r300_prepare_render(struct r300_render* render, unsigned count) 184{ 185 struct r300_context* r300 = render->r300; 186 187 r300_emit_dirty_state(r300); 188} 189 190static void r300_render_draw_arrays(struct vbuf_render* render, 191 unsigned start, 192 unsigned count) 193{ 194 struct r300_render* r300render = r300_render(render); 195 struct r300_context* r300 = r300render->r300; 196 197 CS_LOCALS(r300); 198 199 r300_prepare_render(r300render, count); 200 201 DBG(r300, DBG_DRAW, "r300: Doing vbuf render, count %d\n", count); 202 203 BEGIN_CS(2); 204 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0); 205 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) | 206 r300render->hwprim); 207 END_CS; 208} 209 210static void r300_render_draw(struct vbuf_render* render, 211 const ushort* indices, 212 uint count) 213{ 214 struct r300_render* r300render = r300_render(render); 215 struct r300_context* r300 = r300render->r300; 216 int i; 217 218 CS_LOCALS(r300); 219 220 r300_prepare_render(r300render, count); 221 222 BEGIN_CS(2 + (count+1)/2); 223 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (count+1)/2); 224 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) | 225 r300render->hwprim); 226 for (i = 0; i < count-1; i += 2) { 227 OUT_CS(indices[i+1] << 16 | indices[i]); 228 } 229 if (count % 2) { 230 OUT_CS(indices[count-1]); 231 } 232 END_CS; 233} 234 235static void r300_render_destroy(struct vbuf_render* render) 236{ 237 FREE(render); 238} 239 240static struct vbuf_render* r300_render_create(struct r300_context* r300) 241{ 242 struct r300_render* r300render = CALLOC_STRUCT(r300_render); 243 244 r300render->r300 = r300; 245 246 /* XXX find real numbers plz */ 247 r300render->base.max_vertex_buffer_bytes = 128 * 1024; 248 r300render->base.max_indices = 16 * 1024; 249 250 r300render->base.get_vertex_info = r300_render_get_vertex_info; 251 r300render->base.allocate_vertices = r300_render_allocate_vertices; 252 r300render->base.map_vertices = r300_render_map_vertices; 253 r300render->base.unmap_vertices = r300_render_unmap_vertices; 254 r300render->base.set_primitive = r300_render_set_primitive; 255 r300render->base.draw = r300_render_draw; 256 r300render->base.draw_arrays = r300_render_draw_arrays; 257 r300render->base.release_vertices = r300_render_release_vertices; 258 r300render->base.destroy = r300_render_destroy; 259 260 r300render->vbo = NULL; 261 r300render->vbo_size = 0; 262 r300render->vbo_offset = 0; 263 264 return &r300render->base; 265} 266 267struct draw_stage* r300_draw_stage(struct r300_context* r300) 268{ 269 struct vbuf_render* render; 270 struct draw_stage* stage; 271 272 render = r300_render_create(r300); 273 274 if (!render) { 275 return NULL; 276 } 277 278 stage = draw_vbuf_stage(r300->draw, render); 279 280 if (!stage) { 281 render->destroy(render); 282 return NULL; 283 } 284 285 draw_set_render(r300->draw, render); 286 287 return stage; 288} 289