Rework of shader constant buffers.
[mesa.git] / src / mesa / pipe / softpipe / sp_draw_arrays.c
1 /**************************************************************************
2 *
3 * Copyright 2007 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 /* Author:
29 * Brian Paul
30 * Keith Whitwell
31 */
32
33
34 #include "pipe/p_defines.h"
35 #include "pipe/p_context.h"
36 #include "pipe/p_winsys.h"
37 #include "pipe/p_util.h"
38
39 #include "sp_context.h"
40 #include "sp_state.h"
41
42 #include "pipe/draw/draw_context.h"
43
44
45
46 static void
47 softpipe_map_constant_buffers(struct softpipe_context *sp)
48 {
49 struct pipe_winsys *ws = sp->pipe.winsys;
50 uint i;
51 for (i = 0; i < 2; i++) {
52 if (sp->constants[i].size)
53 sp->mapped_constants[i] = ws->buffer_map(ws, sp->constants[i].buffer,
54 PIPE_BUFFER_FLAG_READ);
55 }
56
57 draw_set_mapped_constant_buffer(sp->draw,
58 sp->mapped_constants[PIPE_SHADER_VERTEX]);
59 }
60
61 static void
62 softpipe_unmap_constant_buffers(struct softpipe_context *sp)
63 {
64 struct pipe_winsys *ws = sp->pipe.winsys;
65 uint i;
66 for (i = 0; i < 2; i++) {
67 if (sp->constants[i].size)
68 ws->buffer_unmap(ws, sp->constants[i].buffer);
69 sp->mapped_constants[i] = NULL;
70 }
71 }
72
73
74 boolean
75 softpipe_draw_arrays(struct pipe_context *pipe, unsigned mode,
76 unsigned start, unsigned count)
77 {
78 return softpipe_draw_elements(pipe, NULL, 0, mode, start, count);
79 }
80
81
82
83 /**
84 * Draw vertex arrays, with optional indexing.
85 * Basically, map the vertex buffers (and drawing surfaces), then hand off
86 * the drawing to the 'draw' module.
87 *
88 * XXX should the element buffer be specified/bound with a separate function?
89 */
90 boolean
91 softpipe_draw_elements(struct pipe_context *pipe,
92 struct pipe_buffer_handle *indexBuffer,
93 unsigned indexSize,
94 unsigned mode, unsigned start, unsigned count)
95 {
96 struct softpipe_context *sp = softpipe_context(pipe);
97 struct draw_context *draw = sp->draw;
98 unsigned length, first, incr, i;
99
100 /* first, check that the primitive is not malformed */
101 draw_prim_info( mode, &first, &incr );
102 length = draw_trim( count, first, incr );
103 if (!length)
104 return TRUE;
105
106
107 if (sp->dirty)
108 softpipe_update_derived( sp );
109
110 softpipe_map_surfaces(sp);
111
112 softpipe_map_constant_buffers(sp);
113
114 /*
115 * Map vertex buffers
116 */
117 for (i = 0; i < PIPE_ATTRIB_MAX; i++) {
118 if (sp->vertex_buffer[i].buffer) {
119 void *buf
120 = pipe->winsys->buffer_map(pipe->winsys,
121 sp->vertex_buffer[i].buffer,
122 PIPE_BUFFER_FLAG_READ);
123 draw_set_mapped_vertex_buffer(draw, i, buf);
124 }
125 }
126 /* Map index buffer, if present */
127 if (indexBuffer) {
128 void *mapped_indexes
129 = pipe->winsys->buffer_map(pipe->winsys, indexBuffer,
130 PIPE_BUFFER_FLAG_READ);
131 draw_set_mapped_element_buffer(draw, indexSize, mapped_indexes);
132 }
133 else {
134 /* no index/element buffer */
135 draw_set_mapped_element_buffer(draw, 0, NULL);
136 }
137
138 /* draw! */
139 draw_arrays(draw, mode, start, count);
140
141 /*
142 * unmap vertex/index buffers
143 */
144 for (i = 0; i < PIPE_ATTRIB_MAX; i++) {
145 if (sp->vertex_buffer[i].buffer) {
146 pipe->winsys->buffer_unmap(pipe->winsys, sp->vertex_buffer[i].buffer);
147 draw_set_mapped_vertex_buffer(draw, i, NULL);
148 }
149 }
150 if (indexBuffer) {
151 pipe->winsys->buffer_unmap(pipe->winsys, indexBuffer);
152 draw_set_mapped_element_buffer(draw, 0, NULL);
153 }
154
155 softpipe_unmap_surfaces(sp);
156 softpipe_unmap_constant_buffers(sp);
157
158 return TRUE;
159 }