f2ca35b02053e18db6b8939c29613e700abaacb1
[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->lod = 0;
71 This->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->lod;
107
108 DBG("This=%p LODNew=%d\n", This, LODNew);
109
110 user_assert(This->base.pool == D3DPOOL_MANAGED, 0);
111
112 This->lod = MIN2(LODNew, This->base.info.last_level);
113
114 if (This->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->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->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->lod_resident != This->lod) {
177 struct pipe_resource *res;
178
179 DBG("updating LOD from %u to %u ...\n", This->lod_resident, This->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->lod_resident != -1);
196 if (FAILED(hr))
197 return hr;
198 res = This->base.resource;
199
200 if (This->lod_resident == -1) /* no levels were resident */
201 This->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->lod; l < This->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->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->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->lod; l < This->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->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->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->lod; l < This->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->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->lod);
261 } else {
262 assert(!"invalid texture type");
263 }
264
265 if (This->lod < This->lod_resident)
266 This->dirty = TRUE;
267 This->lod_resident = This->lod;
268 }
269 if (!This->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 if (tex->dirty_rect.width) {
283 for (l = 0; l <= last_level; ++l) {
284 u_box_minify_2d(&box, &tex->dirty_rect, l);
285 NineSurface9_AddDirtyRect(tex->surfaces[l], &box);
286 }
287 memset(&tex->dirty_rect, 0, sizeof(tex->dirty_rect));
288 tex->dirty_rect.depth = 1;
289 }
290 for (l = This->lod; l <= last_level; ++l)
291 NineSurface9_UploadSelf(tex->surfaces[l]);
292 } else
293 if (This->base.type == D3DRTYPE_CUBETEXTURE) {
294 struct NineCubeTexture9 *tex = NineCubeTexture9(This);
295 unsigned z;
296 struct pipe_box box;
297 box.z = 0;
298 box.depth = 1;
299
300 for (z = 0; z < 6; ++z) {
301 DBG("FACE[%u]: dirty rect=(%u,%u) (%ux%u)\n", z,
302 tex->dirty_rect[z].x, tex->dirty_rect[z].y,
303 tex->dirty_rect[z].width, tex->dirty_rect[z].height);
304
305 if (tex->dirty_rect[z].width) {
306 for (l = 0; l <= last_level; ++l) {
307 u_box_minify_2d(&box, &tex->dirty_rect[z], l);
308 NineSurface9_AddDirtyRect(tex->surfaces[l * 6 + z], &box);
309 }
310 memset(&tex->dirty_rect[z], 0, sizeof(tex->dirty_rect[z]));
311 tex->dirty_rect[z].depth = 1;
312 }
313 for (l = This->lod; l <= last_level; ++l)
314 NineSurface9_UploadSelf(tex->surfaces[l * 6 + z]);
315 }
316 } else
317 if (This->base.type == D3DRTYPE_VOLUMETEXTURE) {
318 struct NineVolumeTexture9 *tex = NineVolumeTexture9(This);
319 struct pipe_box box;
320
321 DBG("VOLUME: dirty_box=(%u,%u,%u) (%ux%ux%u)\n",
322 tex->dirty_box.x, tex->dirty_box.y, tex->dirty_box.y,
323 tex->dirty_box.width, tex->dirty_box.height, tex->dirty_box.depth);
324
325 if (tex->dirty_box.width) {
326 for (l = 0; l <= last_level; ++l) {
327 u_box_minify_2d(&box, &tex->dirty_box, l);
328 NineVolume9_AddDirtyRegion(tex->volumes[l], &tex->dirty_box);
329 }
330 memset(&tex->dirty_box, 0, sizeof(tex->dirty_box));
331 }
332 for (l = This->lod; l <= last_level; ++l)
333 NineVolume9_UploadSelf(tex->volumes[l]);
334 } else {
335 assert(!"invalid texture type");
336 }
337 This->dirty = FALSE;
338
339 if (This->base.usage & D3DUSAGE_AUTOGENMIPMAP)
340 This->dirty_mip = TRUE;
341 /* TODO: if dirty only because of lod change, only generate added levels */
342
343 DBG("DONE, generate mip maps = %i\n", This->dirty_mip);
344 return D3D_OK;
345 }
346
347 void WINAPI
348 NineBaseTexture9_GenerateMipSubLevels( struct NineBaseTexture9 *This )
349 {
350 struct pipe_resource *resource = This->base.resource;
351
352 unsigned base_level = 0;
353 unsigned last_level = This->base.info.last_level - This->lod;
354 unsigned first_layer = 0;
355 unsigned last_layer;
356 unsigned filter = This->mipfilter == D3DTEXF_POINT ? PIPE_TEX_FILTER_NEAREST
357 : PIPE_TEX_FILTER_LINEAR;
358 DBG("This=%p\n", This);
359
360 if (This->base.pool == D3DPOOL_MANAGED)
361 NineBaseTexture9_UploadSelf(This);
362 if (!This->dirty_mip)
363 return;
364 if (This->lod) {
365 ERR("AUTOGENMIPMAP if level 0 is not resident not supported yet !\n");
366 return;
367 }
368
369 if (!This->view[0])
370 NineBaseTexture9_UpdateSamplerView(This, 0);
371
372 last_layer = util_max_layer(This->view[0]->texture, base_level);
373
374 util_gen_mipmap(This->pipe, resource,
375 resource->format, base_level, last_level,
376 first_layer, last_layer, filter);
377
378 This->dirty_mip = FALSE;
379
380 NineDevice9_RestoreNonCSOState(This->base.base.device, ~0x3);
381 }
382
383 HRESULT
384 NineBaseTexture9_CreatePipeResource( struct NineBaseTexture9 *This,
385 BOOL CopyData )
386 {
387 struct pipe_context *pipe = This->pipe;
388 struct pipe_screen *screen = This->base.info.screen;
389 struct pipe_resource templ;
390 unsigned l, m;
391 struct pipe_resource *res;
392 struct pipe_resource *old = This->base.resource;
393
394 DBG("This=%p lod=%u last_level=%u\n", This,
395 This->lod, This->base.info.last_level);
396
397 assert(This->base.pool == D3DPOOL_MANAGED);
398
399 templ = This->base.info;
400
401 if (This->lod) {
402 templ.width0 = u_minify(templ.width0, This->lod);
403 templ.height0 = u_minify(templ.height0, This->lod);
404 templ.depth0 = u_minify(templ.depth0, This->lod);
405 }
406 templ.last_level = This->base.info.last_level - This->lod;
407
408 if (old) {
409 /* LOD might have changed. */
410 if (old->width0 == templ.width0 &&
411 old->height0 == templ.height0 &&
412 old->depth0 == templ.depth0)
413 return D3D_OK;
414 }
415
416 res = screen->resource_create(screen, &templ);
417 if (!res)
418 return D3DERR_OUTOFVIDEOMEMORY;
419 This->base.resource = res;
420
421 if (old && CopyData) { /* Don't return without releasing old ! */
422 struct pipe_box box;
423 box.x = 0;
424 box.y = 0;
425 box.z = 0;
426
427 l = (This->lod < This->lod_resident) ? This->lod_resident - This->lod : 0;
428 m = (This->lod < This->lod_resident) ? 0 : This->lod - This->lod_resident;
429
430 box.width = u_minify(templ.width0, l);
431 box.height = u_minify(templ.height0, l);
432 box.depth = u_minify(templ.depth0, l);
433
434 for (; l <= templ.last_level; ++l, ++m) {
435 pipe->resource_copy_region(pipe,
436 res, l, 0, 0, 0,
437 old, m, &box);
438 box.width = u_minify(box.width, 1);
439 box.height = u_minify(box.height, 1);
440 box.depth = u_minify(box.depth, 1);
441 }
442 }
443 pipe_resource_reference(&old, NULL);
444
445 return D3D_OK;
446 }
447
448 #define SWIZZLE_TO_REPLACE(s) (s == UTIL_FORMAT_SWIZZLE_0 || \
449 s == UTIL_FORMAT_SWIZZLE_1 || \
450 s == UTIL_FORMAT_SWIZZLE_NONE)
451
452 HRESULT
453 NineBaseTexture9_UpdateSamplerView( struct NineBaseTexture9 *This,
454 const int sRGB )
455 {
456 const struct util_format_description *desc;
457 struct pipe_context *pipe = This->pipe;
458 struct pipe_screen *screen = pipe->screen;
459 struct pipe_resource *resource = This->base.resource;
460 struct pipe_sampler_view templ;
461 enum pipe_format srgb_format;
462 unsigned i;
463 uint8_t swizzle[4];
464
465 DBG("This=%p sRGB=%d\n", This, sRGB);
466
467 if (unlikely(!resource)) {
468 if (unlikely(This->format == D3DFMT_NULL))
469 return D3D_OK;
470 NineBaseTexture9_Dump(This);
471 /* hack due to incorrect POOL_MANAGED handling */
472 NineBaseTexture9_GenerateMipSubLevels(This);
473 resource = This->base.resource;
474 }
475 assert(resource);
476
477 pipe_sampler_view_reference(&This->view[sRGB], NULL);
478
479 swizzle[0] = PIPE_SWIZZLE_RED;
480 swizzle[1] = PIPE_SWIZZLE_GREEN;
481 swizzle[2] = PIPE_SWIZZLE_BLUE;
482 swizzle[3] = PIPE_SWIZZLE_ALPHA;
483 desc = util_format_description(resource->format);
484 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
485 /* msdn doc is incomplete here and wrong.
486 * The only formats that can be read directly here
487 * are DF16, DF24 and INTZ.
488 * Tested on win the swizzle is
489 * R = depth, G = B = 0, A = 1 for DF16 and DF24
490 * R = G = B = A = depth for INTZ
491 * For the other ZS formats that can't be read directly
492 * but can be used as shadow map, the result is duplicated on
493 * all channel */
494 if (This->format == D3DFMT_DF16 ||
495 This->format == D3DFMT_DF24) {
496 swizzle[1] = PIPE_SWIZZLE_ZERO;
497 swizzle[2] = PIPE_SWIZZLE_ZERO;
498 swizzle[3] = PIPE_SWIZZLE_ONE;
499 } else {
500 swizzle[1] = PIPE_SWIZZLE_RED;
501 swizzle[2] = PIPE_SWIZZLE_RED;
502 swizzle[3] = PIPE_SWIZZLE_RED;
503 }
504 } else if (resource->format != PIPE_FORMAT_A8_UNORM &&
505 resource->format != PIPE_FORMAT_RGTC1_UNORM) {
506 /* exceptions:
507 * A8 should have 0.0 as default values for RGB.
508 * ATI1/RGTC1 should be r 0 0 1 (tested on windows).
509 * It is already what gallium does. All the other ones
510 * should have 1.0 for non-defined values */
511 for (i = 0; i < 4; i++) {
512 if (SWIZZLE_TO_REPLACE(desc->swizzle[i]))
513 swizzle[i] = PIPE_SWIZZLE_ONE;
514 }
515 }
516
517 /* if requested and supported, convert to the sRGB format */
518 srgb_format = util_format_srgb(resource->format);
519 if (sRGB && srgb_format != PIPE_FORMAT_NONE &&
520 screen->is_format_supported(screen, srgb_format,
521 resource->target, 0, resource->bind))
522 templ.format = srgb_format;
523 else
524 templ.format = resource->format;
525 templ.u.tex.first_layer = 0;
526 templ.u.tex.last_layer = resource->target == PIPE_TEXTURE_3D ?
527 resource->depth0 - 1 : resource->array_size - 1;
528 templ.u.tex.first_level = 0;
529 templ.u.tex.last_level = resource->last_level;
530 templ.swizzle_r = swizzle[0];
531 templ.swizzle_g = swizzle[1];
532 templ.swizzle_b = swizzle[2];
533 templ.swizzle_a = swizzle[3];
534 templ.target = resource->target;
535
536 This->view[sRGB] = pipe->create_sampler_view(pipe, resource, &templ);
537
538 DBG("sampler view = %p(resource = %p)\n", This->view[sRGB], resource);
539
540 return This->view ? D3D_OK : D3DERR_DRIVERINTERNALERROR;
541 }
542
543 void WINAPI
544 NineBaseTexture9_PreLoad( struct NineBaseTexture9 *This )
545 {
546 DBG("This=%p\n", This);
547
548 if (This->dirty && This->base.pool == D3DPOOL_MANAGED)
549 NineBaseTexture9_UploadSelf(This);
550 }
551
552 #ifdef DEBUG
553 void
554 NineBaseTexture9_Dump( struct NineBaseTexture9 *This )
555 {
556 DBG("\nNineBaseTexture9(%p->NULL/%p): Pool=%s Type=%s Usage=%s\n"
557 "Format=%s Dims=%ux%ux%u/%u LastLevel=%u Lod=%u(%u)\n", This,
558 This->base.resource,
559 nine_D3DPOOL_to_str(This->base.pool),
560 nine_D3DRTYPE_to_str(This->base.type),
561 nine_D3DUSAGE_to_str(This->base.usage),
562 d3dformat_to_string(This->format),
563 This->base.info.width0, This->base.info.height0, This->base.info.depth0,
564 This->base.info.array_size, This->base.info.last_level,
565 This->lod, This->lod_resident);
566 }
567 #endif /* DEBUG */