2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
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:
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
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. */
23 #include "basetexture9.h"
28 #include "cubetexture9.h"
29 #include "volumetexture9.h"
32 #include "nine_pipe.h"
33 #include "nine_dump.h"
36 #include "util/u_format.h"
37 #include "util/u_gen_mipmap.h"
39 #define DBG_CHANNEL DBG_BASETEXTURE
42 NineBaseTexture9_ctor( struct NineBaseTexture9
*This
,
43 struct NineUnknownParams
*pParams
,
44 struct pipe_resource
*initResource
,
50 BOOL alloc
= (Pool
== D3DPOOL_DEFAULT
) && !initResource
&&
51 (format
!= D3DFMT_NULL
);
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
);
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
);
62 hr
= NineResource9_ctor(&This
->base
, pParams
, initResource
, alloc
, Type
, Pool
, Usage
);
66 This
->format
= format
;
67 This
->pipe
= pParams
->device
->pipe
;
68 This
->mipfilter
= (Usage
& D3DUSAGE_AUTOGENMIPMAP
) ?
69 D3DTEXF_LINEAR
: D3DTEXF_NONE
;
71 This
->lod_resident
= -1;
72 This
->shadow
= This
->format
!= D3DFMT_INTZ
&& util_format_has_depth(
73 util_format_description(This
->base
.info
.format
));
75 list_inithead(&This
->list
);
81 NineBaseTexture9_dtor( struct NineBaseTexture9
*This
)
83 DBG("This=%p\n", This
);
85 pipe_sampler_view_reference(&This
->view
[0], NULL
);
86 pipe_sampler_view_reference(&This
->view
[1], NULL
);
88 list_del(&This
->list
),
90 NineResource9_dtor(&This
->base
);
94 NineBaseTexture9_SetLOD( struct NineBaseTexture9
*This
,
97 DWORD old
= This
->lod
;
99 DBG("This=%p LODNew=%d\n", This
, LODNew
);
101 user_assert(This
->base
.pool
== D3DPOOL_MANAGED
, 0);
103 This
->lod
= MIN2(LODNew
, This
->base
.info
.last_level
);
105 if (This
->lod
!= old
&& This
->bind_count
&& LIST_IS_EMPTY(&This
->list
))
106 list_add(&This
->list
, &This
->base
.base
.device
->update_textures
);
112 NineBaseTexture9_GetLOD( struct NineBaseTexture9
*This
)
114 DBG("This=%p\n", This
);
120 NineBaseTexture9_GetLevelCount( struct NineBaseTexture9
*This
)
122 DBG("This=%p\n", This
);
124 if (This
->base
.usage
& D3DUSAGE_AUTOGENMIPMAP
)
126 return This
->base
.info
.last_level
+ 1;
130 NineBaseTexture9_SetAutoGenFilterType( struct NineBaseTexture9
*This
,
131 D3DTEXTUREFILTERTYPE FilterType
)
133 DBG("This=%p FilterType=%d\n", This
, FilterType
);
135 if (!(This
->base
.usage
& D3DUSAGE_AUTOGENMIPMAP
))
137 user_assert(FilterType
!= D3DTEXF_NONE
, D3DERR_INVALIDCALL
);
139 This
->mipfilter
= FilterType
;
144 D3DTEXTUREFILTERTYPE WINAPI
145 NineBaseTexture9_GetAutoGenFilterType( struct NineBaseTexture9
*This
)
147 DBG("This=%p\n", This
);
149 return This
->mipfilter
;
153 NineBaseTexture9_UploadSelf( struct NineBaseTexture9
*This
)
156 unsigned last_level
= This
->base
.info
.last_level
;
159 DBG("This=%p dirty=%i type=%s\n", This
, This
->dirty
,
160 nine_D3DRTYPE_to_str(This
->base
.type
));
162 assert(This
->base
.pool
== D3DPOOL_MANAGED
);
164 if (This
->base
.usage
& D3DUSAGE_AUTOGENMIPMAP
)
165 last_level
= 0; /* TODO: What if level 0 is not resident ? */
167 if (This
->lod_resident
!= This
->lod
) {
168 struct pipe_resource
*res
;
170 DBG("updating LOD from %u to %u ...\n", This
->lod_resident
, This
->lod
);
172 pipe_sampler_view_reference(&This
->view
[0], NULL
);
173 pipe_sampler_view_reference(&This
->view
[1], NULL
);
175 if (This
->bind_count
) {
176 /* mark state dirty */
177 struct nine_state
*state
= &This
->base
.base
.device
->state
;
179 for (s
= 0; s
< NINE_MAX_SAMPLERS
; ++s
)
180 if (state
->texture
[s
] == This
)
181 state
->changed
.texture
|= 1 << s
;
182 if (state
->changed
.texture
)
183 state
->changed
.group
|= NINE_STATE_TEXTURE
;
186 hr
= NineBaseTexture9_CreatePipeResource(This
, This
->lod_resident
!= -1);
189 res
= This
->base
.resource
;
191 if (This
->lod_resident
== -1) /* no levels were resident */
192 This
->lod_resident
= This
->base
.info
.last_level
+ 1;
194 if (This
->base
.type
== D3DRTYPE_TEXTURE
) {
195 struct NineTexture9
*tex
= NineTexture9(This
);
198 /* Mark uninitialized levels as dirty. */
199 box
.x
= box
.y
= box
.z
= 0;
201 for (l
= This
->lod
; l
< This
->lod_resident
; ++l
) {
202 box
.width
= u_minify(This
->base
.info
.width0
, l
);
203 box
.height
= u_minify(This
->base
.info
.height0
, l
);
204 NineSurface9_AddDirtyRect(tex
->surfaces
[l
], &box
);
206 for (l
= 0; l
< This
->lod
; ++l
)
207 NineSurface9_SetResource(tex
->surfaces
[l
], NULL
, -1);
208 for (; l
<= This
->base
.info
.last_level
; ++l
)
209 NineSurface9_SetResource(tex
->surfaces
[l
], res
, l
- This
->lod
);
211 if (This
->base
.type
== D3DRTYPE_CUBETEXTURE
) {
212 struct NineCubeTexture9
*tex
= NineCubeTexture9(This
);
216 /* Mark uninitialized levels as dirty. */
217 box
.x
= box
.y
= box
.z
= 0;
219 for (l
= This
->lod
; l
< This
->lod_resident
; ++l
) {
220 box
.width
= u_minify(This
->base
.info
.width0
, l
);
221 box
.height
= u_minify(This
->base
.info
.height0
, l
);
222 for (z
= 0; z
< 6; ++z
)
223 NineSurface9_AddDirtyRect(tex
->surfaces
[l
* 6 + z
], &box
);
225 for (l
= 0; l
< This
->lod
; ++l
) {
226 for (z
= 0; z
< 6; ++z
)
227 NineSurface9_SetResource(tex
->surfaces
[l
* 6 + z
],
230 for (; l
<= This
->base
.info
.last_level
; ++l
) {
231 for (z
= 0; z
< 6; ++z
)
232 NineSurface9_SetResource(tex
->surfaces
[l
* 6 + z
],
236 if (This
->base
.type
== D3DRTYPE_VOLUMETEXTURE
) {
237 struct NineVolumeTexture9
*tex
= NineVolumeTexture9(This
);
240 /* Mark uninitialized levels as dirty. */
241 box
.x
= box
.y
= box
.z
= 0;
242 for (l
= This
->lod
; l
< This
->lod_resident
; ++l
) {
243 box
.width
= u_minify(This
->base
.info
.width0
, l
);
244 box
.height
= u_minify(This
->base
.info
.height0
, l
);
245 box
.depth
= u_minify(This
->base
.info
.depth0
, l
);
246 NineVolume9_AddDirtyRegion(tex
->volumes
[l
], &box
);
248 for (l
= 0; l
< This
->lod
; ++l
)
249 NineVolume9_SetResource(tex
->volumes
[l
], NULL
, -1);
250 for (; l
<= This
->base
.info
.last_level
; ++l
)
251 NineVolume9_SetResource(tex
->volumes
[l
], res
, l
- This
->lod
);
253 assert(!"invalid texture type");
256 if (This
->lod
< This
->lod_resident
)
258 This
->lod_resident
= This
->lod
;
263 if (This
->base
.type
== D3DRTYPE_TEXTURE
) {
264 struct NineTexture9
*tex
= NineTexture9(This
);
269 DBG("TEXTURE: dirty rect=(%u,%u) (%ux%u)\n",
270 tex
->dirty_rect
.x
, tex
->dirty_rect
.y
,
271 tex
->dirty_rect
.width
, tex
->dirty_rect
.height
);
273 if (tex
->dirty_rect
.width
) {
274 for (l
= 0; l
<= last_level
; ++l
) {
275 u_box_minify_2d(&box
, &tex
->dirty_rect
, l
);
276 NineSurface9_AddDirtyRect(tex
->surfaces
[l
], &box
);
278 memset(&tex
->dirty_rect
, 0, sizeof(tex
->dirty_rect
));
279 tex
->dirty_rect
.depth
= 1;
281 for (l
= This
->lod
; l
<= last_level
; ++l
)
282 NineSurface9_UploadSelf(tex
->surfaces
[l
]);
284 if (This
->base
.type
== D3DRTYPE_CUBETEXTURE
) {
285 struct NineCubeTexture9
*tex
= NineCubeTexture9(This
);
291 for (z
= 0; z
< 6; ++z
) {
292 DBG("FACE[%u]: dirty rect=(%u,%u) (%ux%u)\n", z
,
293 tex
->dirty_rect
[z
].x
, tex
->dirty_rect
[z
].y
,
294 tex
->dirty_rect
[z
].width
, tex
->dirty_rect
[z
].height
);
296 if (tex
->dirty_rect
[z
].width
) {
297 for (l
= 0; l
<= last_level
; ++l
) {
298 u_box_minify_2d(&box
, &tex
->dirty_rect
[z
], l
);
299 NineSurface9_AddDirtyRect(tex
->surfaces
[l
* 6 + z
], &box
);
301 memset(&tex
->dirty_rect
[z
], 0, sizeof(tex
->dirty_rect
[z
]));
302 tex
->dirty_rect
[z
].depth
= 1;
304 for (l
= This
->lod
; l
<= last_level
; ++l
)
305 NineSurface9_UploadSelf(tex
->surfaces
[l
* 6 + z
]);
308 if (This
->base
.type
== D3DRTYPE_VOLUMETEXTURE
) {
309 struct NineVolumeTexture9
*tex
= NineVolumeTexture9(This
);
312 DBG("VOLUME: dirty_box=(%u,%u,%u) (%ux%ux%u)\n",
313 tex
->dirty_box
.x
, tex
->dirty_box
.y
, tex
->dirty_box
.y
,
314 tex
->dirty_box
.width
, tex
->dirty_box
.height
, tex
->dirty_box
.depth
);
316 if (tex
->dirty_box
.width
) {
317 for (l
= 0; l
<= last_level
; ++l
) {
318 u_box_minify_2d(&box
, &tex
->dirty_box
, l
);
319 NineVolume9_AddDirtyRegion(tex
->volumes
[l
], &tex
->dirty_box
);
321 memset(&tex
->dirty_box
, 0, sizeof(tex
->dirty_box
));
323 for (l
= This
->lod
; l
<= last_level
; ++l
)
324 NineVolume9_UploadSelf(tex
->volumes
[l
]);
326 assert(!"invalid texture type");
330 if (This
->base
.usage
& D3DUSAGE_AUTOGENMIPMAP
)
331 This
->dirty_mip
= TRUE
;
332 /* TODO: if dirty only because of lod change, only generate added levels */
334 DBG("DONE, generate mip maps = %i\n", This
->dirty_mip
);
339 NineBaseTexture9_GenerateMipSubLevels( struct NineBaseTexture9
*This
)
341 struct pipe_resource
*resource
= This
->base
.resource
;
343 unsigned base_level
= 0;
344 unsigned last_level
= This
->base
.info
.last_level
- This
->lod
;
345 unsigned first_layer
= 0;
347 unsigned filter
= This
->mipfilter
== D3DTEXF_POINT
? PIPE_TEX_FILTER_NEAREST
348 : PIPE_TEX_FILTER_LINEAR
;
349 DBG("This=%p\n", This
);
351 if (This
->base
.pool
== D3DPOOL_MANAGED
)
352 NineBaseTexture9_UploadSelf(This
);
353 if (!This
->dirty_mip
)
356 ERR("AUTOGENMIPMAP if level 0 is not resident not supported yet !\n");
361 NineBaseTexture9_UpdateSamplerView(This
, 0);
363 last_layer
= util_max_layer(This
->view
[0]->texture
, base_level
);
365 util_gen_mipmap(This
->pipe
, resource
,
366 resource
->format
, base_level
, last_level
,
367 first_layer
, last_layer
, filter
);
369 This
->dirty_mip
= FALSE
;
371 NineDevice9_RestoreNonCSOState(This
->base
.base
.device
, ~0x3);
375 NineBaseTexture9_CreatePipeResource( struct NineBaseTexture9
*This
,
378 struct pipe_context
*pipe
= This
->pipe
;
379 struct pipe_screen
*screen
= This
->base
.info
.screen
;
380 struct pipe_resource templ
;
382 struct pipe_resource
*res
;
383 struct pipe_resource
*old
= This
->base
.resource
;
385 DBG("This=%p lod=%u last_level=%u\n", This
,
386 This
->lod
, This
->base
.info
.last_level
);
388 assert(This
->base
.pool
== D3DPOOL_MANAGED
);
390 templ
= This
->base
.info
;
393 templ
.width0
= u_minify(templ
.width0
, This
->lod
);
394 templ
.height0
= u_minify(templ
.height0
, This
->lod
);
395 templ
.depth0
= u_minify(templ
.depth0
, This
->lod
);
397 templ
.last_level
= This
->base
.info
.last_level
- This
->lod
;
400 /* LOD might have changed. */
401 if (old
->width0
== templ
.width0
&&
402 old
->height0
== templ
.height0
&&
403 old
->depth0
== templ
.depth0
)
407 res
= screen
->resource_create(screen
, &templ
);
409 return D3DERR_OUTOFVIDEOMEMORY
;
410 This
->base
.resource
= res
;
412 if (old
&& CopyData
) { /* Don't return without releasing old ! */
418 l
= (This
->lod
< This
->lod_resident
) ? This
->lod_resident
- This
->lod
: 0;
419 m
= (This
->lod
< This
->lod_resident
) ? 0 : This
->lod
- This
->lod_resident
;
421 box
.width
= u_minify(templ
.width0
, l
);
422 box
.height
= u_minify(templ
.height0
, l
);
423 box
.depth
= u_minify(templ
.depth0
, l
);
425 for (; l
<= templ
.last_level
; ++l
, ++m
) {
426 pipe
->resource_copy_region(pipe
,
429 box
.width
= u_minify(box
.width
, 1);
430 box
.height
= u_minify(box
.height
, 1);
431 box
.depth
= u_minify(box
.depth
, 1);
434 pipe_resource_reference(&old
, NULL
);
439 #define SWIZZLE_TO_REPLACE(s) (s == UTIL_FORMAT_SWIZZLE_0 || \
440 s == UTIL_FORMAT_SWIZZLE_1 || \
441 s == UTIL_FORMAT_SWIZZLE_NONE)
444 NineBaseTexture9_UpdateSamplerView( struct NineBaseTexture9
*This
,
447 const struct util_format_description
*desc
;
448 struct pipe_context
*pipe
= This
->pipe
;
449 struct pipe_screen
*screen
= pipe
->screen
;
450 struct pipe_resource
*resource
= This
->base
.resource
;
451 struct pipe_sampler_view templ
;
452 enum pipe_format srgb_format
;
456 DBG("This=%p sRGB=%d\n", This
, sRGB
);
458 if (unlikely(!resource
)) {
459 if (unlikely(This
->format
== D3DFMT_NULL
))
461 NineBaseTexture9_Dump(This
);
462 /* hack due to incorrect POOL_MANAGED handling */
463 NineBaseTexture9_GenerateMipSubLevels(This
);
464 resource
= This
->base
.resource
;
468 pipe_sampler_view_reference(&This
->view
[sRGB
], NULL
);
470 swizzle
[0] = PIPE_SWIZZLE_RED
;
471 swizzle
[1] = PIPE_SWIZZLE_GREEN
;
472 swizzle
[2] = PIPE_SWIZZLE_BLUE
;
473 swizzle
[3] = PIPE_SWIZZLE_ALPHA
;
474 desc
= util_format_description(resource
->format
);
475 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) {
476 /* msdn doc is incomplete here and wrong.
477 * The only formats that can be read directly here
478 * are DF16, DF24 and INTZ.
479 * Tested on win the swizzle is
480 * R = depth, G = B = 0, A = 1 for DF16 and DF24
481 * R = G = B = A = depth for INTZ
482 * For the other ZS formats that can't be read directly
483 * but can be used as shadow map, the result is duplicated on
485 if (This
->format
== D3DFMT_DF16
||
486 This
->format
== D3DFMT_DF24
) {
487 swizzle
[1] = PIPE_SWIZZLE_ZERO
;
488 swizzle
[2] = PIPE_SWIZZLE_ZERO
;
489 swizzle
[3] = PIPE_SWIZZLE_ONE
;
491 swizzle
[1] = PIPE_SWIZZLE_RED
;
492 swizzle
[2] = PIPE_SWIZZLE_RED
;
493 swizzle
[3] = PIPE_SWIZZLE_RED
;
495 } else if (resource
->format
!= PIPE_FORMAT_A8_UNORM
&&
496 resource
->format
!= PIPE_FORMAT_RGTC1_UNORM
) {
498 * A8 should have 0.0 as default values for RGB.
499 * ATI1/RGTC1 should be r 0 0 1 (tested on windows).
500 * It is already what gallium does. All the other ones
501 * should have 1.0 for non-defined values */
502 for (i
= 0; i
< 4; i
++) {
503 if (SWIZZLE_TO_REPLACE(desc
->swizzle
[i
]))
504 swizzle
[i
] = PIPE_SWIZZLE_ONE
;
508 /* if requested and supported, convert to the sRGB format */
509 srgb_format
= util_format_srgb(resource
->format
);
510 if (sRGB
&& srgb_format
!= PIPE_FORMAT_NONE
&&
511 screen
->is_format_supported(screen
, srgb_format
,
512 resource
->target
, 0, resource
->bind
))
513 templ
.format
= srgb_format
;
515 templ
.format
= resource
->format
;
516 templ
.u
.tex
.first_layer
= 0;
517 templ
.u
.tex
.last_layer
= resource
->target
== PIPE_TEXTURE_3D
?
518 resource
->depth0
- 1 : resource
->array_size
- 1;
519 templ
.u
.tex
.first_level
= 0;
520 templ
.u
.tex
.last_level
= resource
->last_level
;
521 templ
.swizzle_r
= swizzle
[0];
522 templ
.swizzle_g
= swizzle
[1];
523 templ
.swizzle_b
= swizzle
[2];
524 templ
.swizzle_a
= swizzle
[3];
525 templ
.target
= resource
->target
;
527 This
->view
[sRGB
] = pipe
->create_sampler_view(pipe
, resource
, &templ
);
529 DBG("sampler view = %p(resource = %p)\n", This
->view
[sRGB
], resource
);
531 return This
->view
? D3D_OK
: D3DERR_DRIVERINTERNALERROR
;
535 NineBaseTexture9_PreLoad( struct NineBaseTexture9
*This
)
537 DBG("This=%p\n", This
);
539 if (This
->dirty
&& This
->base
.pool
== D3DPOOL_MANAGED
)
540 NineBaseTexture9_UploadSelf(This
);
545 NineBaseTexture9_Dump( struct NineBaseTexture9
*This
)
547 DBG("\nNineBaseTexture9(%p->NULL/%p): Pool=%s Type=%s Usage=%s\n"
548 "Format=%s Dims=%ux%ux%u/%u LastLevel=%u Lod=%u(%u)\n", This
,
550 nine_D3DPOOL_to_str(This
->base
.pool
),
551 nine_D3DRTYPE_to_str(This
->base
.type
),
552 nine_D3DUSAGE_to_str(This
->base
.usage
),
553 d3dformat_to_string(This
->format
),
554 This
->base
.info
.width0
, This
->base
.info
.height0
, This
->base
.info
.depth0
,
555 This
->base
.info
.array_size
, This
->base
.info
.last_level
,
556 This
->lod
, This
->lod_resident
);