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 #include "cso_cache/cso_context.h"
35 
36 
37 /**
38  * Draw a simple vertex buffer / primitive.
39  * Limited to float[4] vertex attribs, tightly packed.
40  */
41 void
util_draw_vertex_buffer(struct pipe_context * pipe,struct cso_context * cso,struct pipe_resource * vbuf,uint offset,uint prim_type,uint num_verts,uint num_attribs)42 util_draw_vertex_buffer(struct pipe_context *pipe,
43                         struct cso_context *cso,
44                         struct pipe_resource *vbuf,
45                         uint offset,
46                         uint prim_type,
47                         uint num_verts,
48                         uint num_attribs)
49 {
50    struct pipe_vertex_buffer vbuffer;
51 
52    assert(num_attribs <= PIPE_MAX_ATTRIBS);
53 
54    /* tell pipe about the vertex buffer */
55    memset(&vbuffer, 0, sizeof(vbuffer));
56    vbuffer.buffer = vbuf;
57    vbuffer.stride = num_attribs * 4 * sizeof(float);  /* vertex size */
58    vbuffer.buffer_offset = offset;
59 
60    /* note: vertex elements already set by caller */
61 
62    if (cso) {
63       cso_set_vertex_buffers(cso, 1, &vbuffer);
64       cso_draw_arrays(cso, prim_type, 0, num_verts);
65    } else {
66       pipe->set_vertex_buffers(pipe, 1, &vbuffer);
67       util_draw_arrays(pipe, prim_type, 0, num_verts);
68    }
69 }
70 
71 
72 /**
73  * Draw a simple vertex buffer / primitive.
74  * Limited to float[4] vertex attribs, tightly packed.
75  */
76 void
util_draw_user_vertex_buffer(struct cso_context * cso,void * buffer,uint prim_type,uint num_verts,uint num_attribs)77 util_draw_user_vertex_buffer(struct cso_context *cso, void *buffer,
78                              uint prim_type, uint num_verts, uint num_attribs)
79 {
80    struct pipe_vertex_buffer vbuffer = {0};
81 
82    assert(num_attribs <= PIPE_MAX_ATTRIBS);
83 
84    vbuffer.user_buffer = buffer;
85    vbuffer.stride = num_attribs * 4 * sizeof(float);  /* vertex size */
86 
87    /* note: vertex elements already set by caller */
88 
89    cso_set_vertex_buffers(cso, 1, &vbuffer);
90    cso_draw_arrays(cso, prim_type, 0, num_verts);
91 }
92 
93 
94 /**
95  * Draw screen-aligned textured quad.
96  * Note: this isn't especially efficient.
97  */
98 void
util_draw_texquad(struct pipe_context * pipe,struct cso_context * cso,float x0,float y0,float x1,float y1,float z)99 util_draw_texquad(struct pipe_context *pipe, struct cso_context *cso,
100                   float x0, float y0, float x1, float y1, float z)
101 {
102    uint numAttribs = 2, i, j;
103    uint vertexBytes = 4 * (4 * numAttribs * sizeof(float));
104    struct pipe_resource *vbuf = NULL;
105    float *v = NULL;
106 
107    v = MALLOC(vertexBytes);
108    if (v == NULL)
109       goto out;
110 
111    /*
112     * Load vertex buffer
113     */
114    for (i = j = 0; i < 4; i++) {
115       v[j + 2] = z;   /* z */
116       v[j + 3] = 1.0; /* w */
117       v[j + 6] = 0.0; /* r */
118       v[j + 7] = 1.0; /* q */
119       j += 8;
120    }
121 
122    v[0] = x0;
123    v[1] = y0;
124    v[4] = 0.0; /*s*/
125    v[5] = 0.0; /*t*/
126 
127    v[8] = x1;
128    v[9] = y0;
129    v[12] = 1.0;
130    v[13] = 0.0;
131 
132    v[16] = x1;
133    v[17] = y1;
134    v[20] = 1.0;
135    v[21] = 1.0;
136 
137    v[24] = x0;
138    v[25] = y1;
139    v[28] = 0.0;
140    v[29] = 1.0;
141 
142    vbuf = pipe_buffer_create(pipe->screen, PIPE_BIND_VERTEX_BUFFER,
143                              PIPE_USAGE_STAGING, vertexBytes);
144    if (!vbuf)
145       goto out;
146    pipe_buffer_write(pipe, vbuf, 0, vertexBytes, v);
147 
148    util_draw_vertex_buffer(pipe, cso, vbuf, 0, PIPE_PRIM_TRIANGLE_FAN, 4, 2);
149 
150 out:
151    if (vbuf)
152       pipe_resource_reference(&vbuf, NULL);
153 
154    if (v)
155       FREE(v);
156 }
157