Merge remote-tracking branch 'mesa-public/master' into vulkan
[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 /* Convert the format to a sampler-friendly format, if needed */
112 format = svga_sampler_format(format);
113
114 if (texture->target == PIPE_BUFFER) {
115 viewDesc.buffer.firstElement = sv->base.u.buf.first_element;
116 viewDesc.buffer.numElements = (sv->base.u.buf.last_element -
117 sv->base.u.buf.first_element + 1);
118 }
119 else {
120 viewDesc.tex.mostDetailedMip = sv->base.u.tex.first_level;
121 viewDesc.tex.firstArraySlice = sv->base.u.tex.first_layer;
122 viewDesc.tex.mipLevels = (sv->base.u.tex.last_level -
123 sv->base.u.tex.first_level + 1);
124 }
125
126 /* arraySize in viewDesc specifies the number of array slices in a
127 * texture array. For 3D texture, last_layer in
128 * pipe_sampler_view specifies the last slice of the texture
129 * which is different from the last slice in a texture array,
130 * hence we need to set arraySize to 1 explicitly.
131 */
132 viewDesc.tex.arraySize =
133 (texture->target == PIPE_TEXTURE_3D ||
134 texture->target == PIPE_BUFFER) ? 1 :
135 (sv->base.u.tex.last_layer - sv->base.u.tex.first_layer + 1);
136
137 switch (texture->target) {
138 case PIPE_BUFFER:
139 resourceDim = SVGA3D_RESOURCE_BUFFER;
140 break;
141 case PIPE_TEXTURE_1D:
142 case PIPE_TEXTURE_1D_ARRAY:
143 resourceDim = SVGA3D_RESOURCE_TEXTURE1D;
144 break;
145 case PIPE_TEXTURE_RECT:
146 case PIPE_TEXTURE_2D:
147 case PIPE_TEXTURE_2D_ARRAY:
148 resourceDim = SVGA3D_RESOURCE_TEXTURE2D;
149 break;
150 case PIPE_TEXTURE_3D:
151 resourceDim = SVGA3D_RESOURCE_TEXTURE3D;
152 break;
153 case PIPE_TEXTURE_CUBE:
154 case PIPE_TEXTURE_CUBE_ARRAY:
155 resourceDim = SVGA3D_RESOURCE_TEXTURECUBE;
156 break;
157
158 default:
159 assert(!"Unexpected texture type");
160 resourceDim = SVGA3D_RESOURCE_TEXTURE2D;
161 }
162
163 sv->id = util_bitmask_add(svga->sampler_view_id_bm);
164
165 ret = SVGA3D_vgpu10_DefineShaderResourceView(svga->swc,
166 sv->id,
167 surface,
168 format,
169 resourceDim,
170 &viewDesc);
171 if (ret != PIPE_OK) {
172 util_bitmask_clear(svga->sampler_view_id_bm, sv->id);
173 sv->id = SVGA3D_INVALID_ID;
174 }
175 }
176
177 return ret;
178 }
179
180
181 static enum pipe_error
182 update_sampler_resources(struct svga_context *svga, unsigned dirty)
183 {
184 enum pipe_error ret = PIPE_OK;
185 unsigned shader;
186
187 if (!svga_have_vgpu10(svga))
188 return PIPE_OK;
189
190 for (shader = PIPE_SHADER_VERTEX; shader <= PIPE_SHADER_GEOMETRY; shader++) {
191 SVGA3dShaderResourceViewId ids[PIPE_MAX_SAMPLERS];
192 struct svga_winsys_surface *surfaces[PIPE_MAX_SAMPLERS];
193 unsigned count;
194 unsigned nviews;
195 unsigned i;
196
197 count = svga->curr.num_sampler_views[shader];
198 for (i = 0; i < count; i++) {
199 struct svga_pipe_sampler_view *sv =
200 svga_pipe_sampler_view(svga->curr.sampler_views[shader][i]);
201 struct svga_winsys_surface *surface;
202
203 if (sv) {
204 surface = svga_resource_handle(sv->base.texture);
205
206 ret = svga_validate_pipe_sampler_view(svga, sv);
207 if (ret != PIPE_OK)
208 return ret;
209
210 assert(sv->id != SVGA3D_INVALID_ID);
211 ids[i] = sv->id;
212 }
213 else {
214 surface = NULL;
215 ids[i] = SVGA3D_INVALID_ID;
216 }
217 surfaces[i] = surface;
218 }
219
220 for (; i < Elements(ids); i++) {
221 ids[i] = SVGA3D_INVALID_ID;
222 surfaces[i] = NULL;
223 }
224
225 if (shader == PIPE_SHADER_FRAGMENT) {
226 /* Handle polygon stipple sampler view */
227 if (svga->curr.rast->templ.poly_stipple_enable) {
228 const unsigned unit = svga->state.hw_draw.fs->pstipple_sampler_unit;
229 struct svga_pipe_sampler_view *sv =
230 svga->polygon_stipple.sampler_view;
231
232 assert(sv);
233 if (!sv) {
234 return PIPE_OK; /* probably out of memory */
235 }
236
237 ret = svga_validate_pipe_sampler_view(svga, sv);
238 if (ret != PIPE_OK)
239 return ret;
240
241 ids[unit] = sv->id;
242 surfaces[unit] = svga_resource_handle(sv->base.texture);
243 count = MAX2(count, unit+1);
244 }
245 }
246
247 /* Number of ShaderResources that need to be modified. This includes
248 * the one that need to be unbound.
249 */
250 nviews = MAX2(svga->state.hw_draw.num_sampler_views[shader], count);
251 if (nviews > 0) {
252 ret = SVGA3D_vgpu10_SetShaderResources(svga->swc,
253 svga_shader_type(shader),
254 0, /* startView */
255 nviews,
256 ids,
257 surfaces);
258 if (ret != PIPE_OK)
259 return ret;
260 }
261
262 /* Number of sampler views enabled in the device */
263 svga->state.hw_draw.num_sampler_views[shader] = count;
264 }
265
266 return ret;
267 }
268
269
270 struct svga_tracked_state svga_hw_sampler_bindings = {
271 "shader resources emit",
272 SVGA_NEW_STIPPLE |
273 SVGA_NEW_TEXTURE_BINDING,
274 update_sampler_resources
275 };
276
277
278
279 static enum pipe_error
280 update_samplers(struct svga_context *svga, unsigned dirty )
281 {
282 enum pipe_error ret = PIPE_OK;
283 unsigned shader;
284
285 if (!svga_have_vgpu10(svga))
286 return PIPE_OK;
287
288 for (shader = PIPE_SHADER_VERTEX; shader <= PIPE_SHADER_GEOMETRY; shader++) {
289 const unsigned count = svga->curr.num_samplers[shader];
290 SVGA3dSamplerId ids[PIPE_MAX_SAMPLERS];
291 unsigned i;
292
293 for (i = 0; i < count; i++) {
294 if (svga->curr.sampler[shader][i]) {
295 ids[i] = svga->curr.sampler[shader][i]->id;
296 assert(ids[i] != SVGA3D_INVALID_ID);
297 }
298 else {
299 ids[i] = SVGA3D_INVALID_ID;
300 }
301 }
302
303 if (count > 0) {
304 if (count != svga->state.hw_draw.num_samplers[shader] ||
305 memcmp(ids, svga->state.hw_draw.samplers[shader],
306 count * sizeof(ids[0])) != 0) {
307 /* HW state is really changing */
308 ret = SVGA3D_vgpu10_SetSamplers(svga->swc,
309 count,
310 0, /* start */
311 svga_shader_type(shader), /* type */
312 ids);
313 if (ret != PIPE_OK)
314 return ret;
315 memcpy(svga->state.hw_draw.samplers[shader], ids,
316 count * sizeof(ids[0]));
317 svga->state.hw_draw.num_samplers[shader] = count;
318 }
319 }
320 }
321
322 /* Handle polygon stipple sampler texture */
323 if (svga->curr.rast->templ.poly_stipple_enable) {
324 const unsigned unit = svga->state.hw_draw.fs->pstipple_sampler_unit;
325 struct svga_sampler_state *sampler = svga->polygon_stipple.sampler;
326
327 assert(sampler);
328 if (!sampler) {
329 return PIPE_OK; /* probably out of memory */
330 }
331
332 ret = SVGA3D_vgpu10_SetSamplers(svga->swc,
333 1, /* count */
334 unit, /* start */
335 SVGA3D_SHADERTYPE_PS,
336 &sampler->id);
337 }
338
339 return ret;
340 }
341
342
343 struct svga_tracked_state svga_hw_sampler = {
344 "texture sampler emit",
345 (SVGA_NEW_SAMPLER |
346 SVGA_NEW_STIPPLE |
347 SVGA_NEW_TEXTURE_FLAGS),
348 update_samplers
349 };