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