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
;
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
));
83 list_inithead(&This
->list
);
89 NineBaseTexture9_dtor( struct NineBaseTexture9
*This
)
91 DBG("This=%p\n", This
);
93 pipe_sampler_view_reference(&This
->view
[0], NULL
);
94 pipe_sampler_view_reference(&This
->view
[1], NULL
);
96 if (This
->list
.prev
!= NULL
&& This
->list
.next
!= NULL
)
97 list_del(&This
->list
),
99 NineResource9_dtor(&This
->base
);
103 NineBaseTexture9_SetLOD( struct NineBaseTexture9
*This
,
106 DWORD old
= This
->managed
.lod
;
109 DBG("This=%p LODNew=%d\n", This
, LODNew
);
111 user_assert(This
->base
.pool
== D3DPOOL_MANAGED
, 0);
113 max_level
= (This
->base
.usage
& D3DUSAGE_AUTOGENMIPMAP
) ?
114 0 : This
->base
.info
.last_level
;
115 This
->managed
.lod
= MIN2(LODNew
, max_level
);
117 if (This
->managed
.lod
!= old
&& This
->bind_count
&& LIST_IS_EMPTY(&This
->list
))
118 list_add(&This
->list
, &This
->base
.base
.device
->update_textures
);
124 NineBaseTexture9_GetLOD( struct NineBaseTexture9
*This
)
126 DBG("This=%p\n", This
);
128 return This
->managed
.lod
;
132 NineBaseTexture9_GetLevelCount( struct NineBaseTexture9
*This
)
134 DBG("This=%p\n", This
);
136 if (This
->base
.usage
& D3DUSAGE_AUTOGENMIPMAP
)
138 return This
->base
.info
.last_level
+ 1;
142 NineBaseTexture9_SetAutoGenFilterType( struct NineBaseTexture9
*This
,
143 D3DTEXTUREFILTERTYPE FilterType
)
145 DBG("This=%p FilterType=%d\n", This
, FilterType
);
147 if (!(This
->base
.usage
& D3DUSAGE_AUTOGENMIPMAP
))
149 user_assert(FilterType
!= D3DTEXF_NONE
, D3DERR_INVALIDCALL
);
151 This
->mipfilter
= FilterType
;
156 D3DTEXTUREFILTERTYPE WINAPI
157 NineBaseTexture9_GetAutoGenFilterType( struct NineBaseTexture9
*This
)
159 DBG("This=%p\n", This
);
161 return This
->mipfilter
;
165 NineBaseTexture9_UploadSelf( struct NineBaseTexture9
*This
)
168 unsigned last_level
= This
->base
.info
.last_level
;
169 unsigned l
, min_level_dirty
= This
->managed
.lod
;
172 DBG("This=%p dirty=%i type=%s\n", This
, This
->managed
.dirty
,
173 nine_D3DRTYPE_to_str(This
->base
.type
));
175 assert(This
->base
.pool
== D3DPOOL_MANAGED
);
177 if (This
->base
.usage
& D3DUSAGE_AUTOGENMIPMAP
)
180 update_lod
= This
->managed
.lod_resident
!= This
->managed
.lod
;
181 if (!update_lod
&& !This
->managed
.dirty
)
184 /* Allocate a new resource with the correct number of levels,
185 * Mark states for update, and tell the nine surfaces/volumes
186 * their new resource. */
188 struct pipe_resource
*res
;
190 DBG("updating LOD from %u to %u ...\n", This
->managed
.lod_resident
, This
->managed
.lod
);
192 pipe_sampler_view_reference(&This
->view
[0], NULL
);
193 pipe_sampler_view_reference(&This
->view
[1], NULL
);
195 if (This
->bind_count
) {
196 /* mark state dirty */
197 struct nine_state
*state
= &This
->base
.base
.device
->state
;
199 for (s
= 0; s
< NINE_MAX_SAMPLERS
; ++s
)
200 if (state
->texture
[s
] == This
)
201 state
->changed
.texture
|= 1 << s
;
202 if (state
->changed
.texture
)
203 state
->changed
.group
|= NINE_STATE_TEXTURE
;
206 /* Allocate a new resource */
207 hr
= NineBaseTexture9_CreatePipeResource(This
, This
->managed
.lod_resident
!= -1);
210 res
= This
->base
.resource
;
212 if (This
->managed
.lod_resident
== -1) {/* no levels were resident */
213 This
->managed
.dirty
= FALSE
; /* We are going to upload everything. */
214 This
->managed
.lod_resident
= This
->base
.info
.last_level
+ 1;
217 if (This
->base
.type
== D3DRTYPE_TEXTURE
) {
218 struct NineTexture9
*tex
= NineTexture9(This
);
220 /* last content (if apply) has been copied to the new resource.
221 * Note: We cannot render to surfaces of managed textures.
222 * Note2: the level argument passed is to get the level offset
223 * right when the texture is uploaded (the texture first level
224 * corresponds to This->managed.lod).
225 * Note3: We don't care about the value passed for the surfaces
226 * before This->managed.lod, negative with this implementation. */
227 for (l
= 0; l
<= This
->base
.info
.last_level
; ++l
)
228 NineSurface9_SetResource(tex
->surfaces
[l
], res
, l
- This
->managed
.lod
);
230 if (This
->base
.type
== D3DRTYPE_CUBETEXTURE
) {
231 struct NineCubeTexture9
*tex
= NineCubeTexture9(This
);
234 for (l
= 0; l
<= This
->base
.info
.last_level
; ++l
) {
235 for (z
= 0; z
< 6; ++z
)
236 NineSurface9_SetResource(tex
->surfaces
[l
* 6 + z
],
237 res
, l
- This
->managed
.lod
);
240 if (This
->base
.type
== D3DRTYPE_VOLUMETEXTURE
) {
241 struct NineVolumeTexture9
*tex
= NineVolumeTexture9(This
);
243 for (l
= 0; l
<= This
->base
.info
.last_level
; ++l
)
244 NineVolume9_SetResource(tex
->volumes
[l
], res
, l
- This
->managed
.lod
);
246 assert(!"invalid texture type");
249 /* We are going to fully upload the new levels,
250 * no need to update dirty parts of the texture for these */
251 min_level_dirty
= MAX2(This
->managed
.lod
, This
->managed
.lod_resident
);
254 /* Update dirty parts of the texture */
255 if (This
->managed
.dirty
) {
256 if (This
->base
.type
== D3DRTYPE_TEXTURE
) {
257 struct NineTexture9
*tex
= NineTexture9(This
);
262 DBG("TEXTURE: dirty rect=(%u,%u) (%ux%u)\n",
263 tex
->dirty_rect
.x
, tex
->dirty_rect
.y
,
264 tex
->dirty_rect
.width
, tex
->dirty_rect
.height
);
266 /* Note: for l < min_level_dirty, the resource is
267 * either non-existing (and thus will be entirely re-uploaded
268 * if the lod changes) or going to have a full upload */
269 if (tex
->dirty_rect
.width
) {
270 for (l
= min_level_dirty
; l
<= last_level
; ++l
) {
271 u_box_minify_2d(&box
, &tex
->dirty_rect
, l
);
272 NineSurface9_UploadSelf(tex
->surfaces
[l
], &box
);
274 memset(&tex
->dirty_rect
, 0, sizeof(tex
->dirty_rect
));
275 tex
->dirty_rect
.depth
= 1;
278 if (This
->base
.type
== D3DRTYPE_CUBETEXTURE
) {
279 struct NineCubeTexture9
*tex
= NineCubeTexture9(This
);
285 for (z
= 0; z
< 6; ++z
) {
286 DBG("FACE[%u]: dirty rect=(%u,%u) (%ux%u)\n", z
,
287 tex
->dirty_rect
[z
].x
, tex
->dirty_rect
[z
].y
,
288 tex
->dirty_rect
[z
].width
, tex
->dirty_rect
[z
].height
);
290 if (tex
->dirty_rect
[z
].width
) {
291 for (l
= min_level_dirty
; l
<= last_level
; ++l
) {
292 u_box_minify_2d(&box
, &tex
->dirty_rect
[z
], l
);
293 NineSurface9_UploadSelf(tex
->surfaces
[l
* 6 + z
], &box
);
295 memset(&tex
->dirty_rect
[z
], 0, sizeof(tex
->dirty_rect
[z
]));
296 tex
->dirty_rect
[z
].depth
= 1;
300 if (This
->base
.type
== D3DRTYPE_VOLUMETEXTURE
) {
301 struct NineVolumeTexture9
*tex
= NineVolumeTexture9(This
);
304 DBG("VOLUME: dirty_box=(%u,%u,%u) (%ux%ux%u)\n",
305 tex
->dirty_box
.x
, tex
->dirty_box
.y
, tex
->dirty_box
.y
,
306 tex
->dirty_box
.width
, tex
->dirty_box
.height
, tex
->dirty_box
.depth
);
308 if (tex
->dirty_box
.width
) {
309 for (l
= 0; l
<= last_level
; ++l
) {
310 u_box_minify_2d(&box
, &tex
->dirty_box
, l
);
311 NineVolume9_AddDirtyRegion(tex
->volumes
[l
], &tex
->dirty_box
);
313 memset(&tex
->dirty_box
, 0, sizeof(tex
->dirty_box
));
315 for (l
= min_level_dirty
; l
<= last_level
; ++l
)
316 NineVolume9_UploadSelf(tex
->volumes
[l
]);
318 assert(!"invalid texture type");
320 This
->managed
.dirty
= FALSE
;
323 /* Upload the new levels */
325 if (This
->base
.type
== D3DRTYPE_TEXTURE
) {
326 struct NineTexture9
*tex
= NineTexture9(This
);
329 box
.x
= box
.y
= box
.z
= 0;
331 for (l
= This
->managed
.lod
; l
< This
->managed
.lod_resident
; ++l
) {
332 box
.width
= u_minify(This
->base
.info
.width0
, l
);
333 box
.height
= u_minify(This
->base
.info
.height0
, l
);
334 NineSurface9_UploadSelf(tex
->surfaces
[l
], &box
);
337 if (This
->base
.type
== D3DRTYPE_CUBETEXTURE
) {
338 struct NineCubeTexture9
*tex
= NineCubeTexture9(This
);
342 box
.x
= box
.y
= box
.z
= 0;
344 for (l
= This
->managed
.lod
; l
< This
->managed
.lod_resident
; ++l
) {
345 box
.width
= u_minify(This
->base
.info
.width0
, l
);
346 box
.height
= u_minify(This
->base
.info
.height0
, l
);
347 for (z
= 0; z
< 6; ++z
)
348 NineSurface9_UploadSelf(tex
->surfaces
[l
* 6 + z
], &box
);
351 if (This
->base
.type
== D3DRTYPE_VOLUMETEXTURE
) {
352 struct NineVolumeTexture9
*tex
= NineVolumeTexture9(This
);
355 box
.x
= box
.y
= box
.z
= 0;
356 for (l
= This
->managed
.lod
; l
< This
->managed
.lod_resident
; ++l
) {
357 box
.width
= u_minify(This
->base
.info
.width0
, l
);
358 box
.height
= u_minify(This
->base
.info
.height0
, l
);
359 box
.depth
= u_minify(This
->base
.info
.depth0
, l
);
360 NineVolume9_AddDirtyRegion(tex
->volumes
[l
], &box
);
361 NineVolume9_UploadSelf(tex
->volumes
[l
]);
364 assert(!"invalid texture type");
367 This
->managed
.lod_resident
= This
->managed
.lod
;
370 if (This
->base
.usage
& D3DUSAGE_AUTOGENMIPMAP
)
371 This
->dirty_mip
= TRUE
;
373 DBG("DONE, generate mip maps = %i\n", This
->dirty_mip
);
378 NineBaseTexture9_GenerateMipSubLevels( struct NineBaseTexture9
*This
)
380 struct pipe_resource
*resource
= This
->base
.resource
;
382 unsigned base_level
= 0;
383 unsigned last_level
= This
->base
.info
.last_level
- This
->managed
.lod
;
384 unsigned first_layer
= 0;
386 unsigned filter
= This
->mipfilter
== D3DTEXF_POINT
? PIPE_TEX_FILTER_NEAREST
387 : PIPE_TEX_FILTER_LINEAR
;
388 DBG("This=%p\n", This
);
390 if (This
->base
.pool
== D3DPOOL_MANAGED
)
391 NineBaseTexture9_UploadSelf(This
);
392 if (!This
->dirty_mip
)
394 if (This
->managed
.lod
) {
395 ERR("AUTOGENMIPMAP if level 0 is not resident not supported yet !\n");
400 NineBaseTexture9_UpdateSamplerView(This
, 0);
402 last_layer
= util_max_layer(This
->view
[0]->texture
, base_level
);
404 util_gen_mipmap(This
->pipe
, resource
,
405 resource
->format
, base_level
, last_level
,
406 first_layer
, last_layer
, filter
);
408 This
->dirty_mip
= FALSE
;
412 NineBaseTexture9_CreatePipeResource( struct NineBaseTexture9
*This
,
415 struct pipe_context
*pipe
= This
->pipe
;
416 struct pipe_screen
*screen
= This
->base
.info
.screen
;
417 struct pipe_resource templ
;
419 struct pipe_resource
*res
;
420 struct pipe_resource
*old
= This
->base
.resource
;
422 DBG("This=%p lod=%u last_level=%u\n", This
,
423 This
->managed
.lod
, This
->base
.info
.last_level
);
425 assert(This
->base
.pool
== D3DPOOL_MANAGED
);
427 templ
= This
->base
.info
;
429 if (This
->managed
.lod
) {
430 templ
.width0
= u_minify(templ
.width0
, This
->managed
.lod
);
431 templ
.height0
= u_minify(templ
.height0
, This
->managed
.lod
);
432 templ
.depth0
= u_minify(templ
.depth0
, This
->managed
.lod
);
434 templ
.last_level
= This
->base
.info
.last_level
- This
->managed
.lod
;
437 /* LOD might have changed. */
438 if (old
->width0
== templ
.width0
&&
439 old
->height0
== templ
.height0
&&
440 old
->depth0
== templ
.depth0
)
444 res
= screen
->resource_create(screen
, &templ
);
446 return D3DERR_OUTOFVIDEOMEMORY
;
447 This
->base
.resource
= res
;
449 if (old
&& CopyData
) { /* Don't return without releasing old ! */
455 l
= (This
->managed
.lod
< This
->managed
.lod_resident
) ? This
->managed
.lod_resident
- This
->managed
.lod
: 0;
456 m
= (This
->managed
.lod
< This
->managed
.lod_resident
) ? 0 : This
->managed
.lod
- This
->managed
.lod_resident
;
458 box
.width
= u_minify(templ
.width0
, l
);
459 box
.height
= u_minify(templ
.height0
, l
);
460 box
.depth
= u_minify(templ
.depth0
, l
);
462 for (; l
<= templ
.last_level
; ++l
, ++m
) {
463 pipe
->resource_copy_region(pipe
,
466 box
.width
= u_minify(box
.width
, 1);
467 box
.height
= u_minify(box
.height
, 1);
468 box
.depth
= u_minify(box
.depth
, 1);
471 pipe_resource_reference(&old
, NULL
);
476 #define SWIZZLE_TO_REPLACE(s) (s == UTIL_FORMAT_SWIZZLE_0 || \
477 s == UTIL_FORMAT_SWIZZLE_1 || \
478 s == UTIL_FORMAT_SWIZZLE_NONE)
481 NineBaseTexture9_UpdateSamplerView( struct NineBaseTexture9
*This
,
484 const struct util_format_description
*desc
;
485 struct pipe_context
*pipe
= This
->pipe
;
486 struct pipe_screen
*screen
= pipe
->screen
;
487 struct pipe_resource
*resource
= This
->base
.resource
;
488 struct pipe_sampler_view templ
;
489 enum pipe_format srgb_format
;
493 DBG("This=%p sRGB=%d\n", This
, sRGB
);
495 if (unlikely(!resource
)) {
496 if (unlikely(This
->format
== D3DFMT_NULL
))
498 NineBaseTexture9_Dump(This
);
499 /* hack due to incorrect POOL_MANAGED handling */
500 NineBaseTexture9_GenerateMipSubLevels(This
);
501 resource
= This
->base
.resource
;
505 pipe_sampler_view_reference(&This
->view
[sRGB
], NULL
);
507 swizzle
[0] = PIPE_SWIZZLE_RED
;
508 swizzle
[1] = PIPE_SWIZZLE_GREEN
;
509 swizzle
[2] = PIPE_SWIZZLE_BLUE
;
510 swizzle
[3] = PIPE_SWIZZLE_ALPHA
;
511 desc
= util_format_description(resource
->format
);
512 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) {
513 /* msdn doc is incomplete here and wrong.
514 * The only formats that can be read directly here
515 * are DF16, DF24 and INTZ.
516 * Tested on win the swizzle is
517 * R = depth, G = B = 0, A = 1 for DF16 and DF24
518 * R = G = B = A = depth for INTZ
519 * For the other ZS formats that can't be read directly
520 * but can be used as shadow map, the result is duplicated on
522 if (This
->format
== D3DFMT_DF16
||
523 This
->format
== D3DFMT_DF24
) {
524 swizzle
[1] = PIPE_SWIZZLE_ZERO
;
525 swizzle
[2] = PIPE_SWIZZLE_ZERO
;
526 swizzle
[3] = PIPE_SWIZZLE_ONE
;
528 swizzle
[1] = PIPE_SWIZZLE_RED
;
529 swizzle
[2] = PIPE_SWIZZLE_RED
;
530 swizzle
[3] = PIPE_SWIZZLE_RED
;
532 } else if (resource
->format
!= PIPE_FORMAT_A8_UNORM
&&
533 resource
->format
!= PIPE_FORMAT_RGTC1_UNORM
) {
535 * A8 should have 0.0 as default values for RGB.
536 * ATI1/RGTC1 should be r 0 0 1 (tested on windows).
537 * It is already what gallium does. All the other ones
538 * should have 1.0 for non-defined values */
539 for (i
= 0; i
< 4; i
++) {
540 if (SWIZZLE_TO_REPLACE(desc
->swizzle
[i
]))
541 swizzle
[i
] = PIPE_SWIZZLE_ONE
;
545 /* if requested and supported, convert to the sRGB format */
546 srgb_format
= util_format_srgb(resource
->format
);
547 if (sRGB
&& srgb_format
!= PIPE_FORMAT_NONE
&&
548 screen
->is_format_supported(screen
, srgb_format
,
549 resource
->target
, 0, resource
->bind
))
550 templ
.format
= srgb_format
;
552 templ
.format
= resource
->format
;
553 templ
.u
.tex
.first_layer
= 0;
554 templ
.u
.tex
.last_layer
= resource
->target
== PIPE_TEXTURE_3D
?
555 resource
->depth0
- 1 : resource
->array_size
- 1;
556 templ
.u
.tex
.first_level
= 0;
557 templ
.u
.tex
.last_level
= resource
->last_level
;
558 templ
.swizzle_r
= swizzle
[0];
559 templ
.swizzle_g
= swizzle
[1];
560 templ
.swizzle_b
= swizzle
[2];
561 templ
.swizzle_a
= swizzle
[3];
562 templ
.target
= resource
->target
;
564 This
->view
[sRGB
] = pipe
->create_sampler_view(pipe
, resource
, &templ
);
566 DBG("sampler view = %p(resource = %p)\n", This
->view
[sRGB
], resource
);
568 return This
->view
? D3D_OK
: D3DERR_DRIVERINTERNALERROR
;
572 NineBaseTexture9_PreLoad( struct NineBaseTexture9
*This
)
574 DBG("This=%p\n", This
);
576 if (This
->base
.pool
== D3DPOOL_MANAGED
)
577 NineBaseTexture9_UploadSelf(This
);
582 NineBaseTexture9_Dump( struct NineBaseTexture9
*This
)
584 DBG("\nNineBaseTexture9(%p->NULL/%p): Pool=%s Type=%s Usage=%s\n"
585 "Format=%s Dims=%ux%ux%u/%u LastLevel=%u Lod=%u(%u)\n", This
,
587 nine_D3DPOOL_to_str(This
->base
.pool
),
588 nine_D3DRTYPE_to_str(This
->base
.type
),
589 nine_D3DUSAGE_to_str(This
->base
.usage
),
590 d3dformat_to_string(This
->format
),
591 This
->base
.info
.width0
, This
->base
.info
.height0
, This
->base
.info
.depth0
,
592 This
->base
.info
.array_size
, This
->base
.info
.last_level
,
593 This
->managed
.lod
, This
->managed
.lod_resident
);