st/nine: Simplify Surface9 Managed resources implementation
[mesa.git] / src / gallium / state_trackers / nine / basetexture9.c
1 /*
2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 #include "basetexture9.h"
24 #include "device9.h"
25
26 /* For UploadSelf: */
27 #include "texture9.h"
28 #include "cubetexture9.h"
29 #include "volumetexture9.h"
30
31 #ifdef DEBUG
32 #include "nine_pipe.h"
33 #include "nine_dump.h"
34 #endif
35
36 #include "util/u_format.h"
37 #include "util/u_gen_mipmap.h"
38
39 #define DBG_CHANNEL DBG_BASETEXTURE
40
41 HRESULT
42 NineBaseTexture9_ctor( struct NineBaseTexture9 *This,
43 struct NineUnknownParams *pParams,
44 struct pipe_resource *initResource,
45 D3DRESOURCETYPE Type,
46 D3DFORMAT format,
47 D3DPOOL Pool,
48 DWORD Usage)
49 {
50 BOOL alloc = (Pool == D3DPOOL_DEFAULT) && !initResource &&
51 (format != D3DFMT_NULL);
52 HRESULT hr;
53
54 DBG("This=%p, pParams=%p initResource=%p Type=%d format=%d Pool=%d Usage=%d\n",
55 This, pParams, initResource, Type, format, Pool, Usage);
56
57 user_assert(!(Usage & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL)) ||
58 Pool == D3DPOOL_DEFAULT, D3DERR_INVALIDCALL);
59 user_assert(!(Usage & D3DUSAGE_DYNAMIC) ||
60 Pool != D3DPOOL_MANAGED, D3DERR_INVALIDCALL);
61
62 hr = NineResource9_ctor(&This->base, pParams, initResource, alloc, Type, Pool, Usage);
63 if (FAILED(hr))
64 return hr;
65
66 This->format = format;
67 This->pipe = pParams->device->pipe;
68 This->mipfilter = (Usage & D3DUSAGE_AUTOGENMIPMAP) ?
69 D3DTEXF_LINEAR : D3DTEXF_NONE;
70 This->managed.lod = 0;
71 This->managed.lod_resident = -1;
72 /* When a depth buffer is sampled, it is for shadow mapping, except for
73 * D3DFMT_INTZ, D3DFMT_DF16 and D3DFMT_DF24.
74 * In addition D3DFMT_INTZ can be used for both texturing and depth buffering
75 * if z write is disabled. This particular feature may not work for us in
76 * practice because OGL doesn't have that. However apparently it is known
77 * some cards have performance issues with this feature, so real apps
78 * shouldn't use it. */
79 This->shadow = (This->format != D3DFMT_INTZ && This->format != D3DFMT_DF16 &&
80 This->format != D3DFMT_DF24) &&
81 util_format_has_depth(util_format_description(This->base.info.format));
82
83 list_inithead(&This->list);
84
85 return D3D_OK;
86 }
87
88 void
89 NineBaseTexture9_dtor( struct NineBaseTexture9 *This )
90 {
91 DBG("This=%p\n", This);
92
93 pipe_sampler_view_reference(&This->view[0], NULL);
94 pipe_sampler_view_reference(&This->view[1], NULL);
95
96 if (This->list.prev != NULL && This->list.next != NULL)
97 list_del(&This->list),
98
99 NineResource9_dtor(&This->base);
100 }
101
102 DWORD WINAPI
103 NineBaseTexture9_SetLOD( struct NineBaseTexture9 *This,
104 DWORD LODNew )
105 {
106 DWORD old = This->managed.lod;
107
108 DBG("This=%p LODNew=%d\n", This, LODNew);
109
110 user_assert(This->base.pool == D3DPOOL_MANAGED, 0);
111
112 This->managed.lod = MIN2(LODNew, This->base.info.last_level);
113
114 if (This->managed.lod != old && This->bind_count && LIST_IS_EMPTY(&This->list))
115 list_add(&This->list, &This->base.base.device->update_textures);
116
117 return old;
118 }
119
120 DWORD WINAPI
121 NineBaseTexture9_GetLOD( struct NineBaseTexture9 *This )
122 {
123 DBG("This=%p\n", This);
124
125 return This->managed.lod;
126 }
127
128 DWORD WINAPI
129 NineBaseTexture9_GetLevelCount( struct NineBaseTexture9 *This )
130 {
131 DBG("This=%p\n", This);
132
133 if (This->base.usage & D3DUSAGE_AUTOGENMIPMAP)
134 return 1;
135 return This->base.info.last_level + 1;
136 }
137
138 HRESULT WINAPI
139 NineBaseTexture9_SetAutoGenFilterType( struct NineBaseTexture9 *This,
140 D3DTEXTUREFILTERTYPE FilterType )
141 {
142 DBG("This=%p FilterType=%d\n", This, FilterType);
143
144 if (!(This->base.usage & D3DUSAGE_AUTOGENMIPMAP))
145 return D3D_OK;
146 user_assert(FilterType != D3DTEXF_NONE, D3DERR_INVALIDCALL);
147
148 This->mipfilter = FilterType;
149
150 return D3D_OK;
151 }
152
153 D3DTEXTUREFILTERTYPE WINAPI
154 NineBaseTexture9_GetAutoGenFilterType( struct NineBaseTexture9 *This )
155 {
156 DBG("This=%p\n", This);
157
158 return This->mipfilter;
159 }
160
161 HRESULT
162 NineBaseTexture9_UploadSelf( struct NineBaseTexture9 *This )
163 {
164 HRESULT hr;
165 unsigned last_level = This->base.info.last_level;
166 unsigned l;
167
168 DBG("This=%p dirty=%i type=%s\n", This, This->managed.dirty,
169 nine_D3DRTYPE_to_str(This->base.type));
170
171 assert(This->base.pool == D3DPOOL_MANAGED);
172
173 if (This->base.usage & D3DUSAGE_AUTOGENMIPMAP)
174 last_level = 0; /* TODO: What if level 0 is not resident ? */
175
176 if (This->managed.lod_resident != This->managed.lod) {
177 struct pipe_resource *res;
178
179 DBG("updating LOD from %u to %u ...\n", This->managed.lod_resident, This->managed.lod);
180
181 pipe_sampler_view_reference(&This->view[0], NULL);
182 pipe_sampler_view_reference(&This->view[1], NULL);
183
184 if (This->bind_count) {
185 /* mark state dirty */
186 struct nine_state *state = &This->base.base.device->state;
187 unsigned s;
188 for (s = 0; s < NINE_MAX_SAMPLERS; ++s)
189 if (state->texture[s] == This)
190 state->changed.texture |= 1 << s;
191 if (state->changed.texture)
192 state->changed.group |= NINE_STATE_TEXTURE;
193 }
194
195 hr = NineBaseTexture9_CreatePipeResource(This, This->managed.lod_resident != -1);
196 if (FAILED(hr))
197 return hr;
198 res = This->base.resource;
199
200 if (This->managed.lod_resident == -1) /* no levels were resident */
201 This->managed.lod_resident = This->base.info.last_level + 1;
202
203 if (This->base.type == D3DRTYPE_TEXTURE) {
204 struct NineTexture9 *tex = NineTexture9(This);
205 struct pipe_box box;
206
207 /* Mark uninitialized levels as dirty. */
208 box.x = box.y = box.z = 0;
209 box.depth = 1;
210 for (l = This->managed.lod; l < This->managed.lod_resident; ++l) {
211 box.width = u_minify(This->base.info.width0, l);
212 box.height = u_minify(This->base.info.height0, l);
213 NineSurface9_AddDirtyRect(tex->surfaces[l], &box);
214 }
215 for (l = 0; l < This->managed.lod; ++l)
216 NineSurface9_SetResource(tex->surfaces[l], NULL, -1);
217 for (; l <= This->base.info.last_level; ++l)
218 NineSurface9_SetResource(tex->surfaces[l], res, l - This->managed.lod);
219 } else
220 if (This->base.type == D3DRTYPE_CUBETEXTURE) {
221 struct NineCubeTexture9 *tex = NineCubeTexture9(This);
222 struct pipe_box box;
223 unsigned z;
224
225 /* Mark uninitialized levels as dirty. */
226 box.x = box.y = box.z = 0;
227 box.depth = 1;
228 for (l = This->managed.lod; l < This->managed.lod_resident; ++l) {
229 box.width = u_minify(This->base.info.width0, l);
230 box.height = u_minify(This->base.info.height0, l);
231 for (z = 0; z < 6; ++z)
232 NineSurface9_AddDirtyRect(tex->surfaces[l * 6 + z], &box);
233 }
234 for (l = 0; l < This->managed.lod; ++l) {
235 for (z = 0; z < 6; ++z)
236 NineSurface9_SetResource(tex->surfaces[l * 6 + z],
237 NULL, -1);
238 }
239 for (; l <= This->base.info.last_level; ++l) {
240 for (z = 0; z < 6; ++z)
241 NineSurface9_SetResource(tex->surfaces[l * 6 + z],
242 res, l - This->managed.lod);
243 }
244 } else
245 if (This->base.type == D3DRTYPE_VOLUMETEXTURE) {
246 struct NineVolumeTexture9 *tex = NineVolumeTexture9(This);
247 struct pipe_box box;
248
249 /* Mark uninitialized levels as dirty. */
250 box.x = box.y = box.z = 0;
251 for (l = This->managed.lod; l < This->managed.lod_resident; ++l) {
252 box.width = u_minify(This->base.info.width0, l);
253 box.height = u_minify(This->base.info.height0, l);
254 box.depth = u_minify(This->base.info.depth0, l);
255 NineVolume9_AddDirtyRegion(tex->volumes[l], &box);
256 }
257 for (l = 0; l < This->managed.lod; ++l)
258 NineVolume9_SetResource(tex->volumes[l], NULL, -1);
259 for (; l <= This->base.info.last_level; ++l)
260 NineVolume9_SetResource(tex->volumes[l], res, l - This->managed.lod);
261 } else {
262 assert(!"invalid texture type");
263 }
264
265 if (This->managed.lod < This->managed.lod_resident)
266 This->managed.dirty = TRUE;
267 This->managed.lod_resident = This->managed.lod;
268 }
269 if (!This->managed.dirty)
270 return D3D_OK;
271
272 if (This->base.type == D3DRTYPE_TEXTURE) {
273 struct NineTexture9 *tex = NineTexture9(This);
274 struct pipe_box box;
275 box.z = 0;
276 box.depth = 1;
277
278 DBG("TEXTURE: dirty rect=(%u,%u) (%ux%u)\n",
279 tex->dirty_rect.x, tex->dirty_rect.y,
280 tex->dirty_rect.width, tex->dirty_rect.height);
281
282 /* Note: for l < This->managed.lod, the resource is
283 * non-existing, and thus will be entirely re-uploaded
284 * if This->managed.lod changes */
285 if (tex->dirty_rect.width) {
286 for (l = This->managed.lod; l <= last_level; ++l) {
287 u_box_minify_2d(&box, &tex->dirty_rect, l);
288 NineSurface9_UploadSelf(tex->surfaces[l], &box);
289 }
290 memset(&tex->dirty_rect, 0, sizeof(tex->dirty_rect));
291 tex->dirty_rect.depth = 1;
292 }
293 } else
294 if (This->base.type == D3DRTYPE_CUBETEXTURE) {
295 struct NineCubeTexture9 *tex = NineCubeTexture9(This);
296 unsigned z;
297 struct pipe_box box;
298 box.z = 0;
299 box.depth = 1;
300
301 for (z = 0; z < 6; ++z) {
302 DBG("FACE[%u]: dirty rect=(%u,%u) (%ux%u)\n", z,
303 tex->dirty_rect[z].x, tex->dirty_rect[z].y,
304 tex->dirty_rect[z].width, tex->dirty_rect[z].height);
305
306 if (tex->dirty_rect[z].width) {
307 for (l = This->managed.lod; l <= last_level; ++l) {
308 u_box_minify_2d(&box, &tex->dirty_rect[z], l);
309 NineSurface9_UploadSelf(tex->surfaces[l * 6 + z], &box);
310 }
311 memset(&tex->dirty_rect[z], 0, sizeof(tex->dirty_rect[z]));
312 tex->dirty_rect[z].depth = 1;
313 }
314 }
315 } else
316 if (This->base.type == D3DRTYPE_VOLUMETEXTURE) {
317 struct NineVolumeTexture9 *tex = NineVolumeTexture9(This);
318 struct pipe_box box;
319
320 DBG("VOLUME: dirty_box=(%u,%u,%u) (%ux%ux%u)\n",
321 tex->dirty_box.x, tex->dirty_box.y, tex->dirty_box.y,
322 tex->dirty_box.width, tex->dirty_box.height, tex->dirty_box.depth);
323
324 if (tex->dirty_box.width) {
325 for (l = 0; l <= last_level; ++l) {
326 u_box_minify_2d(&box, &tex->dirty_box, l);
327 NineVolume9_AddDirtyRegion(tex->volumes[l], &tex->dirty_box);
328 }
329 memset(&tex->dirty_box, 0, sizeof(tex->dirty_box));
330 }
331 for (l = This->managed.lod; l <= last_level; ++l)
332 NineVolume9_UploadSelf(tex->volumes[l]);
333 } else {
334 assert(!"invalid texture type");
335 }
336 This->managed.dirty = FALSE;
337
338 if (This->base.usage & D3DUSAGE_AUTOGENMIPMAP)
339 This->dirty_mip = TRUE;
340 /* TODO: if dirty only because of lod change, only generate added levels */
341
342 DBG("DONE, generate mip maps = %i\n", This->dirty_mip);
343 return D3D_OK;
344 }
345
346 void WINAPI
347 NineBaseTexture9_GenerateMipSubLevels( struct NineBaseTexture9 *This )
348 {
349 struct pipe_resource *resource = This->base.resource;
350
351 unsigned base_level = 0;
352 unsigned last_level = This->base.info.last_level - This->managed.lod;
353 unsigned first_layer = 0;
354 unsigned last_layer;
355 unsigned filter = This->mipfilter == D3DTEXF_POINT ? PIPE_TEX_FILTER_NEAREST
356 : PIPE_TEX_FILTER_LINEAR;
357 DBG("This=%p\n", This);
358
359 if (This->base.pool == D3DPOOL_MANAGED)
360 NineBaseTexture9_UploadSelf(This);
361 if (!This->dirty_mip)
362 return;
363 if (This->managed.lod) {
364 ERR("AUTOGENMIPMAP if level 0 is not resident not supported yet !\n");
365 return;
366 }
367
368 if (!This->view[0])
369 NineBaseTexture9_UpdateSamplerView(This, 0);
370
371 last_layer = util_max_layer(This->view[0]->texture, base_level);
372
373 util_gen_mipmap(This->pipe, resource,
374 resource->format, base_level, last_level,
375 first_layer, last_layer, filter);
376
377 This->dirty_mip = FALSE;
378
379 NineDevice9_RestoreNonCSOState(This->base.base.device, ~0x3);
380 }
381
382 HRESULT
383 NineBaseTexture9_CreatePipeResource( struct NineBaseTexture9 *This,
384 BOOL CopyData )
385 {
386 struct pipe_context *pipe = This->pipe;
387 struct pipe_screen *screen = This->base.info.screen;
388 struct pipe_resource templ;
389 unsigned l, m;
390 struct pipe_resource *res;
391 struct pipe_resource *old = This->base.resource;
392
393 DBG("This=%p lod=%u last_level=%u\n", This,
394 This->managed.lod, This->base.info.last_level);
395
396 assert(This->base.pool == D3DPOOL_MANAGED);
397
398 templ = This->base.info;
399
400 if (This->managed.lod) {
401 templ.width0 = u_minify(templ.width0, This->managed.lod);
402 templ.height0 = u_minify(templ.height0, This->managed.lod);
403 templ.depth0 = u_minify(templ.depth0, This->managed.lod);
404 }
405 templ.last_level = This->base.info.last_level - This->managed.lod;
406
407 if (old) {
408 /* LOD might have changed. */
409 if (old->width0 == templ.width0 &&
410 old->height0 == templ.height0 &&
411 old->depth0 == templ.depth0)
412 return D3D_OK;
413 }
414
415 res = screen->resource_create(screen, &templ);
416 if (!res)
417 return D3DERR_OUTOFVIDEOMEMORY;
418 This->base.resource = res;
419
420 if (old && CopyData) { /* Don't return without releasing old ! */
421 struct pipe_box box;
422 box.x = 0;
423 box.y = 0;
424 box.z = 0;
425
426 l = (This->managed.lod < This->managed.lod_resident) ? This->managed.lod_resident - This->managed.lod : 0;
427 m = (This->managed.lod < This->managed.lod_resident) ? 0 : This->managed.lod - This->managed.lod_resident;
428
429 box.width = u_minify(templ.width0, l);
430 box.height = u_minify(templ.height0, l);
431 box.depth = u_minify(templ.depth0, l);
432
433 for (; l <= templ.last_level; ++l, ++m) {
434 pipe->resource_copy_region(pipe,
435 res, l, 0, 0, 0,
436 old, m, &box);
437 box.width = u_minify(box.width, 1);
438 box.height = u_minify(box.height, 1);
439 box.depth = u_minify(box.depth, 1);
440 }
441 }
442 pipe_resource_reference(&old, NULL);
443
444 return D3D_OK;
445 }
446
447 #define SWIZZLE_TO_REPLACE(s) (s == UTIL_FORMAT_SWIZZLE_0 || \
448 s == UTIL_FORMAT_SWIZZLE_1 || \
449 s == UTIL_FORMAT_SWIZZLE_NONE)
450
451 HRESULT
452 NineBaseTexture9_UpdateSamplerView( struct NineBaseTexture9 *This,
453 const int sRGB )
454 {
455 const struct util_format_description *desc;
456 struct pipe_context *pipe = This->pipe;
457 struct pipe_screen *screen = pipe->screen;
458 struct pipe_resource *resource = This->base.resource;
459 struct pipe_sampler_view templ;
460 enum pipe_format srgb_format;
461 unsigned i;
462 uint8_t swizzle[4];
463
464 DBG("This=%p sRGB=%d\n", This, sRGB);
465
466 if (unlikely(!resource)) {
467 if (unlikely(This->format == D3DFMT_NULL))
468 return D3D_OK;
469 NineBaseTexture9_Dump(This);
470 /* hack due to incorrect POOL_MANAGED handling */
471 NineBaseTexture9_GenerateMipSubLevels(This);
472 resource = This->base.resource;
473 }
474 assert(resource);
475
476 pipe_sampler_view_reference(&This->view[sRGB], NULL);
477
478 swizzle[0] = PIPE_SWIZZLE_RED;
479 swizzle[1] = PIPE_SWIZZLE_GREEN;
480 swizzle[2] = PIPE_SWIZZLE_BLUE;
481 swizzle[3] = PIPE_SWIZZLE_ALPHA;
482 desc = util_format_description(resource->format);
483 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
484 /* msdn doc is incomplete here and wrong.
485 * The only formats that can be read directly here
486 * are DF16, DF24 and INTZ.
487 * Tested on win the swizzle is
488 * R = depth, G = B = 0, A = 1 for DF16 and DF24
489 * R = G = B = A = depth for INTZ
490 * For the other ZS formats that can't be read directly
491 * but can be used as shadow map, the result is duplicated on
492 * all channel */
493 if (This->format == D3DFMT_DF16 ||
494 This->format == D3DFMT_DF24) {
495 swizzle[1] = PIPE_SWIZZLE_ZERO;
496 swizzle[2] = PIPE_SWIZZLE_ZERO;
497 swizzle[3] = PIPE_SWIZZLE_ONE;
498 } else {
499 swizzle[1] = PIPE_SWIZZLE_RED;
500 swizzle[2] = PIPE_SWIZZLE_RED;
501 swizzle[3] = PIPE_SWIZZLE_RED;
502 }
503 } else if (resource->format != PIPE_FORMAT_A8_UNORM &&
504 resource->format != PIPE_FORMAT_RGTC1_UNORM) {
505 /* exceptions:
506 * A8 should have 0.0 as default values for RGB.
507 * ATI1/RGTC1 should be r 0 0 1 (tested on windows).
508 * It is already what gallium does. All the other ones
509 * should have 1.0 for non-defined values */
510 for (i = 0; i < 4; i++) {
511 if (SWIZZLE_TO_REPLACE(desc->swizzle[i]))
512 swizzle[i] = PIPE_SWIZZLE_ONE;
513 }
514 }
515
516 /* if requested and supported, convert to the sRGB format */
517 srgb_format = util_format_srgb(resource->format);
518 if (sRGB && srgb_format != PIPE_FORMAT_NONE &&
519 screen->is_format_supported(screen, srgb_format,
520 resource->target, 0, resource->bind))
521 templ.format = srgb_format;
522 else
523 templ.format = resource->format;
524 templ.u.tex.first_layer = 0;
525 templ.u.tex.last_layer = resource->target == PIPE_TEXTURE_3D ?
526 resource->depth0 - 1 : resource->array_size - 1;
527 templ.u.tex.first_level = 0;
528 templ.u.tex.last_level = resource->last_level;
529 templ.swizzle_r = swizzle[0];
530 templ.swizzle_g = swizzle[1];
531 templ.swizzle_b = swizzle[2];
532 templ.swizzle_a = swizzle[3];
533 templ.target = resource->target;
534
535 This->view[sRGB] = pipe->create_sampler_view(pipe, resource, &templ);
536
537 DBG("sampler view = %p(resource = %p)\n", This->view[sRGB], resource);
538
539 return This->view ? D3D_OK : D3DERR_DRIVERINTERNALERROR;
540 }
541
542 void WINAPI
543 NineBaseTexture9_PreLoad( struct NineBaseTexture9 *This )
544 {
545 DBG("This=%p\n", This);
546
547 if (This->managed.dirty && This->base.pool == D3DPOOL_MANAGED)
548 NineBaseTexture9_UploadSelf(This);
549 }
550
551 #ifdef DEBUG
552 void
553 NineBaseTexture9_Dump( struct NineBaseTexture9 *This )
554 {
555 DBG("\nNineBaseTexture9(%p->NULL/%p): Pool=%s Type=%s Usage=%s\n"
556 "Format=%s Dims=%ux%ux%u/%u LastLevel=%u Lod=%u(%u)\n", This,
557 This->base.resource,
558 nine_D3DPOOL_to_str(This->base.pool),
559 nine_D3DRTYPE_to_str(This->base.type),
560 nine_D3DUSAGE_to_str(This->base.usage),
561 d3dformat_to_string(This->format),
562 This->base.info.width0, This->base.info.height0, This->base.info.depth0,
563 This->base.info.array_size, This->base.info.last_level,
564 This->managed.lod, This->managed.lod_resident);
565 }
566 #endif /* DEBUG */