611d2c6102f9158aa06b339f3d06fee09023383d
[mesa.git] / src / gallium / drivers / svga / svga_state_sampler.c
1 /*
2 * Copyright 2013 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 /**
27 * VGPU10 sampler and sampler view functions.
28 */
29
30
31 #include "pipe/p_defines.h"
32 #include "util/u_bitmask.h"
33 #include "util/u_inlines.h"
34 #include "util/u_math.h"
35 #include "util/u_memory.h"
36
37 #include "svga_cmd.h"
38 #include "svga_context.h"
39 #include "svga_format.h"
40 #include "svga_resource_buffer.h"
41 #include "svga_resource_texture.h"
42 #include "svga_shader.h"
43 #include "svga_state.h"
44 #include "svga_sampler_view.h"
45
46
47 /** Get resource handle for a texture or buffer */
48 static inline struct svga_winsys_surface *
49 svga_resource_handle(struct pipe_resource *res)
50 {
51 if (res->target == PIPE_BUFFER) {
52 return svga_buffer(res)->handle;
53 }
54 else {
55 return svga_texture(res)->handle;
56 }
57 }
58
59
60 /**
61 * This helper function returns TRUE if the specified resource collides with
62 * any of the resources bound to any of the currently bound sampler views.
63 */
64 boolean
65 svga_check_sampler_view_resource_collision(struct svga_context *svga,
66 struct svga_winsys_surface *res,
67 unsigned shader)
68 {
69 struct pipe_screen *screen = svga->pipe.screen;
70 unsigned i;
71
72 if (svga_screen(screen)->debug.no_surface_view) {
73 return FALSE;
74 }
75
76 for (i = 0; i < svga->curr.num_sampler_views[shader]; i++) {
77 struct svga_pipe_sampler_view *sv =
78 svga_pipe_sampler_view(svga->curr.sampler_views[shader][i]);
79
80 if (sv && res == svga_resource_handle(sv->base.texture)) {
81 return TRUE;
82 }
83 }
84
85 return FALSE;
86 }
87
88
89 /**
90 * Create a DX ShaderResourceSamplerView for the given pipe_sampler_view,
91 * if needed.
92 */
93 static enum pipe_error
94 svga_validate_pipe_sampler_view(struct svga_context *svga,
95 struct svga_pipe_sampler_view *sv)
96 {
97 enum pipe_error ret = PIPE_OK;
98
99 if (sv->id == SVGA3D_INVALID_ID) {
100 struct svga_screen *ss = svga_screen(svga->pipe.screen);
101 struct pipe_resource *texture = sv->base.texture;
102 struct svga_winsys_surface *surface = svga_resource_handle(texture);
103 SVGA3dSurfaceFormat format;
104 SVGA3dResourceType resourceDim;
105 SVGA3dShaderResourceViewDesc viewDesc;
106
107 format = svga_translate_format(ss, sv->base.format,
108 PIPE_BIND_SAMPLER_VIEW);
109 assert(format != SVGA3D_FORMAT_INVALID);
110
111 if (texture->target == PIPE_BUFFER) {
112 viewDesc.buffer.firstElement = sv->base.u.buf.first_element;
113 viewDesc.buffer.numElements = (sv->base.u.buf.last_element -
114 sv->base.u.buf.first_element + 1);
115 }
116 else {
117 viewDesc.tex.mostDetailedMip = sv->base.u.tex.first_level;
118 viewDesc.tex.firstArraySlice = sv->base.u.tex.first_layer;
119 viewDesc.tex.mipLevels = (sv->base.u.tex.last_level -
120 sv->base.u.tex.first_level + 1);
121 }
122
123 /* arraySize in viewDesc specifies the number of array slices in a
124 * texture array. For 3D texture, last_layer in
125 * pipe_sampler_view specifies the last slice of the texture
126 * which is different from the last slice in a texture array,
127 * hence we need to set arraySize to 1 explicitly.
128 */
129 viewDesc.tex.arraySize =
130 (texture->target == PIPE_TEXTURE_3D ||
131 texture->target == PIPE_BUFFER) ? 1 :
132 (sv->base.u.tex.last_layer - sv->base.u.tex.first_layer + 1);
133
134 switch (texture->target) {
135 case PIPE_BUFFER:
136 resourceDim = SVGA3D_RESOURCE_BUFFER;
137 break;
138 case PIPE_TEXTURE_1D:
139 case PIPE_TEXTURE_1D_ARRAY:
140 resourceDim = SVGA3D_RESOURCE_TEXTURE1D;
141 break;
142 case PIPE_TEXTURE_RECT:
143 case PIPE_TEXTURE_2D:
144 case PIPE_TEXTURE_2D_ARRAY:
145 resourceDim = SVGA3D_RESOURCE_TEXTURE2D;
146 break;
147 case PIPE_TEXTURE_3D:
148 resourceDim = SVGA3D_RESOURCE_TEXTURE3D;
149 break;
150 case PIPE_TEXTURE_CUBE:
151 case PIPE_TEXTURE_CUBE_ARRAY:
152 resourceDim = SVGA3D_RESOURCE_TEXTURECUBE;
153 break;
154
155 default:
156 assert(!"Unexpected texture type");
157 resourceDim = SVGA3D_RESOURCE_TEXTURE2D;
158 }
159
160 sv->id = util_bitmask_add(svga->sampler_view_id_bm);
161
162 ret = SVGA3D_vgpu10_DefineShaderResourceView(svga->swc,
163 sv->id,
164 surface,
165 format,
166 resourceDim,
167 &viewDesc);
168 if (ret != PIPE_OK) {
169 util_bitmask_clear(svga->sampler_view_id_bm, sv->id);
170 sv->id = SVGA3D_INVALID_ID;
171 }
172 }
173
174 return ret;
175 }
176
177
178 static enum pipe_error
179 update_sampler_resources(struct svga_context *svga, unsigned dirty)
180 {
181 enum pipe_error ret = PIPE_OK;
182 unsigned shader;
183
184 if (!svga_have_vgpu10(svga))
185 return PIPE_OK;
186
187 for (shader = PIPE_SHADER_VERTEX; shader <= PIPE_SHADER_GEOMETRY; shader++) {
188 SVGA3dShaderResourceViewId ids[PIPE_MAX_SAMPLERS];
189 struct svga_winsys_surface *surfaces[PIPE_MAX_SAMPLERS];
190 unsigned count;
191 unsigned nviews;
192 unsigned i;
193
194 count = svga->curr.num_sampler_views[shader];
195 for (i = 0; i < count; i++) {
196 struct svga_pipe_sampler_view *sv =
197 svga_pipe_sampler_view(svga->curr.sampler_views[shader][i]);
198 struct svga_winsys_surface *surface;
199
200 if (sv) {
201 surface = svga_resource_handle(sv->base.texture);
202
203 ret = svga_validate_pipe_sampler_view(svga, sv);
204 if (ret != PIPE_OK)
205 return ret;
206
207 assert(sv->id != SVGA3D_INVALID_ID);
208 ids[i] = sv->id;
209 }
210 else {
211 surface = NULL;
212 ids[i] = SVGA3D_INVALID_ID;
213 }
214 surfaces[i] = surface;
215 }
216
217 for (; i < Elements(ids); i++) {
218 ids[i] = SVGA3D_INVALID_ID;
219 surfaces[i] = NULL;
220 }
221
222 if (shader == PIPE_SHADER_FRAGMENT) {
223 /* Handle polygon stipple sampler view */
224 if (svga->curr.rast->templ.poly_stipple_enable) {
225 const unsigned unit = svga->state.hw_draw.fs->pstipple_sampler_unit;
226 struct svga_pipe_sampler_view *sv =
227 svga->polygon_stipple.sampler_view;
228
229 assert(sv);
230 if (!sv) {
231 return PIPE_OK; /* probably out of memory */
232 }
233
234 ret = svga_validate_pipe_sampler_view(svga, sv);
235 if (ret != PIPE_OK)
236 return ret;
237
238 ids[unit] = sv->id;
239 surfaces[unit] = svga_resource_handle(sv->base.texture);
240 count = MAX2(count, unit+1);
241 }
242 }
243
244 /* Number of ShaderResources that need to be modified. This includes
245 * the one that need to be unbound.
246 */
247 nviews = MAX2(svga->state.hw_draw.num_sampler_views[shader], count);
248 if (nviews > 0) {
249 ret = SVGA3D_vgpu10_SetShaderResources(svga->swc,
250 svga_shader_type(shader),
251 0, /* startView */
252 nviews,
253 ids,
254 surfaces);
255 if (ret != PIPE_OK)
256 return ret;
257 }
258
259 /* Number of sampler views enabled in the device */
260 svga->state.hw_draw.num_sampler_views[shader] = count;
261 }
262
263 return ret;
264 }
265
266
267 struct svga_tracked_state svga_hw_sampler_bindings = {
268 "shader resources emit",
269 SVGA_NEW_STIPPLE |
270 SVGA_NEW_TEXTURE_BINDING,
271 update_sampler_resources
272 };
273
274
275
276 static enum pipe_error
277 update_samplers(struct svga_context *svga, unsigned dirty )
278 {
279 enum pipe_error ret = PIPE_OK;
280 unsigned shader;
281
282 if (!svga_have_vgpu10(svga))
283 return PIPE_OK;
284
285 for (shader = PIPE_SHADER_VERTEX; shader <= PIPE_SHADER_GEOMETRY; shader++) {
286 const unsigned count = svga->curr.num_samplers[shader];
287 SVGA3dSamplerId ids[PIPE_MAX_SAMPLERS];
288 unsigned i;
289
290 for (i = 0; i < count; i++) {
291 if (svga->curr.sampler[shader][i]) {
292 ids[i] = svga->curr.sampler[shader][i]->id;
293 assert(ids[i] != SVGA3D_INVALID_ID);
294 }
295 else {
296 ids[i] = SVGA3D_INVALID_ID;
297 }
298 }
299
300 if (count > 0) {
301 ret = SVGA3D_vgpu10_SetSamplers(svga->swc,
302 count,
303 0, /* start */
304 svga_shader_type(shader), /* type */
305 ids);
306 if (ret != PIPE_OK)
307 return ret;
308 }
309 }
310
311 /* Handle polygon stipple sampler texture */
312 if (svga->curr.rast->templ.poly_stipple_enable) {
313 const unsigned unit = svga->state.hw_draw.fs->pstipple_sampler_unit;
314 struct svga_sampler_state *sampler = svga->polygon_stipple.sampler;
315
316 assert(sampler);
317 if (!sampler) {
318 return PIPE_OK; /* probably out of memory */
319 }
320
321 ret = SVGA3D_vgpu10_SetSamplers(svga->swc,
322 1, /* count */
323 unit, /* start */
324 SVGA3D_SHADERTYPE_PS,
325 &sampler->id);
326 }
327
328 return ret;
329 }
330
331
332 struct svga_tracked_state svga_hw_sampler = {
333 "texture sampler emit",
334 (SVGA_NEW_SAMPLER |
335 SVGA_NEW_STIPPLE |
336 SVGA_NEW_TEXTURE_FLAGS),
337 update_samplers
338 };