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