1857a3294a959015bf893241199f7fd7f7882a6abBen Skeggs/*
2857a3294a959015bf893241199f7fd7f7882a6abBen Skeggs * Copyright 2008 Ben Skeggs
3857a3294a959015bf893241199f7fd7f7882a6abBen Skeggs *
4857a3294a959015bf893241199f7fd7f7882a6abBen Skeggs * Permission is hereby granted, free of charge, to any person obtaining a
5857a3294a959015bf893241199f7fd7f7882a6abBen Skeggs * copy of this software and associated documentation files (the "Software"),
6857a3294a959015bf893241199f7fd7f7882a6abBen Skeggs * to deal in the Software without restriction, including without limitation
7857a3294a959015bf893241199f7fd7f7882a6abBen Skeggs * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8857a3294a959015bf893241199f7fd7f7882a6abBen Skeggs * and/or sell copies of the Software, and to permit persons to whom the
9857a3294a959015bf893241199f7fd7f7882a6abBen Skeggs * Software is furnished to do so, subject to the following conditions:
10857a3294a959015bf893241199f7fd7f7882a6abBen Skeggs *
11857a3294a959015bf893241199f7fd7f7882a6abBen Skeggs * The above copyright notice and this permission notice shall be included in
12857a3294a959015bf893241199f7fd7f7882a6abBen Skeggs * all copies or substantial portions of the Software.
13857a3294a959015bf893241199f7fd7f7882a6abBen Skeggs *
14857a3294a959015bf893241199f7fd7f7882a6abBen Skeggs * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15857a3294a959015bf893241199f7fd7f7882a6abBen Skeggs * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16857a3294a959015bf893241199f7fd7f7882a6abBen Skeggs * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
173d8d5b298a268b119d840bc9bae0ee9e0c9244a9Kenneth Graunke * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
183d8d5b298a268b119d840bc9bae0ee9e0c9244a9Kenneth Graunke * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
193d8d5b298a268b119d840bc9bae0ee9e0c9244a9Kenneth Graunke * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
203d8d5b298a268b119d840bc9bae0ee9e0c9244a9Kenneth Graunke * OTHER DEALINGS IN THE SOFTWARE.
21857a3294a959015bf893241199f7fd7f7882a6abBen Skeggs */
22857a3294a959015bf893241199f7fd7f7882a6abBen Skeggs
2347b418b8fa5fd242e9021503d6ec329ac3d56fb0Ben Skeggs#include "pipe/p_state.h"
2447b418b8fa5fd242e9021503d6ec329ac3d56fb0Ben Skeggs#include "pipe/p_defines.h"
2528486880ca3ec39419ccee0cb1a3bedc9ef7117cJosé Fonseca#include "util/u_inlines.h"
26b1ed72ebe2599ec178f51d86fd42f26486b9a19bMichal Krol#include "util/u_format.h"
2747b418b8fa5fd242e9021503d6ec329ac3d56fb0Ben Skeggs
285eb7ff1175a644ffe3b0f1a75cb235400355f9fbJohannes Obermayr#include "nv50/nv50_context.h"
295eb7ff1175a644ffe3b0f1a75cb235400355f9fbJohannes Obermayr#include "nv50/nv50_resource.h"
3047b418b8fa5fd242e9021503d6ec329ac3d56fb0Ben Skeggs
316bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayruint32_t
32ae720c66cb91c2640dfd6707446899694a24ab5bIlia Mirkinnv50_tex_choose_tile_dims_helper(unsigned nx, unsigned ny, unsigned nz,
33cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset                                 bool is_3d)
346bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr{
356bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr   uint32_t tile_mode = 0x000;
366bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr
376bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr   if (ny > 64) tile_mode = 0x040; /* height 128 tiles */
386bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr   else
396bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr   if (ny > 32) tile_mode = 0x030; /* height 64 tiles */
406bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr   else
416bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr   if (ny > 16) tile_mode = 0x020; /* height 32 tiles */
426bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr   else
436bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr   if (ny >  8) tile_mode = 0x010; /* height 16 tiles */
446bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr
45ae720c66cb91c2640dfd6707446899694a24ab5bIlia Mirkin   if (!is_3d)
466bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr      return tile_mode;
476bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr   else
486bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr      if (tile_mode > 0x020)
496bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr         tile_mode = 0x020;
506bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr
516bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr   if (nz > 16 && tile_mode < 0x020)
526bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr      return tile_mode | 0x500; /* depth 32 tiles */
536bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr   if (nz > 8) return tile_mode | 0x400; /* depth 16 tiles */
546bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr   if (nz > 4) return tile_mode | 0x300; /* depth 8 tiles */
556bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr   if (nz > 2) return tile_mode | 0x200; /* depth 4 tiles */
56ae720c66cb91c2640dfd6707446899694a24ab5bIlia Mirkin   if (nz > 1) return tile_mode | 0x100; /* depth 2 tiles */
576bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr
58ae720c66cb91c2640dfd6707446899694a24ab5bIlia Mirkin   return tile_mode;
596bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr}
606bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayr
616bca283ad5ebdd85e268c6757842b3c808c6b73dJohannes Obermayrstatic uint32_t
62cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoisetnv50_tex_choose_tile_dims(unsigned nx, unsigned ny, unsigned nz, bool is_3d)
631cc16e1b831cef8e1573cc998cee3e55179bb830Christoph Bumiller{
64ae720c66cb91c2640dfd6707446899694a24ab5bIlia Mirkin   return nv50_tex_choose_tile_dims_helper(nx, ny * 2, nz, is_3d);
651cc16e1b831cef8e1573cc998cee3e55179bb830Christoph Bumiller}
661cc16e1b831cef8e1573cc998cee3e55179bb830Christoph Bumiller
67cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumillerstatic uint32_t
68cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoisetnv50_mt_choose_storage_type(struct nv50_miptree *mt, bool compressed)
69079b670111fe41cabf700d089f489d4b116af5ebChristoph Bumiller{
701f8c0be27e1aa09e39d3016b6aae96c3ac228e17Tobias Klausmann   const unsigned ms = util_logbase2(mt->base.base.nr_samples);
71cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   uint32_t tile_flags;
72cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
736d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   if (unlikely(mt->base.base.flags & NOUVEAU_RESOURCE_FLAG_LINEAR))
746d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      return 0;
756d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   if (unlikely(mt->base.base.bind & PIPE_BIND_CURSOR))
766d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      return 0;
77079b670111fe41cabf700d089f489d4b116af5ebChristoph Bumiller
78cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   switch (mt->base.base.format) {
79cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   case PIPE_FORMAT_Z16_UNORM:
80acd66ec03346d37c593b5ba13e4c94ae11eef50dChristoph Bumiller      tile_flags = 0x6c + ms;
81cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      break;
82b49dfb68edd6b9c9f8fb45db3e5acb07fe87644dIlia Mirkin   case PIPE_FORMAT_X8Z24_UNORM:
83b49dfb68edd6b9c9f8fb45db3e5acb07fe87644dIlia Mirkin   case PIPE_FORMAT_S8X24_UINT:
84866f9b18c68ede63c00917ec9c3dae3524ca8826Dave Airlie   case PIPE_FORMAT_S8_UINT_Z24_UNORM:
85acd66ec03346d37c593b5ba13e4c94ae11eef50dChristoph Bumiller      tile_flags = 0x18 + ms;
86cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      break;
87b49dfb68edd6b9c9f8fb45db3e5acb07fe87644dIlia Mirkin   case PIPE_FORMAT_X24S8_UINT:
88cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   case PIPE_FORMAT_Z24X8_UNORM:
89866f9b18c68ede63c00917ec9c3dae3524ca8826Dave Airlie   case PIPE_FORMAT_Z24_UNORM_S8_UINT:
90acd66ec03346d37c593b5ba13e4c94ae11eef50dChristoph Bumiller      tile_flags = 0x128 + ms;
91cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      break;
92c011f94b7b4e1e93d5563f4bfd9906fa29e2ffb0Christoph Bumiller   case PIPE_FORMAT_Z32_FLOAT:
93acd66ec03346d37c593b5ba13e4c94ae11eef50dChristoph Bumiller      tile_flags = 0x40 + ms;
94c011f94b7b4e1e93d5563f4bfd9906fa29e2ffb0Christoph Bumiller      break;
95b49dfb68edd6b9c9f8fb45db3e5acb07fe87644dIlia Mirkin   case PIPE_FORMAT_X32_S8X24_UINT:
96866f9b18c68ede63c00917ec9c3dae3524ca8826Dave Airlie   case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
97acd66ec03346d37c593b5ba13e4c94ae11eef50dChristoph Bumiller      tile_flags = 0x60 + ms;
98cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      break;
99cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   default:
1001f8c0be27e1aa09e39d3016b6aae96c3ac228e17Tobias Klausmann      /* Most color formats don't work with compression. */
1011f8c0be27e1aa09e39d3016b6aae96c3ac228e17Tobias Klausmann      compressed = false;
1021f8c0be27e1aa09e39d3016b6aae96c3ac228e17Tobias Klausmann      /* fallthrough */
1031f8c0be27e1aa09e39d3016b6aae96c3ac228e17Tobias Klausmann   case PIPE_FORMAT_R8G8B8A8_UNORM:
1041f8c0be27e1aa09e39d3016b6aae96c3ac228e17Tobias Klausmann   case PIPE_FORMAT_R8G8B8A8_SRGB:
1051f8c0be27e1aa09e39d3016b6aae96c3ac228e17Tobias Klausmann   case PIPE_FORMAT_R8G8B8X8_UNORM:
1061f8c0be27e1aa09e39d3016b6aae96c3ac228e17Tobias Klausmann   case PIPE_FORMAT_R8G8B8X8_SRGB:
1071f8c0be27e1aa09e39d3016b6aae96c3ac228e17Tobias Klausmann   case PIPE_FORMAT_B8G8R8A8_UNORM:
1081f8c0be27e1aa09e39d3016b6aae96c3ac228e17Tobias Klausmann   case PIPE_FORMAT_B8G8R8A8_SRGB:
1091f8c0be27e1aa09e39d3016b6aae96c3ac228e17Tobias Klausmann   case PIPE_FORMAT_B8G8R8X8_UNORM:
1101f8c0be27e1aa09e39d3016b6aae96c3ac228e17Tobias Klausmann   case PIPE_FORMAT_B8G8R8X8_SRGB:
1111f8c0be27e1aa09e39d3016b6aae96c3ac228e17Tobias Klausmann   case PIPE_FORMAT_R10G10B10A2_UNORM:
1121f8c0be27e1aa09e39d3016b6aae96c3ac228e17Tobias Klausmann   case PIPE_FORMAT_B10G10R10A2_UNORM:
1131f8c0be27e1aa09e39d3016b6aae96c3ac228e17Tobias Klausmann   case PIPE_FORMAT_R16G16B16A16_FLOAT:
1141f8c0be27e1aa09e39d3016b6aae96c3ac228e17Tobias Klausmann   case PIPE_FORMAT_R16G16B16X16_FLOAT:
1151f8c0be27e1aa09e39d3016b6aae96c3ac228e17Tobias Klausmann   case PIPE_FORMAT_R11G11B10_FLOAT:
116cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      switch (util_format_get_blocksizebits(mt->base.base.format)) {
117cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      case 128:
118cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller         assert(ms < 3);
1196d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         tile_flags = 0x74;
120cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller         break;
121cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      case 64:
122cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller         switch (ms) {
1236d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         case 2: tile_flags = 0xfc; break;
1246d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         case 3: tile_flags = 0xfd; break;
125cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller         default:
1266d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller            tile_flags = 0x70;
127cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller            break;
128cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller         }
129cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller         break;
130cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      case 32:
131cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller         if (mt->base.base.bind & PIPE_BIND_SCANOUT) {
132cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller            assert(ms == 0);
1336d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller            tile_flags = 0x7a;
134cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller         } else {
135cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller            switch (ms) {
1366d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller            case 2: tile_flags = 0xf8; break;
1376d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller            case 3: tile_flags = 0xf9; break;
138cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller            default:
1396d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller               tile_flags = 0x70;
140cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller               break;
141cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller            }
142cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller         }
143cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller         break;
144cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      case 16:
145cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      case 8:
1466d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller         tile_flags = 0x70;
147cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller         break;
148cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      default:
149cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller         return 0;
150cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      }
151cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      if (mt->base.base.bind & PIPE_BIND_CURSOR)
152cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller         tile_flags = 0;
153cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   }
154cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
155cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   if (!compressed)
1566d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      tile_flags &= ~0x180;
1570a3f0ff2645d03d5f572cc26932595b8df400505Christoph Bumiller
158cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   return tile_flags;
159079b670111fe41cabf700d089f489d4b116af5ebChristoph Bumiller}
160079b670111fe41cabf700d089f489d4b116af5ebChristoph Bumiller
161cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumillervoid
162f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumillernv50_miptree_destroy(struct pipe_screen *pscreen, struct pipe_resource *pt)
163287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell{
164f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   struct nv50_miptree *mt = nv50_miptree(pt);
165287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
1663a6b9a7830c3df14ffcfbbf57c82ea08bd59ef04Ilia Mirkin   if (mt->base.fence && mt->base.fence->state < NOUVEAU_FENCE_STATE_FLUSHED)
1673a6b9a7830c3df14ffcfbbf57c82ea08bd59ef04Ilia Mirkin      nouveau_fence_work(mt->base.fence, nouveau_fence_unref_bo, mt->base.bo);
1683a6b9a7830c3df14ffcfbbf57c82ea08bd59ef04Ilia Mirkin   else
1693a6b9a7830c3df14ffcfbbf57c82ea08bd59ef04Ilia Mirkin      nouveau_bo_ref(NULL, &mt->base.bo);
170287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
17194a25b216bb7bf1f55e657d93d84f6de3f5e7ed4Christoph Bumiller   nouveau_fence_ref(NULL, &mt->base.fence);
17294a25b216bb7bf1f55e657d93d84f6de3f5e7ed4Christoph Bumiller   nouveau_fence_ref(NULL, &mt->base.fence_wr);
17394a25b216bb7bf1f55e657d93d84f6de3f5e7ed4Christoph Bumiller
174198f514aa6f08bc43a3002519843b0fe94f340bdChristoph Bumiller   NOUVEAU_DRV_STAT(nouveau_screen(pscreen), tex_obj_current_count, -1);
175198f514aa6f08bc43a3002519843b0fe94f340bdChristoph Bumiller   NOUVEAU_DRV_STAT(nouveau_screen(pscreen), tex_obj_current_bytes,
176198f514aa6f08bc43a3002519843b0fe94f340bdChristoph Bumiller                    -(uint64_t)mt->total_size);
177198f514aa6f08bc43a3002519843b0fe94f340bdChristoph Bumiller
178f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   FREE(mt);
179287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell}
180287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
181cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumillerboolean
182287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwellnv50_miptree_get_handle(struct pipe_screen *pscreen,
183f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller                        struct pipe_resource *pt,
184f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller                        struct winsys_handle *whandle)
185287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell{
186f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   struct nv50_miptree *mt = nv50_miptree(pt);
187f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   unsigned stride;
188287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
189f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   if (!mt || !mt->base.bo)
190cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset      return false;
191287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
19293eba269351c6e256db3a4cc7c7018f5a3fae5a1Marcin Ślusarz   stride = mt->level[0].pitch;
193287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
194f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   return nouveau_screen_bo_get_handle(pscreen,
195f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller                                       mt->base.bo,
196f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller                                       stride,
197f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller                                       whandle);
198287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell}
199287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
20051430c21ffd212d801f129d625f3ae8713372f68Christoph Bumillerconst struct u_resource_vtbl nv50_miptree_vtbl =
201287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell{
202f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   nv50_miptree_get_handle,         /* get_handle */
203f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   nv50_miptree_destroy,            /* resource_destroy */
204cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   nv50_miptree_transfer_map,       /* transfer_map */
205f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   u_default_transfer_flush_region, /* transfer_flush_region */
206f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   nv50_miptree_transfer_unmap,     /* transfer_unmap */
207287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell};
208287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell
209a2a1a5805fd617e7f3cc8be44dd79b50da07ebb9Ilia Mirkinstatic inline bool
210cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumillernv50_miptree_init_ms_mode(struct nv50_miptree *mt)
21147b418b8fa5fd242e9021503d6ec329ac3d56fb0Ben Skeggs{
212cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   switch (mt->base.base.nr_samples) {
213cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   case 8:
214cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      mt->ms_mode = NV50_3D_MULTISAMPLE_MODE_MS8;
215cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      mt->ms_x = 2;
216cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      mt->ms_y = 1;
217f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller      break;
218cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   case 4:
219cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      mt->ms_mode = NV50_3D_MULTISAMPLE_MODE_MS4;
220cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      mt->ms_x = 1;
221cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      mt->ms_y = 1;
222f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller      break;
223cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   case 2:
224cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      mt->ms_mode = NV50_3D_MULTISAMPLE_MODE_MS2;
225cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      mt->ms_x = 1;
226f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller      break;
227cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   case 1:
228cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   case 0:
229cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      mt->ms_mode = NV50_3D_MULTISAMPLE_MODE_MS1;
230f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller      break;
231f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   default:
232cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      NOUVEAU_ERR("invalid nr_samples: %u\n", mt->base.base.nr_samples);
233cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset      return false;
234f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   }
235cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset   return true;
236cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller}
237cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
238cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoisetbool
2398e1dd58a7e76834c1fb6d117b6ea5755e05cfb77Christoph Bumillernv50_miptree_init_layout_linear(struct nv50_miptree *mt, unsigned pitch_align)
240cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller{
241cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   struct pipe_resource *pt = &mt->base.base;
24255bab45a7c6c8e659e31b398b4ccd6e790c2738bChristoph Bumiller   const unsigned blocksize = util_format_get_blocksize(pt->format);
243ba9b0b682f51222752ac56e7a0615f9bbb686089Christoph Bumiller   unsigned h = pt->height0;
244cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
245cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   if (util_format_is_depth_or_stencil(pt->format))
246cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset      return false;
247cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
248cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   if ((pt->last_level > 0) || (pt->depth0 > 1) || (pt->array_size > 1))
249cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset      return false;
250cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   if (mt->ms_x | mt->ms_y)
251cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset      return false;
252cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
2538e1dd58a7e76834c1fb6d117b6ea5755e05cfb77Christoph Bumiller   mt->level[0].pitch = align(pt->width0 * blocksize, pitch_align);
254cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
255ba9b0b682f51222752ac56e7a0615f9bbb686089Christoph Bumiller   /* Account for very generous prefetch (allocate size as if tiled). */
256ba9b0b682f51222752ac56e7a0615f9bbb686089Christoph Bumiller   h = MAX2(h, 8);
257ba9b0b682f51222752ac56e7a0615f9bbb686089Christoph Bumiller   h = util_next_power_of_two(h);
258ba9b0b682f51222752ac56e7a0615f9bbb686089Christoph Bumiller
259ba9b0b682f51222752ac56e7a0615f9bbb686089Christoph Bumiller   mt->total_size = mt->level[0].pitch * h;
260cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
261cd0dec0d9dfab642c51774c3f5788cbdf00b8c9bSamuel Pitoiset   return true;
262cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller}
263cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
264cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumillerstatic void
265fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkinnv50_miptree_init_layout_video(struct nv50_miptree *mt)
266fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin{
267fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin   const struct pipe_resource *pt = &mt->base.base;
268fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin   const unsigned blocksize = util_format_get_blocksize(pt->format);
269fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin
270fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin   assert(pt->last_level == 0);
271fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin   assert(mt->ms_x == 0 && mt->ms_y == 0);
272fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin   assert(!util_format_is_compressed(pt->format));
273fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin
274fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin   mt->layout_3d = pt->target == PIPE_TEXTURE_3D;
275fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin
276fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin   mt->level[0].tile_mode = 0x20;
277fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin   mt->level[0].pitch = align(pt->width0 * blocksize, 64);
278fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin   mt->total_size = align(pt->height0, 16) * mt->level[0].pitch * (mt->layout_3d ? pt->depth0 : 1);
279fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin
280fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin   if (pt->array_size > 1) {
281fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin      mt->layer_stride = align(mt->total_size, NV50_TILE_SIZE(0x20));
282fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin      mt->total_size = mt->layer_stride * pt->array_size;
283fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin   }
284fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin}
285fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin
286fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkinstatic void
287cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumillernv50_miptree_init_layout_tiled(struct nv50_miptree *mt)
288cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller{
289cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   struct pipe_resource *pt = &mt->base.base;
290cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   unsigned w, h, d, l;
291cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   const unsigned blocksize = util_format_get_blocksize(pt->format);
292cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
293cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   mt->layout_3d = pt->target == PIPE_TEXTURE_3D;
294cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
295cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   w = pt->width0 << mt->ms_x;
296cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   h = pt->height0 << mt->ms_y;
297f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller
298f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   /* For 3D textures, a mipmap is spanned by all the layers, for array
299f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller    * textures and cube maps, each layer contains its own mipmaps.
300f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller    */
301cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   d = mt->layout_3d ? pt->depth0 : 1;
302cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
303f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   for (l = 0; l <= pt->last_level; ++l) {
304f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller      struct nv50_miptree_level *lvl = &mt->level[l];
305cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      unsigned tsx, tsy, tsz;
306f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller      unsigned nbx = util_format_get_nblocksx(pt->format, w);
307f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller      unsigned nby = util_format_get_nblocksy(pt->format, h);
308f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller
309f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller      lvl->offset = mt->total_size;
3103b2bc4ac5b63429738687fd296180149b54e67a4Marcin Ślusarz
311ae720c66cb91c2640dfd6707446899694a24ab5bIlia Mirkin      lvl->tile_mode = nv50_tex_choose_tile_dims(nbx, nby, d, mt->layout_3d);
3123b2bc4ac5b63429738687fd296180149b54e67a4Marcin Ślusarz
313cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      tsx = NV50_TILE_SIZE_X(lvl->tile_mode); /* x is tile row pitch in bytes */
314cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      tsy = NV50_TILE_SIZE_Y(lvl->tile_mode);
315cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      tsz = NV50_TILE_SIZE_Z(lvl->tile_mode);
316f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller
317cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      lvl->pitch = align(nbx * blocksize, tsx);
318cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
319cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      mt->total_size += lvl->pitch * align(nby, tsy) * align(d, tsz);
320f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller
321f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller      w = u_minify(w, 1);
322f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller      h = u_minify(h, 1);
323f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller      d = u_minify(d, 1);
324f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   }
325f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller
326f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   if (pt->array_size > 1) {
327f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller      mt->layer_stride = align(mt->total_size,
328f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller                               NV50_TILE_SIZE(mt->level[0].tile_mode));
329f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller      mt->total_size = mt->layer_stride * pt->array_size;
330f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   }
331cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller}
332cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
333cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumillerstruct pipe_resource *
334cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumillernv50_miptree_create(struct pipe_screen *pscreen,
335cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller                    const struct pipe_resource *templ)
336cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller{
337cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   struct nouveau_device *dev = nouveau_screen(pscreen)->device;
3381a9ec8e062f597fd4e4905602adc7c2e507c525fBen Skeggs   struct nouveau_drm *drm = nouveau_screen(pscreen)->drm;
339cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   struct nv50_miptree *mt = CALLOC_STRUCT(nv50_miptree);
340cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   struct pipe_resource *pt = &mt->base.base;
3411a9ec8e062f597fd4e4905602adc7c2e507c525fBen Skeggs   bool compressed = drm->version >= 0x01000101;
342cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   int ret;
3436d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   union nouveau_bo_config bo_config;
3446d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   uint32_t bo_flags;
345cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
346cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   if (!mt)
347cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      return NULL;
348cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
349cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   mt->base.vtbl = &nv50_miptree_vtbl;
350cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   *pt = *templ;
351cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   pipe_reference_init(&pt->reference, 1);
352cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   pt->screen = pscreen;
353cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
354e8f9195e5fb34a45783d6491d2e0305a0b137439Axel Davy   if (pt->bind & PIPE_BIND_LINEAR)
355e8f9195e5fb34a45783d6491d2e0305a0b137439Axel Davy      pt->flags |= NOUVEAU_RESOURCE_FLAG_LINEAR;
356e8f9195e5fb34a45783d6491d2e0305a0b137439Axel Davy
3571f8c0be27e1aa09e39d3016b6aae96c3ac228e17Tobias Klausmann   bo_config.nv50.memtype = nv50_mt_choose_storage_type(mt, compressed);
358cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
359cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   if (!nv50_miptree_init_ms_mode(mt)) {
360cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      FREE(mt);
361cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      return NULL;
362cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   }
363f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller
364fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin   if (unlikely(pt->flags & NV50_RESOURCE_FLAG_VIDEO)) {
365fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin      nv50_miptree_init_layout_video(mt);
366b3f6f127f28737d034dcf1c12a9b66b88750e93bIlia Mirkin      if (pt->flags & NV50_RESOURCE_FLAG_NOALLOC) {
367b3f6f127f28737d034dcf1c12a9b66b88750e93bIlia Mirkin         /* BO allocation done by client */
368b3f6f127f28737d034dcf1c12a9b66b88750e93bIlia Mirkin         return pt;
369b3f6f127f28737d034dcf1c12a9b66b88750e93bIlia Mirkin      }
370fbdae1ca41946af374c7924fd4c296199172890dIlia Mirkin   } else
3716d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   if (bo_config.nv50.memtype != 0) {
372cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      nv50_miptree_init_layout_tiled(mt);
373cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   } else
3748e1dd58a7e76834c1fb6d117b6ea5755e05cfb77Christoph Bumiller   if (!nv50_miptree_init_layout_linear(mt, 64)) {
375cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      FREE(mt);
376cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      return NULL;
377cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   }
3786d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   bo_config.nv50.tile_mode = mt->level[0].tile_mode;
3796d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller
380c58486516f2ec8341f92554e28fd84c10d835a45Axel Davy   if (!bo_config.nv50.memtype && (pt->bind & PIPE_BIND_SHARED))
381c58486516f2ec8341f92554e28fd84c10d835a45Axel Davy      mt->base.domain = NOUVEAU_BO_GART;
382c58486516f2ec8341f92554e28fd84c10d835a45Axel Davy   else
383da8300cb03e8cf1f37b5573a2db026fd28e0a3c5Alexandre Courbot      mt->base.domain = NV_VRAM_DOMAIN(nouveau_screen(pscreen));
384c58486516f2ec8341f92554e28fd84c10d835a45Axel Davy
385c58486516f2ec8341f92554e28fd84c10d835a45Axel Davy   bo_flags = mt->base.domain | NOUVEAU_BO_NOSNOOP;
3866d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   if (mt->base.base.bind & (PIPE_BIND_CURSOR | PIPE_BIND_DISPLAY_TARGET))
3876d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller      bo_flags |= NOUVEAU_BO_CONTIG;
388f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller
3896d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   ret = nouveau_bo_new(dev, bo_flags, 4096, mt->total_size, &bo_config,
3906d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller                        &mt->base.bo);
391f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   if (ret) {
392f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller      FREE(mt);
393f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller      return NULL;
394f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   }
3956d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   mt->base.address = mt->base.bo->offset;
396f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller
397f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   return pt;
39847b418b8fa5fd242e9021503d6ec329ac3d56fb0Ben Skeggs}
39947b418b8fa5fd242e9021503d6ec329ac3d56fb0Ben Skeggs
400287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwellstruct pipe_resource *
401287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwellnv50_miptree_from_handle(struct pipe_screen *pscreen,
402f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller                         const struct pipe_resource *templ,
403f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller                         struct winsys_handle *whandle)
40479bf0bdc7ffe97ec128e5dd143c4ed54648aae42Ben Skeggs{
405f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   struct nv50_miptree *mt;
406f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   unsigned stride;
407f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller
408f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   /* only supports 2D, non-mipmapped textures for the moment */
409f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   if ((templ->target != PIPE_TEXTURE_2D &&
410f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller        templ->target != PIPE_TEXTURE_RECT) ||
411f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller       templ->last_level != 0 ||
412f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller       templ->depth0 != 1 ||
413f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller       templ->array_size > 1)
414f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller      return NULL;
415f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller
416f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   mt = CALLOC_STRUCT(nv50_miptree);
417f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   if (!mt)
418f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller      return NULL;
419f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller
420f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   mt->base.bo = nouveau_screen_bo_from_handle(pscreen, whandle, &stride);
421f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   if (mt->base.bo == NULL) {
422f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller      FREE(mt);
423f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller      return NULL;
424f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   }
425c6877c9e5983287a0741b26a358b7d744aebe232Dave Airlie   mt->base.domain = mt->base.bo->flags & NOUVEAU_BO_APER;
4266d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   mt->base.address = mt->base.bo->offset;
427f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller
428f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   mt->base.base = *templ;
429f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   mt->base.vtbl = &nv50_miptree_vtbl;
430f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   pipe_reference_init(&mt->base.base.reference, 1);
431f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   mt->base.base.screen = pscreen;
432f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   mt->level[0].pitch = stride;
433f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   mt->level[0].offset = 0;
4346d1cdec3ba151168bfc3aef222fba6265dfb41fbChristoph Bumiller   mt->level[0].tile_mode = mt->base.bo->config.nv50.tile_mode;
435f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller
436404387ecd72a4a9ace8c1fa6895823aabfd759adJoakim Sindholt   NOUVEAU_DRV_STAT(nouveau_screen(pscreen), tex_obj_current_count, 1);
437404387ecd72a4a9ace8c1fa6895823aabfd759adJoakim Sindholt
438f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   /* no need to adjust bo reference count */
439f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   return &mt->base.base;
44079bf0bdc7ffe97ec128e5dd143c4ed54648aae42Ben Skeggs}
44179bf0bdc7ffe97ec128e5dd143c4ed54648aae42Ben Skeggs
4429f3644c42350fec2cda17e66548c517d9d00e47fChristoph Bumiller
443cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller/* Offset of zslice @z from start of level @l. */
444a2a1a5805fd617e7f3cc8be44dd79b50da07ebb9Ilia Mirkininline unsigned
445cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumillernv50_mt_zslice_offset(const struct nv50_miptree *mt, unsigned l, unsigned z)
446cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller{
447cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   const struct pipe_resource *pt = &mt->base.base;
448cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
449cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   unsigned tds = NV50_TILE_SHIFT_Z(mt->level[l].tile_mode);
450cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   unsigned ths = NV50_TILE_SHIFT_Y(mt->level[l].tile_mode);
451cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
452cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   unsigned nby = util_format_get_nblocksy(pt->format,
453cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller                                           u_minify(pt->height0, l));
454cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
455cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   /* to next 2D tile slice within a 3D tile */
456cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   unsigned stride_2d = NV50_TILE_SIZE_2D(mt->level[l].tile_mode);
457cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
458cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   /* to slice in the next (in z direction) 3D tile */
459cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   unsigned stride_3d = (align(nby, (1 << ths)) * mt->level[l].pitch) << tds;
460cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
461cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   return (z & ((1 << tds) - 1)) * stride_2d + (z >> tds) * stride_3d;
462cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller}
463cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
464f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller/* Surface functions.
465287c94ea4987033f9c99a2f91c5750c9083504caKeith Whitwell */
466b8965bee404cb36ccd97ac089fbd3ffc63268080Ben Skeggs
467cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumillerstruct nv50_surface *
468cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumillernv50_surface_from_miptree(struct nv50_miptree *mt,
469cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller                          const struct pipe_surface *templ)
47084cc07dc89c0ebce4ad55b4b3684d4420a202683Ben Skeggs{
471f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   struct pipe_surface *ps;
472cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   struct nv50_surface *ns = CALLOC_STRUCT(nv50_surface);
473f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   if (!ns)
474f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller      return NULL;
475f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   ps = &ns->base;
476f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller
477f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   pipe_reference_init(&ps->reference, 1);
478cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   pipe_resource_reference(&ps->texture, &mt->base.base);
479cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
480f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   ps->format = templ->format;
48147f2179844bd21f8112367ef8ed0acead7160f10Christoph Bumiller   ps->writable = templ->writable;
482f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   ps->u.tex.level = templ->u.tex.level;
483f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   ps->u.tex.first_layer = templ->u.tex.first_layer;
484f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   ps->u.tex.last_layer = templ->u.tex.last_layer;
485f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller
486cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   ns->width = u_minify(mt->base.base.width0, ps->u.tex.level);
487cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   ns->height = u_minify(mt->base.base.height0, ps->u.tex.level);
488f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   ns->depth = ps->u.tex.last_layer - ps->u.tex.first_layer + 1;
489cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   ns->offset = mt->level[templ->u.tex.level].offset;
490f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller
491f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   /* comment says there are going to be removed, but they're used by the st */
492f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   ps->width = ns->width;
493f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   ps->height = ns->height;
494f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller
495cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   ns->width <<= mt->ms_x;
496cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   ns->height <<= mt->ms_y;
497cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
498cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   return ns;
499cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller}
500cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
501cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumillerstruct pipe_surface *
502cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumillernv50_miptree_surface_new(struct pipe_context *pipe,
503cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller                         struct pipe_resource *pt,
504cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller                         const struct pipe_surface *templ)
505cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller{
506cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   struct nv50_miptree *mt = nv50_miptree(pt);
507cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   struct nv50_surface *ns = nv50_surface_from_miptree(mt, templ);
508cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   if (!ns)
509cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      return NULL;
510cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   ns->base.context = pipe;
511cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller
512cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   if (ns->base.u.tex.first_layer) {
513cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      const unsigned l = ns->base.u.tex.level;
514cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      const unsigned z = ns->base.u.tex.first_layer;
515f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller
516cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      if (mt->layout_3d) {
517cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller         ns->offset += nv50_mt_zslice_offset(mt, l, z);
518f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller
51956503fd13847ae5072af761ac69b923323e053f8Christoph Bumiller         /* TODO: switch to depth 1 tiles; but actually this shouldn't happen */
52056503fd13847ae5072af761ac69b923323e053f8Christoph Bumiller         if (ns->depth > 1 &&
52156503fd13847ae5072af761ac69b923323e053f8Christoph Bumiller             (z & (NV50_TILE_SIZE_Z(mt->level[l].tile_mode) - 1)))
522cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller            NOUVEAU_ERR("Creating unsupported 3D surface !\n");
523cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      } else {
524cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller         ns->offset += mt->layer_stride * z;
525cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller      }
526f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller   }
527f80c03e1875fe96ff2f4c022e3cb76357828140dChristoph Bumiller
528cad17554c4b121c03e188dd0281718a52d603a15Christoph Bumiller   return &ns->base;
52984cc07dc89c0ebce4ad55b4b3684d4420a202683Ben Skeggs}
530