Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  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.     /* Mark the texture as dirty to trigger first upload when we need the texture,
  73.      * even if it wasn't set by the application */
  74.     if (Pool == D3DPOOL_MANAGED)
  75.         This->managed.dirty = TRUE;
  76.     /* When a depth buffer is sampled, it is for shadow mapping, except for
  77.      * D3DFMT_INTZ, D3DFMT_DF16 and D3DFMT_DF24.
  78.      * In addition D3DFMT_INTZ can be used for both texturing and depth buffering
  79.      * if z write is disabled. This particular feature may not work for us in
  80.      * practice because OGL doesn't have that. However apparently it is known
  81.      * some cards have performance issues with this feature, so real apps
  82.      * shouldn't use it. */
  83.     This->shadow = (This->format != D3DFMT_INTZ && This->format != D3DFMT_DF16 &&
  84.                     This->format != D3DFMT_DF24) &&
  85.                    util_format_has_depth(util_format_description(This->base.info.format));
  86.  
  87.     list_inithead(&This->list);
  88.  
  89.     return D3D_OK;
  90. }
  91.  
  92. void
  93. NineBaseTexture9_dtor( struct NineBaseTexture9 *This )
  94. {
  95.     DBG("This=%p\n", This);
  96.  
  97.     pipe_sampler_view_reference(&This->view[0], NULL);
  98.     pipe_sampler_view_reference(&This->view[1], NULL);
  99.  
  100.     if (This->list.prev != NULL && This->list.next != NULL)
  101.         list_del(&This->list),
  102.  
  103.     NineResource9_dtor(&This->base);
  104. }
  105.  
  106. DWORD WINAPI
  107. NineBaseTexture9_SetLOD( struct NineBaseTexture9 *This,
  108.                          DWORD LODNew )
  109. {
  110.     DWORD old = This->managed.lod;
  111.     DWORD max_level;
  112.  
  113.     DBG("This=%p LODNew=%d\n", This, LODNew);
  114.  
  115.     user_assert(This->base.pool == D3DPOOL_MANAGED, 0);
  116.  
  117.     max_level = (This->base.usage & D3DUSAGE_AUTOGENMIPMAP) ?
  118.                 0 : This->base.info.last_level;
  119.     This->managed.lod = MIN2(LODNew, max_level);
  120.  
  121.     if (This->managed.lod != old && This->bind_count && LIST_IS_EMPTY(&This->list))
  122.        list_add(&This->list, &This->base.base.device->update_textures);
  123.  
  124.     return old;
  125. }
  126.  
  127. DWORD WINAPI
  128. NineBaseTexture9_GetLOD( struct NineBaseTexture9 *This )
  129. {
  130.     DBG("This=%p\n", This);
  131.  
  132.     return This->managed.lod;
  133. }
  134.  
  135. DWORD WINAPI
  136. NineBaseTexture9_GetLevelCount( struct NineBaseTexture9 *This )
  137. {
  138.     DBG("This=%p\n", This);
  139.  
  140.     if (This->base.usage & D3DUSAGE_AUTOGENMIPMAP)
  141.         return 1;
  142.     return This->base.info.last_level + 1;
  143. }
  144.  
  145. HRESULT WINAPI
  146. NineBaseTexture9_SetAutoGenFilterType( struct NineBaseTexture9 *This,
  147.                                        D3DTEXTUREFILTERTYPE FilterType )
  148. {
  149.     DBG("This=%p FilterType=%d\n", This, FilterType);
  150.  
  151.     if (!(This->base.usage & D3DUSAGE_AUTOGENMIPMAP))
  152.         return D3D_OK;
  153.     user_assert(FilterType != D3DTEXF_NONE, D3DERR_INVALIDCALL);
  154.  
  155.     This->mipfilter = FilterType;
  156.  
  157.     return D3D_OK;
  158. }
  159.  
  160. D3DTEXTUREFILTERTYPE WINAPI
  161. NineBaseTexture9_GetAutoGenFilterType( struct NineBaseTexture9 *This )
  162. {
  163.     DBG("This=%p\n", This);
  164.  
  165.     return This->mipfilter;
  166. }
  167.  
  168. HRESULT
  169. NineBaseTexture9_UploadSelf( struct NineBaseTexture9 *This )
  170. {
  171.     HRESULT hr;
  172.     unsigned last_level = This->base.info.last_level;
  173.     unsigned l, min_level_dirty = This->managed.lod;
  174.     BOOL update_lod;
  175.  
  176.     DBG("This=%p dirty=%i type=%s\n", This, This->managed.dirty,
  177.         nine_D3DRTYPE_to_str(This->base.type));
  178.  
  179.     assert(This->base.pool == D3DPOOL_MANAGED);
  180.  
  181.     if (This->base.usage & D3DUSAGE_AUTOGENMIPMAP)
  182.         last_level = 0;
  183.  
  184.     update_lod = This->managed.lod_resident != This->managed.lod;
  185.     if (!update_lod && !This->managed.dirty)
  186.         return D3D_OK;
  187.  
  188.     /* Allocate a new resource with the correct number of levels,
  189.      * Mark states for update, and tell the nine surfaces/volumes
  190.      * their new resource. */
  191.     if (update_lod) {
  192.         struct pipe_resource *res;
  193.  
  194.         DBG("updating LOD from %u to %u ...\n", This->managed.lod_resident, This->managed.lod);
  195.  
  196.         pipe_sampler_view_reference(&This->view[0], NULL);
  197.         pipe_sampler_view_reference(&This->view[1], NULL);
  198.  
  199.         if (This->bind_count) {
  200.             /* mark state dirty */
  201.             struct nine_state *state = &This->base.base.device->state;
  202.             unsigned s;
  203.             for (s = 0; s < NINE_MAX_SAMPLERS; ++s)
  204.                 if (state->texture[s] == This)
  205.                     state->changed.texture |= 1 << s;
  206.             if (state->changed.texture)
  207.                 state->changed.group |= NINE_STATE_TEXTURE;
  208.         }
  209.  
  210.         /* Allocate a new resource */
  211.         hr = NineBaseTexture9_CreatePipeResource(This, This->managed.lod_resident != -1);
  212.         if (FAILED(hr))
  213.             return hr;
  214.         res = This->base.resource;
  215.  
  216.         if (This->managed.lod_resident == -1) {/* no levels were resident */
  217.             This->managed.dirty = FALSE; /* We are going to upload everything. */
  218.             This->managed.lod_resident = This->base.info.last_level + 1;
  219.         }
  220.  
  221.         if (This->base.type == D3DRTYPE_TEXTURE) {
  222.             struct NineTexture9 *tex = NineTexture9(This);
  223.  
  224.             /* last content (if apply) has been copied to the new resource.
  225.              * Note: We cannot render to surfaces of managed textures.
  226.              * Note2: the level argument passed is to get the level offset
  227.              * right when the texture is uploaded (the texture first level
  228.              * corresponds to This->managed.lod).
  229.              * Note3: We don't care about the value passed for the surfaces
  230.              * before This->managed.lod, negative with this implementation. */
  231.             for (l = 0; l <= This->base.info.last_level; ++l)
  232.                 NineSurface9_SetResource(tex->surfaces[l], res, l - This->managed.lod);
  233.         } else
  234.         if (This->base.type == D3DRTYPE_CUBETEXTURE) {
  235.             struct NineCubeTexture9 *tex = NineCubeTexture9(This);
  236.             unsigned z;
  237.  
  238.             for (l = 0; l <= This->base.info.last_level; ++l) {
  239.                 for (z = 0; z < 6; ++z)
  240.                     NineSurface9_SetResource(tex->surfaces[l * 6 + z],
  241.                                              res, l - This->managed.lod);
  242.             }
  243.         } else
  244.         if (This->base.type == D3DRTYPE_VOLUMETEXTURE) {
  245.             struct NineVolumeTexture9 *tex = NineVolumeTexture9(This);
  246.  
  247.             for (l = 0; l <= This->base.info.last_level; ++l)
  248.                 NineVolume9_SetResource(tex->volumes[l], res, l - This->managed.lod);
  249.         } else {
  250.             assert(!"invalid texture type");
  251.         }
  252.  
  253.         /* We are going to fully upload the new levels,
  254.          * no need to update dirty parts of the texture for these */
  255.         min_level_dirty = MAX2(This->managed.lod, This->managed.lod_resident);
  256.     }
  257.  
  258.     /* Update dirty parts of the texture */
  259.     if (This->managed.dirty) {
  260.         if (This->base.type == D3DRTYPE_TEXTURE) {
  261.             struct NineTexture9 *tex = NineTexture9(This);
  262.             struct pipe_box box;
  263.             box.z = 0;
  264.             box.depth = 1;
  265.  
  266.             DBG("TEXTURE: dirty rect=(%u,%u) (%ux%u)\n",
  267.                 tex->dirty_rect.x, tex->dirty_rect.y,
  268.                 tex->dirty_rect.width, tex->dirty_rect.height);
  269.  
  270.             /* Note: for l < min_level_dirty, the resource is
  271.              * either non-existing (and thus will be entirely re-uploaded
  272.              * if the lod changes) or going to have a full upload */
  273.             if (tex->dirty_rect.width) {
  274.                 for (l = min_level_dirty; l <= last_level; ++l) {
  275.                     u_box_minify_2d(&box, &tex->dirty_rect, l);
  276.                     NineSurface9_UploadSelf(tex->surfaces[l], &box);
  277.                 }
  278.                 memset(&tex->dirty_rect, 0, sizeof(tex->dirty_rect));
  279.                 tex->dirty_rect.depth = 1;
  280.             }
  281.         } else
  282.         if (This->base.type == D3DRTYPE_CUBETEXTURE) {
  283.             struct NineCubeTexture9 *tex = NineCubeTexture9(This);
  284.             unsigned z;
  285.             struct pipe_box box;
  286.             box.z = 0;
  287.             box.depth = 1;
  288.  
  289.             for (z = 0; z < 6; ++z) {
  290.                 DBG("FACE[%u]: dirty rect=(%u,%u) (%ux%u)\n", z,
  291.                     tex->dirty_rect[z].x, tex->dirty_rect[z].y,
  292.                     tex->dirty_rect[z].width, tex->dirty_rect[z].height);
  293.  
  294.                 if (tex->dirty_rect[z].width) {
  295.                     for (l = min_level_dirty; l <= last_level; ++l) {
  296.                         u_box_minify_2d(&box, &tex->dirty_rect[z], l);
  297.                         NineSurface9_UploadSelf(tex->surfaces[l * 6 + z], &box);
  298.                     }
  299.                     memset(&tex->dirty_rect[z], 0, sizeof(tex->dirty_rect[z]));
  300.                     tex->dirty_rect[z].depth = 1;
  301.                 }
  302.             }
  303.         } else
  304.         if (This->base.type == D3DRTYPE_VOLUMETEXTURE) {
  305.             struct NineVolumeTexture9 *tex = NineVolumeTexture9(This);
  306.             struct pipe_box box;
  307.  
  308.             DBG("VOLUME: dirty_box=(%u,%u,%u) (%ux%ux%u)\n",
  309.                 tex->dirty_box.x, tex->dirty_box.y, tex->dirty_box.y,
  310.                 tex->dirty_box.width, tex->dirty_box.height, tex->dirty_box.depth);
  311.  
  312.             if (tex->dirty_box.width) {
  313.                 for (l = 0; l <= last_level; ++l) {
  314.                     u_box_minify_2d(&box, &tex->dirty_box, l);
  315.                     NineVolume9_AddDirtyRegion(tex->volumes[l], &tex->dirty_box);
  316.                 }
  317.                 memset(&tex->dirty_box, 0, sizeof(tex->dirty_box));
  318.             }
  319.             for (l = min_level_dirty; l <= last_level; ++l)
  320.                 NineVolume9_UploadSelf(tex->volumes[l]);
  321.         } else {
  322.             assert(!"invalid texture type");
  323.         }
  324.         This->managed.dirty = FALSE;
  325.     }
  326.  
  327.     /* Upload the new levels */
  328.     if (update_lod) {
  329.         if (This->base.type == D3DRTYPE_TEXTURE) {
  330.             struct NineTexture9 *tex = NineTexture9(This);
  331.             struct pipe_box box;
  332.  
  333.             box.x = box.y = box.z = 0;
  334.             box.depth = 1;
  335.             for (l = This->managed.lod; l < This->managed.lod_resident; ++l) {
  336.                 box.width = u_minify(This->base.info.width0, l);
  337.                 box.height = u_minify(This->base.info.height0, l);
  338.                 NineSurface9_UploadSelf(tex->surfaces[l], &box);
  339.             }
  340.         } else
  341.         if (This->base.type == D3DRTYPE_CUBETEXTURE) {
  342.             struct NineCubeTexture9 *tex = NineCubeTexture9(This);
  343.             struct pipe_box box;
  344.             unsigned z;
  345.  
  346.             box.x = box.y = box.z = 0;
  347.             box.depth = 1;
  348.             for (l = This->managed.lod; l < This->managed.lod_resident; ++l) {
  349.                 box.width = u_minify(This->base.info.width0, l);
  350.                 box.height = u_minify(This->base.info.height0, l);
  351.                 for (z = 0; z < 6; ++z)
  352.                     NineSurface9_UploadSelf(tex->surfaces[l * 6 + z], &box);
  353.             }
  354.         } else
  355.         if (This->base.type == D3DRTYPE_VOLUMETEXTURE) {
  356.             struct NineVolumeTexture9 *tex = NineVolumeTexture9(This);
  357.             struct pipe_box box;
  358.  
  359.             box.x = box.y = box.z = 0;
  360.             for (l = This->managed.lod; l < This->managed.lod_resident; ++l) {
  361.                 box.width = u_minify(This->base.info.width0, l);
  362.                 box.height = u_minify(This->base.info.height0, l);
  363.                 box.depth = u_minify(This->base.info.depth0, l);
  364.                 NineVolume9_AddDirtyRegion(tex->volumes[l], &box);
  365.                 NineVolume9_UploadSelf(tex->volumes[l]);
  366.             }
  367.         } else {
  368.             assert(!"invalid texture type");
  369.         }
  370.  
  371.         This->managed.lod_resident = This->managed.lod;
  372.     }
  373.  
  374.     if (This->base.usage & D3DUSAGE_AUTOGENMIPMAP)
  375.         This->dirty_mip = TRUE;
  376.  
  377.     DBG("DONE, generate mip maps = %i\n", This->dirty_mip);
  378.     return D3D_OK;
  379. }
  380.  
  381. void WINAPI
  382. NineBaseTexture9_GenerateMipSubLevels( struct NineBaseTexture9 *This )
  383. {
  384.     struct pipe_resource *resource = This->base.resource;
  385.  
  386.     unsigned base_level = 0;
  387.     unsigned last_level = This->base.info.last_level - This->managed.lod;
  388.     unsigned first_layer = 0;
  389.     unsigned last_layer;
  390.     unsigned filter = This->mipfilter == D3DTEXF_POINT ? PIPE_TEX_FILTER_NEAREST
  391.                                                        : PIPE_TEX_FILTER_LINEAR;
  392.     DBG("This=%p\n", This);
  393.  
  394.     if (This->base.pool == D3DPOOL_MANAGED)
  395.         NineBaseTexture9_UploadSelf(This);
  396.     if (!This->dirty_mip)
  397.         return;
  398.     if (This->managed.lod) {
  399.         ERR("AUTOGENMIPMAP if level 0 is not resident not supported yet !\n");
  400.         return;
  401.     }
  402.  
  403.     if (!This->view[0])
  404.         NineBaseTexture9_UpdateSamplerView(This, 0);
  405.  
  406.     last_layer = util_max_layer(This->view[0]->texture, base_level);
  407.  
  408.     util_gen_mipmap(This->pipe, resource,
  409.                     resource->format, base_level, last_level,
  410.                     first_layer, last_layer, filter);
  411.  
  412.     This->dirty_mip = FALSE;
  413. }
  414.  
  415. HRESULT
  416. NineBaseTexture9_CreatePipeResource( struct NineBaseTexture9 *This,
  417.                                      BOOL CopyData )
  418. {
  419.     struct pipe_context *pipe = This->pipe;
  420.     struct pipe_screen *screen = This->base.info.screen;
  421.     struct pipe_resource templ;
  422.     unsigned l, m;
  423.     struct pipe_resource *res;
  424.     struct pipe_resource *old = This->base.resource;
  425.  
  426.     DBG("This=%p lod=%u last_level=%u\n", This,
  427.         This->managed.lod, This->base.info.last_level);
  428.  
  429.     assert(This->base.pool == D3DPOOL_MANAGED);
  430.  
  431.     templ = This->base.info;
  432.  
  433.     if (This->managed.lod) {
  434.         templ.width0 = u_minify(templ.width0, This->managed.lod);
  435.         templ.height0 = u_minify(templ.height0, This->managed.lod);
  436.         templ.depth0 = u_minify(templ.depth0, This->managed.lod);
  437.     }
  438.     templ.last_level = This->base.info.last_level - This->managed.lod;
  439.  
  440.     if (old) {
  441.         /* LOD might have changed. */
  442.         if (old->width0 == templ.width0 &&
  443.             old->height0 == templ.height0 &&
  444.             old->depth0 == templ.depth0)
  445.             return D3D_OK;
  446.     }
  447.  
  448.     res = screen->resource_create(screen, &templ);
  449.     if (!res)
  450.         return D3DERR_OUTOFVIDEOMEMORY;
  451.     This->base.resource = res;
  452.  
  453.     if (old && CopyData) { /* Don't return without releasing old ! */
  454.         struct pipe_box box;
  455.         box.x = 0;
  456.         box.y = 0;
  457.         box.z = 0;
  458.  
  459.         l = (This->managed.lod < This->managed.lod_resident) ? This->managed.lod_resident - This->managed.lod : 0;
  460.         m = (This->managed.lod < This->managed.lod_resident) ? 0 : This->managed.lod - This->managed.lod_resident;
  461.  
  462.         box.width = u_minify(templ.width0, l);
  463.         box.height = u_minify(templ.height0, l);
  464.         box.depth = u_minify(templ.depth0, l);
  465.  
  466.         for (; l <= templ.last_level; ++l, ++m) {
  467.             pipe->resource_copy_region(pipe,
  468.                                        res, l, 0, 0, 0,
  469.                                        old, m, &box);
  470.             box.width = u_minify(box.width, 1);
  471.             box.height = u_minify(box.height, 1);
  472.             box.depth = u_minify(box.depth, 1);
  473.         }
  474.     }
  475.     pipe_resource_reference(&old, NULL);
  476.  
  477.     return D3D_OK;
  478. }
  479.  
  480. #define SWIZZLE_TO_REPLACE(s) (s == UTIL_FORMAT_SWIZZLE_0 || \
  481.                                s == UTIL_FORMAT_SWIZZLE_1 || \
  482.                                s == UTIL_FORMAT_SWIZZLE_NONE)
  483.  
  484. HRESULT
  485. NineBaseTexture9_UpdateSamplerView( struct NineBaseTexture9 *This,
  486.                                     const int sRGB )
  487. {
  488.     const struct util_format_description *desc;
  489.     struct pipe_context *pipe = This->pipe;
  490.     struct pipe_screen *screen = pipe->screen;
  491.     struct pipe_resource *resource = This->base.resource;
  492.     struct pipe_sampler_view templ;
  493.     enum pipe_format srgb_format;
  494.     unsigned i;
  495.     uint8_t swizzle[4];
  496.  
  497.     DBG("This=%p sRGB=%d\n", This, sRGB);
  498.  
  499.     if (unlikely(!resource)) {
  500.         if (unlikely(This->format == D3DFMT_NULL))
  501.             return D3D_OK;
  502.         NineBaseTexture9_Dump(This);
  503.     }
  504.     assert(resource);
  505.  
  506.     pipe_sampler_view_reference(&This->view[sRGB], NULL);
  507.  
  508.     swizzle[0] = PIPE_SWIZZLE_RED;
  509.     swizzle[1] = PIPE_SWIZZLE_GREEN;
  510.     swizzle[2] = PIPE_SWIZZLE_BLUE;
  511.     swizzle[3] = PIPE_SWIZZLE_ALPHA;
  512.     desc = util_format_description(resource->format);
  513.     if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
  514.         /* msdn doc is incomplete here and wrong.
  515.          * The only formats that can be read directly here
  516.          * are DF16, DF24 and INTZ.
  517.          * Tested on win the swizzle is
  518.          * R = depth, G = B = 0, A = 1 for DF16 and DF24
  519.          * R = G = B = A = depth for INTZ
  520.          * For the other ZS formats that can't be read directly
  521.          * but can be used as shadow map, the result is duplicated on
  522.          * all channel */
  523.         if (This->format == D3DFMT_DF16 ||
  524.             This->format == D3DFMT_DF24) {
  525.             swizzle[1] = PIPE_SWIZZLE_ZERO;
  526.             swizzle[2] = PIPE_SWIZZLE_ZERO;
  527.             swizzle[3] = PIPE_SWIZZLE_ONE;
  528.         } else {
  529.             swizzle[1] = PIPE_SWIZZLE_RED;
  530.             swizzle[2] = PIPE_SWIZZLE_RED;
  531.             swizzle[3] = PIPE_SWIZZLE_RED;
  532.         }
  533.     } else if (resource->format != PIPE_FORMAT_A8_UNORM &&
  534.                resource->format != PIPE_FORMAT_RGTC1_UNORM) {
  535.         /* exceptions:
  536.          * A8 should have 0.0 as default values for RGB.
  537.          * ATI1/RGTC1 should be r 0 0 1 (tested on windows).
  538.          * It is already what gallium does. All the other ones
  539.          * should have 1.0 for non-defined values */
  540.         for (i = 0; i < 4; i++) {
  541.             if (SWIZZLE_TO_REPLACE(desc->swizzle[i]))
  542.                 swizzle[i] = PIPE_SWIZZLE_ONE;
  543.         }
  544.     }
  545.  
  546.     /* if requested and supported, convert to the sRGB format */
  547.     srgb_format = util_format_srgb(resource->format);
  548.     if (sRGB && srgb_format != PIPE_FORMAT_NONE &&
  549.         screen->is_format_supported(screen, srgb_format,
  550.                                     resource->target, 0, resource->bind))
  551.         templ.format = srgb_format;
  552.     else
  553.         templ.format = resource->format;
  554.     templ.u.tex.first_layer = 0;
  555.     templ.u.tex.last_layer = resource->target == PIPE_TEXTURE_3D ?
  556.                              resource->depth0 - 1 : resource->array_size - 1;
  557.     templ.u.tex.first_level = 0;
  558.     templ.u.tex.last_level = resource->last_level;
  559.     templ.swizzle_r = swizzle[0];
  560.     templ.swizzle_g = swizzle[1];
  561.     templ.swizzle_b = swizzle[2];
  562.     templ.swizzle_a = swizzle[3];
  563.     templ.target = resource->target;
  564.  
  565.     This->view[sRGB] = pipe->create_sampler_view(pipe, resource, &templ);
  566.  
  567.     DBG("sampler view = %p(resource = %p)\n", This->view[sRGB], resource);
  568.  
  569.     return This->view ? D3D_OK : D3DERR_DRIVERINTERNALERROR;
  570. }
  571.  
  572. void WINAPI
  573. NineBaseTexture9_PreLoad( struct NineBaseTexture9 *This )
  574. {
  575.     DBG("This=%p\n", This);
  576.  
  577.     if (This->base.pool == D3DPOOL_MANAGED)
  578.         NineBaseTexture9_UploadSelf(This);
  579. }
  580.  
  581. #ifdef DEBUG
  582. void
  583. NineBaseTexture9_Dump( struct NineBaseTexture9 *This )
  584. {
  585.     DBG("\nNineBaseTexture9(%p->NULL/%p): Pool=%s Type=%s Usage=%s\n"
  586.         "Format=%s Dims=%ux%ux%u/%u LastLevel=%u Lod=%u(%u)\n", This,
  587.         This->base.resource,
  588.         nine_D3DPOOL_to_str(This->base.pool),
  589.         nine_D3DRTYPE_to_str(This->base.type),
  590.         nine_D3DUSAGE_to_str(This->base.usage),
  591.         d3dformat_to_string(This->format),
  592.         This->base.info.width0, This->base.info.height0, This->base.info.depth0,
  593.         This->base.info.array_size, This->base.info.last_level,
  594.         This->managed.lod, This->managed.lod_resident);
  595. }
  596. #endif /* DEBUG */
  597.