gallium: added new u_draw_quad.c and u_gen_mipmap.c files.
[mesa.git] / src / gallium / auxiliary / util / u_draw_quad.c
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 "pipe/p_inlines.h"
32 #include "pipe/p_winsys.h"
33 #include "util/u_draw_quad.h"
34
35
36 /**
37 * Draw screen-aligned textured quad.
38 */
39 void
40 util_draw_texquad(struct pipe_context *pipe,
41 float x0, float y0, float x1, float y1, float z)
42 {
43 struct pipe_buffer *vbuf;
44 struct pipe_vertex_buffer vbuffer;
45 struct pipe_vertex_element velement;
46 uint numAttribs = 2, vertexBytes, i, j;
47 float *v;
48
49 vertexBytes = 4 * (4 * numAttribs * sizeof(float));
50
51 /* XXX create one-time */
52 vbuf = pipe->winsys->buffer_create(pipe->winsys, 32,
53 PIPE_BUFFER_USAGE_VERTEX, vertexBytes);
54 assert(vbuf);
55
56 v = (float *) pipe->winsys->buffer_map(pipe->winsys, vbuf,
57 PIPE_BUFFER_USAGE_CPU_WRITE);
58
59 /*
60 * Load vertex buffer
61 */
62 for (i = j = 0; i < 4; i++) {
63 v[j + 2] = z; /* z */
64 v[j + 3] = 1.0; /* w */
65 v[j + 6] = 0.0; /* r */
66 v[j + 7] = 1.0; /* q */
67 j += 8;
68 }
69
70 v[0] = x0;
71 v[1] = y0;
72 v[4] = 0.0; /*s*/
73 v[5] = 0.0; /*t*/
74
75 v[8] = x1;
76 v[9] = y0;
77 v[12] = 1.0;
78 v[13] = 0.0;
79
80 v[16] = x1;
81 v[17] = y1;
82 v[20] = 1.0;
83 v[21] = 1.0;
84
85 v[24] = x0;
86 v[25] = y1;
87 v[28] = 0.0;
88 v[29] = 1.0;
89
90 pipe->winsys->buffer_unmap(pipe->winsys, vbuf);
91
92 /* tell pipe about the vertex buffer */
93 vbuffer.buffer = vbuf;
94 vbuffer.pitch = numAttribs * 4 * sizeof(float); /* vertex size */
95 vbuffer.buffer_offset = 0;
96 pipe->set_vertex_buffer(pipe, 0, &vbuffer);
97
98 /* tell pipe about the vertex attributes */
99 for (i = 0; i < numAttribs; i++) {
100 velement.src_offset = i * 4 * sizeof(float);
101 velement.vertex_buffer_index = 0;
102 velement.src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
103 velement.nr_components = 4;
104 pipe->set_vertex_element(pipe, i, &velement);
105 }
106
107 /* draw */
108 pipe->draw_arrays(pipe, PIPE_PRIM_TRIANGLE_FAN, 0, 4);
109
110 /* XXX: do one-time */
111 pipe_buffer_reference(pipe->winsys, &vbuf, NULL);
112 }