vl_vertex_buffers.c revision 1482b9a7f39ec8875fcd4137c35b3cb9ac0c0934
1/************************************************************************** 2 * 3 * Copyright 2010 Christian König 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28#include <assert.h> 29#include <pipe/p_context.h> 30#include <pipe/p_screen.h> 31#include <util/u_memory.h> 32#include <util/u_inlines.h> 33#include <util/u_format.h> 34#include "vl_vertex_buffers.h" 35#include "vl_types.h" 36 37/* vertices for a quad covering a block */ 38static const struct quadf const_quad = { 39 {0.0f, 1.0f}, {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f} 40}; 41 42struct pipe_vertex_buffer 43vl_vb_upload_quads(struct pipe_context *pipe, unsigned max_blocks) 44{ 45 struct pipe_vertex_buffer quad; 46 struct pipe_transfer *buf_transfer; 47 struct quadf *v; 48 49 unsigned i; 50 51 assert(pipe); 52 assert(max_blocks); 53 54 /* create buffer */ 55 quad.stride = sizeof(struct vertex2f); 56 quad.max_index = 4 * max_blocks - 1; 57 quad.buffer_offset = 0; 58 quad.buffer = pipe_buffer_create 59 ( 60 pipe->screen, 61 PIPE_BIND_VERTEX_BUFFER, 62 sizeof(struct vertex2f) * 4 * max_blocks 63 ); 64 65 if(!quad.buffer) 66 return quad; 67 68 /* and fill it */ 69 v = pipe_buffer_map 70 ( 71 pipe, 72 quad.buffer, 73 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 74 &buf_transfer 75 ); 76 77 for ( i = 0; i < max_blocks; ++i) 78 memcpy(v + i, &const_quad, sizeof(const_quad)); 79 80 pipe_buffer_unmap(pipe, quad.buffer, buf_transfer); 81 82 return quad; 83} 84 85struct pipe_vertex_element 86vl_vb_get_quad_vertex_element() 87{ 88 struct pipe_vertex_element element; 89 90 /* setup rectangle element */ 91 element.src_offset = 0; 92 element.instance_divisor = 0; 93 element.vertex_buffer_index = 0; 94 element.src_format = PIPE_FORMAT_R32G32_FLOAT; 95 96 return element; 97} 98 99unsigned 100vl_vb_element_helper(struct pipe_vertex_element* elements, unsigned num_elements, 101 unsigned vertex_buffer_index) 102{ 103 unsigned i, offset = 0; 104 105 assert(elements && num_elements); 106 107 for ( i = 0; i < num_elements; ++i ) { 108 elements[i].src_offset = offset; 109 elements[i].instance_divisor = 0; 110 elements[i].vertex_buffer_index = vertex_buffer_index; 111 offset += util_format_get_blocksize(elements[i].src_format); 112 } 113 114 return offset; 115} 116 117struct pipe_vertex_buffer 118vl_vb_init(struct vl_vertex_buffer *buffer, struct pipe_context *pipe, 119 unsigned max_blocks, unsigned num_elements, unsigned stride) 120{ 121 struct pipe_vertex_buffer buf; 122 123 assert(buffer); 124 125 buffer->num_verts = 0; 126 buffer->num_elements = num_elements; 127 128 buf.stride = stride; 129 buf.max_index = 4 * max_blocks - 1; 130 buf.buffer_offset = 0; 131 buf.buffer = pipe_buffer_create 132 ( 133 pipe->screen, 134 PIPE_BIND_VERTEX_BUFFER, 135 stride * 4 * max_blocks 136 ); 137 138 pipe_resource_reference(&buffer->resource, buf.buffer); 139 140 vl_vb_map(buffer, pipe); 141 142 return buf; 143} 144 145void 146vl_vb_map(struct vl_vertex_buffer *buffer, struct pipe_context *pipe) 147{ 148 assert(buffer && pipe); 149 150 buffer->vectors = pipe_buffer_map 151 ( 152 pipe, 153 buffer->resource, 154 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 155 &buffer->transfer 156 ); 157} 158 159void 160vl_vb_unmap(struct vl_vertex_buffer *buffer, struct pipe_context *pipe) 161{ 162 assert(buffer && pipe); 163 164 pipe_buffer_unmap(pipe, buffer->resource, buffer->transfer); 165} 166 167unsigned 168vl_vb_restart(struct vl_vertex_buffer *buffer) 169{ 170 assert(buffer); 171 172 unsigned todo = buffer->num_verts; 173 buffer->num_verts = 0; 174 return todo; 175} 176 177void 178vl_vb_cleanup(struct vl_vertex_buffer *buffer) 179{ 180 assert(buffer); 181 182 pipe_resource_reference(&buffer->resource, NULL); 183} 184