gallium/drivers: Sanitize NULL checks into canonical form
[mesa.git] / src / gallium / drivers / svga / svga_draw_elements.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "util/u_inlines.h"
27 #include "util/u_prim.h"
28 #include "util/u_upload_mgr.h"
29 #include "indices/u_indices.h"
30
31 #include "svga_cmd.h"
32 #include "svga_draw.h"
33 #include "svga_draw_private.h"
34 #include "svga_resource_buffer.h"
35 #include "svga_winsys.h"
36 #include "svga_context.h"
37 #include "svga_hw_reg.h"
38
39
40 static enum pipe_error
41 translate_indices(struct svga_hwtnl *hwtnl, struct pipe_resource *src,
42 unsigned offset, unsigned prim, unsigned nr,
43 unsigned index_size,
44 u_translate_func translate, struct pipe_resource **out_buf)
45 {
46 struct pipe_context *pipe = &hwtnl->svga->pipe;
47 struct pipe_transfer *src_transfer = NULL;
48 struct pipe_transfer *dst_transfer = NULL;
49 unsigned size = index_size * nr;
50 const void *src_map = NULL;
51 struct pipe_resource *dst = NULL;
52 void *dst_map = NULL;
53
54 /* Need to trim vertex count to make sure we don't write too much data
55 * to the dst buffer in the translate() call.
56 */
57 u_trim_pipe_prim(prim, &nr);
58
59 size = index_size * nr;
60
61 dst = pipe_buffer_create(pipe->screen,
62 PIPE_BIND_INDEX_BUFFER, PIPE_USAGE_DEFAULT, size);
63 if (!dst)
64 goto fail;
65
66 src_map = pipe_buffer_map(pipe, src, PIPE_TRANSFER_READ, &src_transfer);
67 if (!src_map)
68 goto fail;
69
70 dst_map = pipe_buffer_map(pipe, dst, PIPE_TRANSFER_WRITE, &dst_transfer);
71 if (!dst_map)
72 goto fail;
73
74 translate((const char *) src_map + offset, 0, 0, nr, 0, dst_map);
75
76 pipe_buffer_unmap(pipe, src_transfer);
77 pipe_buffer_unmap(pipe, dst_transfer);
78
79 *out_buf = dst;
80 return PIPE_OK;
81
82 fail:
83 if (src_map)
84 pipe_buffer_unmap(pipe, src_transfer);
85
86 if (dst_map)
87 pipe_buffer_unmap(pipe, dst_transfer);
88
89 if (dst)
90 pipe->screen->resource_destroy(pipe->screen, dst);
91
92 return PIPE_ERROR_OUT_OF_MEMORY;
93 }
94
95
96 enum pipe_error
97 svga_hwtnl_simple_draw_range_elements(struct svga_hwtnl *hwtnl,
98 struct pipe_resource *index_buffer,
99 unsigned index_size, int index_bias,
100 unsigned min_index, unsigned max_index,
101 unsigned prim, unsigned start,
102 unsigned count,
103 unsigned start_instance,
104 unsigned instance_count)
105 {
106 SVGA3dPrimitiveRange range;
107 unsigned hw_prim;
108 unsigned hw_count;
109 unsigned index_offset = start * index_size;
110
111 hw_prim = svga_translate_prim(prim, count, &hw_count);
112 if (hw_count == 0)
113 return PIPE_OK; /* nothing to draw */
114
115 range.primType = hw_prim;
116 range.primitiveCount = hw_count;
117 range.indexArray.offset = index_offset;
118 range.indexArray.stride = index_size;
119 range.indexWidth = index_size;
120 range.indexBias = index_bias;
121
122 return svga_hwtnl_prim(hwtnl, &range, count,
123 min_index, max_index, index_buffer,
124 start_instance, instance_count);
125 }
126
127
128 enum pipe_error
129 svga_hwtnl_draw_range_elements(struct svga_hwtnl *hwtnl,
130 struct pipe_resource *index_buffer,
131 unsigned index_size, int index_bias,
132 unsigned min_index, unsigned max_index,
133 unsigned prim, unsigned start, unsigned count,
134 unsigned start_instance, unsigned instance_count)
135 {
136 unsigned gen_prim, gen_size, gen_nr;
137 enum indices_mode gen_type;
138 u_translate_func gen_func;
139 enum pipe_error ret = PIPE_OK;
140
141 if (hwtnl->api_fillmode != PIPE_POLYGON_MODE_FILL &&
142 prim >= PIPE_PRIM_TRIANGLES) {
143 gen_type = u_unfilled_translator(prim,
144 index_size,
145 count,
146 hwtnl->api_fillmode,
147 &gen_prim,
148 &gen_size, &gen_nr, &gen_func);
149 }
150 else {
151 gen_type = u_index_translator(svga_hw_prims,
152 prim,
153 index_size,
154 count,
155 hwtnl->api_pv,
156 hwtnl->hw_pv,
157 PR_DISABLE,
158 &gen_prim, &gen_size, &gen_nr, &gen_func);
159 }
160
161 if (gen_type == U_TRANSLATE_MEMCPY) {
162 /* No need for translation, just pass through to hardware:
163 */
164 return svga_hwtnl_simple_draw_range_elements(hwtnl, index_buffer,
165 index_size,
166 index_bias,
167 min_index,
168 max_index,
169 gen_prim, start, count,
170 start_instance,
171 instance_count);
172 }
173 else {
174 struct pipe_resource *gen_buf = NULL;
175
176 /* Need to allocate a new index buffer and run the translate
177 * func to populate it. Could potentially cache this translated
178 * index buffer with the original to avoid future
179 * re-translations. Not much point if we're just accelerating
180 * GL though, as index buffers are typically used only once
181 * there.
182 */
183 ret = translate_indices(hwtnl,
184 index_buffer,
185 start * index_size,
186 gen_prim, gen_nr, gen_size, gen_func, &gen_buf);
187 if (ret != PIPE_OK)
188 goto done;
189
190 ret = svga_hwtnl_simple_draw_range_elements(hwtnl,
191 gen_buf,
192 gen_size,
193 index_bias,
194 min_index,
195 max_index,
196 gen_prim, 0, gen_nr,
197 start_instance,
198 instance_count);
199 if (ret != PIPE_OK)
200 goto done;
201
202 done:
203 if (gen_buf)
204 pipe_resource_reference(&gen_buf, NULL);
205
206 return ret;
207 }
208 }