Squashed commit of the following:
[profile/ivi/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 "util/u_inlines.h"
32 #include "util/u_draw_quad.h"
33 #include "util/u_memory.h"
34
35
36 /**
37  * Draw a simple vertex buffer / primitive.
38  * Limited to float[4] vertex attribs, tightly packed.
39  */
40 void 
41 util_draw_vertex_buffer(struct pipe_context *pipe,
42                         struct pipe_resource *vbuf,
43                         uint offset,
44                         uint prim_type,
45                         uint num_verts,
46                         uint num_attribs)
47 {
48    struct pipe_vertex_buffer vbuffer;
49
50    assert(num_attribs <= PIPE_MAX_ATTRIBS);
51
52    /* tell pipe about the vertex buffer */
53    memset(&vbuffer, 0, sizeof(vbuffer));
54    vbuffer.buffer = vbuf;
55    vbuffer.stride = num_attribs * 4 * sizeof(float);  /* vertex size */
56    vbuffer.buffer_offset = offset;
57    vbuffer.max_index = num_verts - 1;
58    pipe->set_vertex_buffers(pipe, 1, &vbuffer);
59
60    /* note: vertex elements already set by caller */
61
62    /* draw */
63    pipe->draw_arrays(pipe, prim_type, 0, num_verts);
64 }
65
66
67
68 /**
69  * Draw screen-aligned textured quad.
70  * Note: this isn't especially efficient.
71  */
72 void 
73 util_draw_texquad(struct pipe_context *pipe,
74                   float x0, float y0, float x1, float y1, float z)
75 {
76    uint numAttribs = 2, i, j;
77    uint vertexBytes = 4 * (4 * numAttribs * sizeof(float));
78    struct pipe_resource *vbuf = NULL;  
79    uint *v = NULL;
80
81    v = MALLOC(vertexBytes);
82    if (v == NULL)
83       goto out;
84
85    /*
86     * Load vertex buffer
87     */
88    for (i = j = 0; i < 4; i++) {
89       v[j + 2] = z;   /* z */
90       v[j + 3] = 1.0; /* w */
91       v[j + 6] = 0.0; /* r */
92       v[j + 7] = 1.0; /* q */
93       j += 8;
94    }
95
96    v[0] = x0;
97    v[1] = y0;
98    v[4] = 0.0; /*s*/
99    v[5] = 0.0; /*t*/
100
101    v[8] = x1;
102    v[9] = y0;
103    v[12] = 1.0;
104    v[13] = 0.0;
105
106    v[16] = x1;
107    v[17] = y1;
108    v[20] = 1.0;
109    v[21] = 1.0;
110
111    v[24] = x0;
112    v[25] = y1;
113    v[28] = 0.0;
114    v[29] = 1.0;
115          
116    vbuf = pipe_user_buffer_create(pipe->screen, v, vertexBytes,
117                                   PIPE_BIND_VERTEX_BUFFER);
118    if (!vbuf) 
119       goto out;
120
121    util_draw_vertex_buffer(pipe, vbuf, 0, PIPE_PRIM_TRIANGLE_FAN, 4, 2);
122
123 out:
124    if (vbuf)
125       pipe_resource_reference(&vbuf, NULL);
126    
127    if (v)
128       FREE(v);
129 }