1e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian/**************************************************************************
2e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian *
3e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian * All Rights Reserved.
5e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian *
6e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian * Permission is hereby granted, free of charge, to any person obtaining a
7e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian * copy of this software and associated documentation files (the
8e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian * "Software"), to deal in the Software without restriction, including
9e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian * without limitation the rights to use, copy, modify, merge, publish,
10e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian * distribute, sub license, and/or sell copies of the Software, and to
11e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian * permit persons to whom the Software is furnished to do so, subject to
12e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian * the following conditions:
13e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian *
14e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian * The above copyright notice and this permission notice (including the
15e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian * next paragraph) shall be included in all copies or substantial portions
16e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian * of the Software.
17e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian *
18e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian *
26e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian **************************************************************************/
27e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian
28e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian
29e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian#include "pipe/p_context.h"
30e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian#include "pipe/p_defines.h"
3128486880ca3ec39419ccee0cb1a3bedc9ef7117cJosé Fonseca#include "util/u_inlines.h"
32e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian#include "util/u_draw_quad.h"
33287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell#include "util/u_memory.h"
34d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák#include "cso_cache/cso_context.h"
35e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian
36e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian
37e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian/**
388c71406c74b80fa2d2b1a488938c3b9dfc156343Brian * Draw a simple vertex buffer / primitive.
398c71406c74b80fa2d2b1a488938c3b9dfc156343Brian * Limited to float[4] vertex attribs, tightly packed.
408c71406c74b80fa2d2b1a488938c3b9dfc156343Brian */
418c71406c74b80fa2d2b1a488938c3b9dfc156343Brianvoid
428c71406c74b80fa2d2b1a488938c3b9dfc156343Brianutil_draw_vertex_buffer(struct pipe_context *pipe,
43d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák                        struct cso_context *cso,
44287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell                        struct pipe_resource *vbuf,
45d2c2e9316d043ab584794a3524f22776deb4c777Keith Whitwell                        uint offset,
468c71406c74b80fa2d2b1a488938c3b9dfc156343Brian                        uint prim_type,
478c71406c74b80fa2d2b1a488938c3b9dfc156343Brian                        uint num_verts,
488c71406c74b80fa2d2b1a488938c3b9dfc156343Brian                        uint num_attribs)
498c71406c74b80fa2d2b1a488938c3b9dfc156343Brian{
508c71406c74b80fa2d2b1a488938c3b9dfc156343Brian   struct pipe_vertex_buffer vbuffer;
518c71406c74b80fa2d2b1a488938c3b9dfc156343Brian
52a52c0416d1f2105960b4646e2e268aed26814689Brian Paul   assert(num_attribs <= PIPE_MAX_ATTRIBS);
53a52c0416d1f2105960b4646e2e268aed26814689Brian Paul
548c71406c74b80fa2d2b1a488938c3b9dfc156343Brian   /* tell pipe about the vertex buffer */
55b747e1dec4a2999412ee25371994a07a4bed900eKeith Whitwell   memset(&vbuffer, 0, sizeof(vbuffer));
568c71406c74b80fa2d2b1a488938c3b9dfc156343Brian   vbuffer.buffer = vbuf;
57872b515e8f0bb1be5bad85fd9d01529c71f07ba2Zack Rusin   vbuffer.stride = num_attribs * 4 * sizeof(float);  /* vertex size */
58d2c2e9316d043ab584794a3524f22776deb4c777Keith Whitwell   vbuffer.buffer_offset = offset;
59d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák
600f00786868720d8b56bdf31cb28ece7590b16809Marek Olšák   /* note: vertex elements already set by caller */
610f00786868720d8b56bdf31cb28ece7590b16809Marek Olšák
62d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák   if (cso) {
63d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák      cso_set_vertex_buffers(cso, 1, &vbuffer);
640f00786868720d8b56bdf31cb28ece7590b16809Marek Olšák      cso_draw_arrays(cso, prim_type, 0, num_verts);
65d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák   } else {
66d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák      pipe->set_vertex_buffers(pipe, 1, &vbuffer);
670f00786868720d8b56bdf31cb28ece7590b16809Marek Olšák      util_draw_arrays(pipe, prim_type, 0, num_verts);
68d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák   }
698c71406c74b80fa2d2b1a488938c3b9dfc156343Brian}
708c71406c74b80fa2d2b1a488938c3b9dfc156343Brian
718c71406c74b80fa2d2b1a488938c3b9dfc156343Brian
72fa20733a622770eaaa941f64d570d7b63d8f37b6Marek Olšák/**
73fa20733a622770eaaa941f64d570d7b63d8f37b6Marek Olšák * Draw a simple vertex buffer / primitive.
74fa20733a622770eaaa941f64d570d7b63d8f37b6Marek Olšák * Limited to float[4] vertex attribs, tightly packed.
75fa20733a622770eaaa941f64d570d7b63d8f37b6Marek Olšák */
76fa20733a622770eaaa941f64d570d7b63d8f37b6Marek Olšákvoid
77fa20733a622770eaaa941f64d570d7b63d8f37b6Marek Olšákutil_draw_user_vertex_buffer(struct cso_context *cso, void *buffer,
78fa20733a622770eaaa941f64d570d7b63d8f37b6Marek Olšák                             uint prim_type, uint num_verts, uint num_attribs)
79fa20733a622770eaaa941f64d570d7b63d8f37b6Marek Olšák{
80fa20733a622770eaaa941f64d570d7b63d8f37b6Marek Olšák   struct pipe_vertex_buffer vbuffer = {0};
81fa20733a622770eaaa941f64d570d7b63d8f37b6Marek Olšák
82fa20733a622770eaaa941f64d570d7b63d8f37b6Marek Olšák   assert(num_attribs <= PIPE_MAX_ATTRIBS);
83fa20733a622770eaaa941f64d570d7b63d8f37b6Marek Olšák
84fa20733a622770eaaa941f64d570d7b63d8f37b6Marek Olšák   vbuffer.user_buffer = buffer;
85fa20733a622770eaaa941f64d570d7b63d8f37b6Marek Olšák   vbuffer.stride = num_attribs * 4 * sizeof(float);  /* vertex size */
86fa20733a622770eaaa941f64d570d7b63d8f37b6Marek Olšák
87fa20733a622770eaaa941f64d570d7b63d8f37b6Marek Olšák   /* note: vertex elements already set by caller */
88fa20733a622770eaaa941f64d570d7b63d8f37b6Marek Olšák
89fa20733a622770eaaa941f64d570d7b63d8f37b6Marek Olšák   cso_set_vertex_buffers(cso, 1, &vbuffer);
90fa20733a622770eaaa941f64d570d7b63d8f37b6Marek Olšák   cso_draw_arrays(cso, prim_type, 0, num_verts);
91fa20733a622770eaaa941f64d570d7b63d8f37b6Marek Olšák}
92fa20733a622770eaaa941f64d570d7b63d8f37b6Marek Olšák
938c71406c74b80fa2d2b1a488938c3b9dfc156343Brian
948c71406c74b80fa2d2b1a488938c3b9dfc156343Brian/**
95e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian * Draw screen-aligned textured quad.
96287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell * Note: this isn't especially efficient.
97e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian */
98e3a747f1fefc147e8dca5729bcc8d68f419c595aBrianvoid
99d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšákutil_draw_texquad(struct pipe_context *pipe, struct cso_context *cso,
100e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian                  float x0, float y0, float x1, float y1, float z)
101e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian{
102287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   uint numAttribs = 2, i, j;
103287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   uint vertexBytes = 4 * (4 * numAttribs * sizeof(float));
104287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   struct pipe_resource *vbuf = NULL;
105eb2e8167fa543d12bac91e451838934ebbf0d594Alex Corscadden   float *v = NULL;
106287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
107287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   v = MALLOC(vertexBytes);
108287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   if (v == NULL)
109287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      goto out;
110287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
111287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   /*
112287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell    * Load vertex buffer
113287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell    */
114287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   for (i = j = 0; i < 4; i++) {
115287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      v[j + 2] = z;   /* z */
116287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      v[j + 3] = 1.0; /* w */
117287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      v[j + 6] = 0.0; /* r */
118287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      v[j + 7] = 1.0; /* q */
119287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      j += 8;
120e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian   }
121287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
122287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   v[0] = x0;
123287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   v[1] = y0;
124287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   v[4] = 0.0; /*s*/
125287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   v[5] = 0.0; /*t*/
126287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
127287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   v[8] = x1;
128287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   v[9] = y0;
129287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   v[12] = 1.0;
130287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   v[13] = 0.0;
131287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
132287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   v[16] = x1;
133287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   v[17] = y1;
134287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   v[20] = 1.0;
135287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   v[21] = 1.0;
136287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
137287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   v[24] = x0;
138287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   v[25] = y1;
139287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   v[28] = 0.0;
140287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   v[29] = 1.0;
141287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
1423b5f4b173b2703b7f72dcf33ad4cb00347712733Marek Olšák   vbuf = pipe_buffer_create(pipe->screen, PIPE_BIND_VERTEX_BUFFER,
1433b5f4b173b2703b7f72dcf33ad4cb00347712733Marek Olšák                             PIPE_USAGE_STAGING, vertexBytes);
144eb2e8167fa543d12bac91e451838934ebbf0d594Alex Corscadden   if (!vbuf)
145287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      goto out;
1463b5f4b173b2703b7f72dcf33ad4cb00347712733Marek Olšák   pipe_buffer_write(pipe, vbuf, 0, vertexBytes, v);
147287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
148d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák   util_draw_vertex_buffer(pipe, cso, vbuf, 0, PIPE_PRIM_TRIANGLE_FAN, 4, 2);
149287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
150287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwellout:
151287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   if (vbuf)
152287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      pipe_resource_reference(&vbuf, NULL);
153287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
154287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell   if (v)
155287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell      FREE(v);
156e3a747f1fefc147e8dca5729bcc8d68f419c595aBrian}
157