u_draw_quad.c revision 28486880ca3ec39419ccee0cb1a3bedc9ef7117c
1/************************************************************************** 2 * 3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas. 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 29#include "pipe/p_context.h" 30#include "pipe/p_defines.h" 31#include "util/u_inlines.h" 32#include "util/u_draw_quad.h" 33 34 35/** 36 * Draw a simple vertex buffer / primitive. 37 * Limited to float[4] vertex attribs, tightly packed. 38 */ 39void 40util_draw_vertex_buffer(struct pipe_context *pipe, 41 struct pipe_buffer *vbuf, 42 uint offset, 43 uint prim_type, 44 uint num_verts, 45 uint num_attribs) 46{ 47 struct pipe_vertex_buffer vbuffer; 48 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS]; 49 uint i; 50 51 assert(num_attribs <= PIPE_MAX_ATTRIBS); 52 53 /* tell pipe about the vertex buffer */ 54 memset(&vbuffer, 0, sizeof(vbuffer)); 55 vbuffer.buffer = vbuf; 56 vbuffer.stride = num_attribs * 4 * sizeof(float); /* vertex size */ 57 vbuffer.buffer_offset = offset; 58 vbuffer.max_index = num_verts - 1; 59 pipe->set_vertex_buffers(pipe, 1, &vbuffer); 60 61 /* tell pipe about the vertex attributes */ 62 for (i = 0; i < num_attribs; i++) { 63 velements[i].src_offset = i * 4 * sizeof(float); 64 velements[i].instance_divisor = 0; 65 velements[i].vertex_buffer_index = 0; 66 velements[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 67 velements[i].nr_components = 4; 68 } 69 pipe->set_vertex_elements(pipe, num_attribs, velements); 70 71 /* draw */ 72 pipe->draw_arrays(pipe, prim_type, 0, num_verts); 73} 74 75 76 77/** 78 * Draw screen-aligned textured quad. 79 * Note: this function allocs/destroys a vertex buffer and isn't especially 80 * efficient. 81 */ 82void 83util_draw_texquad(struct pipe_context *pipe, 84 float x0, float y0, float x1, float y1, float z) 85{ 86 struct pipe_buffer *vbuf; 87 uint numAttribs = 2, vertexBytes, i, j; 88 89 vertexBytes = 4 * (4 * numAttribs * sizeof(float)); 90 91 /* XXX create one-time */ 92 vbuf = pipe_buffer_create(pipe->screen, 32, 93 PIPE_BUFFER_USAGE_VERTEX, vertexBytes); 94 if (vbuf) { 95 float *v = (float *) pipe_buffer_map(pipe->screen, vbuf, 96 PIPE_BUFFER_USAGE_CPU_WRITE); 97 if (v) { 98 /* 99 * Load vertex buffer 100 */ 101 for (i = j = 0; i < 4; i++) { 102 v[j + 2] = z; /* z */ 103 v[j + 3] = 1.0; /* w */ 104 v[j + 6] = 0.0; /* r */ 105 v[j + 7] = 1.0; /* q */ 106 j += 8; 107 } 108 109 v[0] = x0; 110 v[1] = y0; 111 v[4] = 0.0; /*s*/ 112 v[5] = 0.0; /*t*/ 113 114 v[8] = x1; 115 v[9] = y0; 116 v[12] = 1.0; 117 v[13] = 0.0; 118 119 v[16] = x1; 120 v[17] = y1; 121 v[20] = 1.0; 122 v[21] = 1.0; 123 124 v[24] = x0; 125 v[25] = y1; 126 v[28] = 0.0; 127 v[29] = 1.0; 128 129 pipe_buffer_unmap(pipe->screen, vbuf); 130 util_draw_vertex_buffer(pipe, vbuf, 0, PIPE_PRIM_TRIANGLE_FAN, 4, 2); 131 } 132 133 pipe_buffer_reference(&vbuf, NULL); 134 } 135} 136