aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/swr/rasterizer/memory/TilingFunctions.h
blob: 350e44b37b5921c5a9fdb3b1e5caf0f10de5fe22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/****************************************************************************
* Copyright (C) 2014-2015 Intel Corporation.   All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
* 
* @file TilingFunctions.h
* 
* @brief Tiling functions.
* 
******************************************************************************/
#pragma once

#include "core/state.h"
#include "core/format_traits.h"
#include "memory/tilingtraits.h"

#include <algorithm>

#define MAX_NUM_LOD 15

#define GFX_ALIGN(x, a) (((x) + ((a) - 1)) - (((x) + ((a) - 1)) & ((a) - 1))) // Alt implementation with bitwise not (~) has issue with uint32 align used with 64-bit value, since ~'ed value will remain 32-bit.

//////////////////////////////////////////////////////////////////////////
/// SimdTile SSE(2x2), AVX(4x2), or AVX-512(4x4?)
//////////////////////////////////////////////////////////////////////////
template<SWR_FORMAT HotTileFormat, SWR_FORMAT SrcOrDstFormat>
struct SimdTile
{
    // SimdTile is SOA (e.g. rrrrrrrr gggggggg bbbbbbbb aaaaaaaa )
    float color[FormatTraits<HotTileFormat>::numComps][KNOB_SIMD_WIDTH];

    //////////////////////////////////////////////////////////////////////////
    /// @brief Retrieve color from simd.
    /// @param index - linear index to color within simd.
    /// @param outputColor - output color
    INLINE void GetSwizzledColor(
        uint32_t index,
        float outputColor[4])
    {
        // SOA pattern for 2x2 is a subset of 4x2.
        //   0 1 4 5
        //   2 3 6 7
        // The offset converts pattern to linear
#if (SIMD_TILE_X_DIM == 4)
        static const uint32_t offset[] = { 0, 1, 4, 5, 2, 3, 6, 7 };
#elif (SIMD_TILE_X_DIM == 2)
        static const uint32_t offset[] = { 0, 1, 2, 3 };
#endif

        for (uint32_t i = 0; i < FormatTraits<SrcOrDstFormat>::numComps; ++i)
        {
            outputColor[i] = this->color[FormatTraits<SrcOrDstFormat>::swizzle(i)][offset[index]];
        }
    }

    //////////////////////////////////////////////////////////////////////////
    /// @brief Retrieve color from simd.
    /// @param index - linear index to color within simd.
    /// @param outputColor - output color
    INLINE void SetSwizzledColor(
        uint32_t index,
        const float src[4])
    {
        // SOA pattern for 2x2 is a subset of 4x2.
        //   0 1 4 5
        //   2 3 6 7
        // The offset converts pattern to linear
#if (SIMD_TILE_X_DIM == 4)
        static const uint32_t offset[] = { 0, 1, 4, 5, 2, 3, 6, 7 };
#elif (SIMD_TILE_X_DIM == 2)
        static const uint32_t offset[] = { 0, 1, 2, 3 };
#endif

        // Only loop over the components needed for destination.
        for (uint32_t i = 0; i < FormatTraits<SrcOrDstFormat>::numComps; ++i)
        {
            this->color[i][offset[index]] = src[i];
        }
    }
};

template<>
struct SimdTile <R8_UINT,R8_UINT>
{
    // SimdTile is SOA (e.g. rrrrrrrr gggggggg bbbbbbbb aaaaaaaa )
    uint8_t color[FormatTraits<R8_UINT>::numComps][KNOB_SIMD_WIDTH];

    //////////////////////////////////////////////////////////////////////////
    /// @brief Retrieve color from simd.
    /// @param index - linear index to color within simd.
    /// @param outputColor - output color
    INLINE void GetSwizzledColor(
        uint32_t index,
        float outputColor[4])
    {
        // SOA pattern for 2x2 is a subset of 4x2.
        //   0 1 4 5
        //   2 3 6 7
        // The offset converts pattern to linear
#if (SIMD_TILE_X_DIM == 4)
        static const uint32_t offset[] = { 0, 1, 4, 5, 2, 3, 6, 7 };
#elif (SIMD_TILE_X_DIM == 2)
        static const uint32_t offset[] = { 0, 1, 2, 3 };
#endif

        for (uint32_t i = 0; i < FormatTraits<R8_UINT>::numComps; ++i)
        {
            uint32_t src = this->color[FormatTraits<R8_UINT>::swizzle(i)][offset[index]];
            outputColor[i] = *(float*)&src;
        }
    }

    //////////////////////////////////////////////////////////////////////////
    /// @brief Retrieve color from simd.
    /// @param index - linear index to color within simd.
    /// @param outputColor - output color
    INLINE void SetSwizzledColor(
        uint32_t index,
        const float src[4])
    {
        // SOA pattern for 2x2 is a subset of 4x2.
        //   0 1 4 5
        //   2 3 6 7
        // The offset converts pattern to linear
#if (SIMD_TILE_X_DIM == 4)
        static const uint32_t offset[] = { 0, 1, 4, 5, 2, 3, 6, 7 };
#elif (SIMD_TILE_X_DIM == 2)
        static const uint32_t offset[] = { 0, 1, 2, 3 };
#endif

        // Only loop over the components needed for destination.
        for (uint32_t i = 0; i < FormatTraits<R8_UINT>::numComps; ++i)
        {
            this->color[i][offset[index]] = *(uint8_t*)&src[i];
        }
    }
};

#if ENABLE_AVX512_SIMD16
//////////////////////////////////////////////////////////////////////////
/// SimdTile 8x2 for AVX-512
//////////////////////////////////////////////////////////////////////////

template<SWR_FORMAT HotTileFormat, SWR_FORMAT SrcOrDstFormat>
struct SimdTile_16
{
    // SimdTile is SOA (e.g. rrrrrrrrrrrrrrrr gggggggggggggggg bbbbbbbbbbbbbbbb aaaaaaaaaaaaaaaa )
    float color[FormatTraits<HotTileFormat>::numComps][KNOB_SIMD16_WIDTH];

    //////////////////////////////////////////////////////////////////////////
    /// @brief Retrieve color from simd.
    /// @param index - linear index to color within simd.
    /// @param outputColor - output color
    INLINE void GetSwizzledColor(
        uint32_t index,
        float outputColor[4])
    {
        // SOA pattern for 8x2..
        //   0 1 4 5 8 9 C D
        //   2 3 6 7 A B E F
        // The offset converts pattern to linear
        static const uint32_t offset[KNOB_SIMD16_WIDTH] = { 0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15 };

        for (uint32_t i = 0; i < FormatTraits<SrcOrDstFormat>::numComps; ++i)
        {
            outputColor[i] = this->color[FormatTraits<SrcOrDstFormat>::swizzle(i)][offset[index]];
        }
    }

    //////////////////////////////////////////////////////////////////////////
    /// @brief Retrieve color from simd.
    /// @param index - linear index to color within simd.
    /// @param outputColor - output color
    INLINE void SetSwizzledColor(
        uint32_t index,
        const float src[4])
    {
        // SOA pattern for 8x2..
        //   0 1 4 5 8 9 C D
        //   2 3 6 7 A B E F
        // The offset converts pattern to linear
        static const uint32_t offset[KNOB_SIMD16_WIDTH] = { 0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15 };

        for (uint32_t i = 0; i < FormatTraits<SrcOrDstFormat>::numComps; ++i)
        {
            this->color[i][offset[index]] = src[i];
        }
    }
};

template<>
struct SimdTile_16 <R8_UINT, R8_UINT>
{
    // SimdTile is SOA (e.g. rrrrrrrrrrrrrrrr gggggggggggggggg bbbbbbbbbbbbbbbb aaaaaaaaaaaaaaaa )
    uint8_t color[FormatTraits<R8_UINT>::numComps][KNOB_SIMD16_WIDTH];

    //////////////////////////////////////////////////////////////////////////
    /// @brief Retrieve color from simd.
    /// @param index - linear index to color within simd.
    /// @param outputColor - output color
    INLINE void GetSwizzledColor(
        uint32_t index,
        float outputColor[4])
    {
        // SOA pattern for 8x2..
        //   0 1 4 5 8 9 C D
        //   2 3 6 7 A B E F
        // The offset converts pattern to linear
        static const uint32_t offset[KNOB_SIMD16_WIDTH] = { 0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15 };

        for (uint32_t i = 0; i < FormatTraits<R8_UINT>::numComps; ++i)
        {
            uint32_t src = this->color[FormatTraits<R8_UINT>::swizzle(i)][offset[index]];
            outputColor[i] = *(float*)&src;
        }
    }

    //////////////////////////////////////////////////////////////////////////
    /// @brief Retrieve color from simd.
    /// @param index - linear index to color within simd.
    /// @param outputColor - output color
    INLINE void SetSwizzledColor(
        uint32_t index,
        const float src[4])
    {
        // SOA pattern for 8x2..
        //   0 1 4 5 8 9 C D
        //   2 3 6 7 A B E F
        // The offset converts pattern to linear
        static const uint32_t offset[KNOB_SIMD16_WIDTH] = { 0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15 };

        for (uint32_t i = 0; i < FormatTraits<R8_UINT>::numComps; ++i)
        {
            this->color[i][offset[index]] = *(uint8_t*)&src[i];
        }
    }
};

#endif
//////////////////////////////////////////////////////////////////////////
/// @brief Computes lod offset for 1D surface at specified lod.
/// @param baseWidth - width of basemip (mip 0).
/// @param hAlign - horizontal alignment per miip, in texels
/// @param lod - lod index
/// @param offset - output offset.
INLINE void ComputeLODOffset1D(
    const SWR_FORMAT_INFO& info,
    uint32_t baseWidth,
    uint32_t hAlign,
    uint32_t lod,
    uint32_t &offset)
{
    if (lod == 0)
    {
        offset = 0;
    }
    else
    {
        uint32_t curWidth = baseWidth;
        // @note hAlign is already in blocks for compressed formats so upconvert
        //       so that we have the desired alignment post-divide.
        if (info.isBC)
        {
            hAlign *= info.bcWidth;
        }

        offset = GFX_ALIGN(curWidth, hAlign);
        for (uint32_t l = 1; l < lod; ++l)
        {
            curWidth = std::max<uint32_t>(curWidth >> 1, 1U);
            offset += GFX_ALIGN(curWidth, hAlign);
        }

        if (info.isSubsampled || info.isBC)
        {
            offset /= info.bcWidth;
        }
    }
}

//////////////////////////////////////////////////////////////////////////
/// @brief Computes x lod offset for 2D surface at specified lod.
/// @param baseWidth - width of basemip (mip 0).
/// @param hAlign - horizontal alignment per mip, in texels
/// @param lod - lod index
/// @param offset - output offset.
INLINE void ComputeLODOffsetX(
    const SWR_FORMAT_INFO& info,
    uint32_t baseWidth,
    uint32_t hAlign,
    uint32_t lod,
    uint32_t &offset)
{
    if (lod < 2)
    {
        offset = 0;
    }
    else
    {
        uint32_t curWidth = baseWidth;
        // @note hAlign is already in blocks for compressed formats so upconvert
        //       so that we have the desired alignment post-divide.
        if (info.isBC)
        {
            hAlign *= info.bcWidth;
        }

        curWidth = std::max<uint32_t>(curWidth >> 1, 1U);
        curWidth = GFX_ALIGN(curWidth, hAlign);

        if (info.isSubsampled || info.isBC)
        {
            curWidth /= info.bcWidth;
        }

        offset = curWidth;
    }
}

//////////////////////////////////////////////////////////////////////////
/// @brief Computes y lod offset for 2D surface at specified lod.
/// @param baseWidth - width of basemip (mip 0).
/// @param vAlign - vertical alignment per mip, in rows
/// @param lod - lod index
/// @param offset - output offset.
INLINE void ComputeLODOffsetY(
    const SWR_FORMAT_INFO& info,
    uint32_t baseHeight,
    uint32_t vAlign,
    uint32_t lod,
    uint32_t &offset)
{
    if (lod == 0)
    {
        offset = 0;
    }
    else
    {
        offset = 0;
        uint32_t mipHeight = baseHeight;

        // @note vAlign is already in blocks for compressed formats so upconvert
        //       so that we have the desired alignment post-divide.
        if (info.isBC)
        {
            vAlign *= info.bcHeight;
        }

        for (uint32_t l = 1; l <= lod; ++l)
        {
            uint32_t alignedMipHeight = GFX_ALIGN(mipHeight, vAlign);
            offset += ((l != 2) ? alignedMipHeight : 0);
            mipHeight = std::max<uint32_t>(mipHeight >> 1, 1U);
        }

        if (info.isBC)
        {
            offset /= info.bcHeight;
        }
    }
}

//////////////////////////////////////////////////////////////////////////
/// @brief Computes 1D surface offset
/// @param x - offset from start of array slice at given lod.
/// @param array - array slice index
/// @param lod - lod index
/// @param pState - surface state
/// @param xOffsetBytes - output offset in bytes.
template<bool UseCachedOffsets>
INLINE void ComputeSurfaceOffset1D(
    uint32_t x,
    uint32_t array,
    uint32_t lod,
    const SWR_SURFACE_STATE *pState,
    uint32_t &xOffsetBytes)
{
    const SWR_FORMAT_INFO &info = GetFormatInfo(pState->format);
    uint32_t lodOffset;

    if (UseCachedOffsets)
    {
        lodOffset = pState->lodOffsets[0][lod];
    }
    else
    {
        ComputeLODOffset1D(info, pState->width, pState->halign, lod, lodOffset);
    }

    xOffsetBytes = (array * pState->qpitch + lodOffset + x) * info.Bpp;
}

//////////////////////////////////////////////////////////////////////////
/// @brief Adjusts the array slice for legacy TileY MSAA
/// @param pState - surface state
/// @param array - array slice index
/// @param sampleNum - requested sample
INLINE void AdjustCoordsForMSAA(const SWR_SURFACE_STATE *pState, uint32_t& x, uint32_t& y, uint32_t& arrayIndex, uint32_t sampleNum)
{
    /// @todo: might want to templatize adjusting for sample slices when we support tileYS/tileYF.
    if((pState->tileMode == SWR_TILE_MODE_YMAJOR ||
        pState->tileMode == SWR_TILE_MODE_WMAJOR) && 
       pState->bInterleavedSamples)
    {
        uint32_t newX, newY, newSampleX, newSampleY;
        switch(pState->numSamples)
        {
        case 1:
            newX = x;
            newY = y;
            newSampleX = newSampleY = 0;
            break;
        case 2:
        {
            assert(pState->type == SURFACE_2D);
            static const uint32_t xMask = 0xFFFFFFFD;
            static const uint32_t sampleMaskX = 0x1;
            newX = pdep_u32(x, xMask);
            newY = y;
            newSampleX = pext_u32(sampleNum, sampleMaskX);
            newSampleY = 0;
        }
            break;
        case 4:
        {
            assert(pState->type == SURFACE_2D);
            static const uint32_t mask = 0xFFFFFFFD;
            static const uint32_t sampleMaskX = 0x1;
            static const uint32_t sampleMaskY = 0x2;
            newX = pdep_u32(x, mask);
            newY = pdep_u32(y, mask);
            newSampleX = pext_u32(sampleNum, sampleMaskX);
            newSampleY = pext_u32(sampleNum, sampleMaskY);
        }
            break;
        case 8:
        {
            assert(pState->type == SURFACE_2D);
            static const uint32_t xMask = 0xFFFFFFF9;
            static const uint32_t yMask = 0xFFFFFFFD;
            static const uint32_t sampleMaskX = 0x5;
            static const uint32_t sampleMaskY = 0x2;
            newX = pdep_u32(x, xMask);
            newY = pdep_u32(y, yMask);
            newSampleX = pext_u32(sampleNum, sampleMaskX);
            newSampleY = pext_u32(sampleNum, sampleMaskY);
        }
            break;
        case 16:
        {
            assert(pState->type == SURFACE_2D);
            static const uint32_t mask = 0xFFFFFFF9;
            static const uint32_t sampleMaskX = 0x5;
            static const uint32_t sampleMaskY = 0xA;
            newX = pdep_u32(x, mask);
            newY = pdep_u32(y, mask);
            newSampleX = pext_u32(sampleNum, sampleMaskX);
            newSampleY = pext_u32(sampleNum, sampleMaskY);
        }
            break;
        default:
            assert(0 && "Unsupported sample count");
            newX = newY = 0;
            newSampleX = newSampleY = 0;
            break;
        }
        x = newX | (newSampleX << 1);
        y = newY | (newSampleY << 1);
    }
    else if(pState->tileMode == SWR_TILE_MODE_YMAJOR ||
            pState->tileMode == SWR_TILE_NONE)
    {
        uint32_t sampleShift;
        switch(pState->numSamples)
        {
        case 1:
            assert(sampleNum == 0);
            sampleShift = 0;
            break;
        case 2:
            assert(pState->type == SURFACE_2D);
            sampleShift = 1;
            break;
        case 4:
            assert(pState->type == SURFACE_2D);
            sampleShift = 2;
            break;
        case 8:
            assert(pState->type == SURFACE_2D);
            sampleShift = 3;
            break;
        case 16:
            assert(pState->type == SURFACE_2D);
            sampleShift = 4;
            break;
        default:
            assert(0 && "Unsupported sample count");
            sampleShift = 0;
            break;
        }
        arrayIndex = (arrayIndex << sampleShift) | sampleNum;
    }
}

//////////////////////////////////////////////////////////////////////////
/// @brief Computes 2D surface offset
/// @param x - horizontal offset from start of array slice and lod.
/// @param y - vertical offset from start of array slice and lod.
/// @param array - array slice index
/// @param lod - lod index
/// @param pState - surface state
/// @param xOffsetBytes - output x offset in bytes.
/// @param yOffsetRows - output y offset in bytes.
template<bool UseCachedOffsets>
INLINE void ComputeSurfaceOffset2D(uint32_t x, uint32_t y, uint32_t array, uint32_t sampleNum, uint32_t lod, const SWR_SURFACE_STATE *pState, uint32_t &xOffsetBytes, uint32_t &yOffsetRows)
{
    const SWR_FORMAT_INFO &info = GetFormatInfo(pState->format);
    uint32_t lodOffsetX, lodOffsetY;

    if (UseCachedOffsets)
    {
        lodOffsetX = pState->lodOffsets[0][lod];
        lodOffsetY = pState->lodOffsets[1][lod];
    }
    else
    {
        ComputeLODOffsetX(info, pState->width, pState->halign, lod, lodOffsetX);
        ComputeLODOffsetY(info, pState->height, pState->valign, lod, lodOffsetY);
    }

    AdjustCoordsForMSAA(pState, x, y, array, sampleNum);
    xOffsetBytes = (x + lodOffsetX + pState->xOffset) * info.Bpp;
    yOffsetRows = (array * pState->qpitch) + lodOffsetY + y + pState->yOffset;
}

//////////////////////////////////////////////////////////////////////////
/// @brief Computes 3D surface offset
/// @param x - horizontal offset from start of array slice and lod.
/// @param y - vertical offset from start of array slice and lod.
/// @param z - depth offset from start of array slice and lod.
/// @param lod - lod index
/// @param pState - surface state
/// @param xOffsetBytes - output x offset in bytes.
/// @param yOffsetRows - output y offset in rows.
/// @param zOffsetSlices - output y offset in slices.
template<bool UseCachedOffsets>
INLINE void ComputeSurfaceOffset3D(uint32_t x, uint32_t y, uint32_t z, uint32_t lod, const SWR_SURFACE_STATE *pState, uint32_t &xOffsetBytes, uint32_t &yOffsetRows, uint32_t &zOffsetSlices)
{
    const SWR_FORMAT_INFO &info = GetFormatInfo(pState->format);
    uint32_t lodOffsetX, lodOffsetY;

    if (UseCachedOffsets)
    {
        lodOffsetX = pState->lodOffsets[0][lod];
        lodOffsetY = pState->lodOffsets[1][lod];
    }
    else
    {
        ComputeLODOffsetX(info, pState->width, pState->halign, lod, lodOffsetX);
        ComputeLODOffsetY(info, pState->height, pState->valign, lod, lodOffsetY);
    }

    xOffsetBytes = (x + lodOffsetX) * info.Bpp;
    yOffsetRows = lodOffsetY + y;
    zOffsetSlices = z;
}

//////////////////////////////////////////////////////////////////////////
/// @brief Swizzles the linear x,y offsets depending on surface tiling mode
///        and returns final surface address
/// @param xOffsetBytes - x offset from base of surface in bytes
/// @param yOffsetRows - y offset from base of surface in rows
/// @param pState - pointer to the surface state
template<typename TTraits>
INLINE uint32_t ComputeTileSwizzle2D(uint32_t xOffsetBytes, uint32_t yOffsetRows, const SWR_SURFACE_STATE *pState)
{
    return ComputeOffset2D<TTraits>(pState->pitch, xOffsetBytes, yOffsetRows);
}

//////////////////////////////////////////////////////////////////////////
/// @brief Swizzles the linear x,y offsets depending on surface tiling mode
///        and returns final surface address
/// @param xOffsetBytes - x offset from base of surface in bytes
/// @param yOffsetRows - y offset from base of surface in rows
/// @param pState - pointer to the surface state
template<typename TTraits>
INLINE uint32_t ComputeTileSwizzle3D(uint32_t xOffsetBytes, uint32_t yOffsetRows, uint32_t zOffsetSlices, const SWR_SURFACE_STATE *pState)
{
    return ComputeOffset3D<TTraits>(pState->qpitch, pState->pitch, xOffsetBytes, yOffsetRows, zOffsetSlices);
}

//////////////////////////////////////////////////////////////////////////
/// @brief Swizzles the linear x,y offsets depending on surface tiling mode
///        and returns final surface address
/// @param xOffsetBytes - x offset from base of surface in bytes
/// @param yOffsetRows - y offset from base of surface in rows
/// @param pState - pointer to the surface state
INLINE
uint32_t TileSwizzle2D(uint32_t xOffsetBytes, uint32_t yOffsetRows, const SWR_SURFACE_STATE *pState)
{
    switch (pState->tileMode)
    {
    case SWR_TILE_NONE: return ComputeTileSwizzle2D<TilingTraits<SWR_TILE_NONE, 32> >(xOffsetBytes, yOffsetRows, pState);
    case SWR_TILE_SWRZ: return ComputeTileSwizzle2D<TilingTraits<SWR_TILE_SWRZ, 32> >(xOffsetBytes, yOffsetRows, pState);
    case SWR_TILE_MODE_XMAJOR: return ComputeTileSwizzle2D<TilingTraits<SWR_TILE_MODE_XMAJOR, 8> >(xOffsetBytes, yOffsetRows, pState);
    case SWR_TILE_MODE_YMAJOR: return ComputeTileSwizzle2D<TilingTraits<SWR_TILE_MODE_YMAJOR, 32> >(xOffsetBytes, yOffsetRows, pState);
    case SWR_TILE_MODE_WMAJOR: return ComputeTileSwizzle2D<TilingTraits<SWR_TILE_MODE_WMAJOR, 8> >(xOffsetBytes, yOffsetRows, pState);
    default: SWR_ASSERT(0, "Unsupported tiling mode");
    }
    return (uint32_t) NULL;
}

//////////////////////////////////////////////////////////////////////////
/// @brief Swizzles the linear x,y,z offsets depending on surface tiling mode
///        and returns final surface address
/// @param xOffsetBytes - x offset from base of surface in bytes
/// @param yOffsetRows - y offset from base of surface in rows
/// @param zOffsetSlices - z offset from base of surface in slices
/// @param pState - pointer to the surface state
INLINE
uint32_t TileSwizzle3D(uint32_t xOffsetBytes, uint32_t yOffsetRows, uint32_t zOffsetSlices, const SWR_SURFACE_STATE *pState)
{
    switch (pState->tileMode)
    {
    case SWR_TILE_NONE: return ComputeTileSwizzle3D<TilingTraits<SWR_TILE_NONE, 32> >(xOffsetBytes, yOffsetRows, zOffsetSlices, pState);
    case SWR_TILE_SWRZ: return ComputeTileSwizzle3D<TilingTraits<SWR_TILE_SWRZ, 32> >(xOffsetBytes, yOffsetRows, zOffsetSlices, pState);
    case SWR_TILE_MODE_YMAJOR: return ComputeTileSwizzle3D<TilingTraits<SWR_TILE_MODE_YMAJOR, 32> >(xOffsetBytes, yOffsetRows, zOffsetSlices, pState);
    default: SWR_ASSERT(0, "Unsupported tiling mode");
    }
    return (uint32_t) NULL;
}

template<bool UseCachedOffsets>
INLINE
uint32_t ComputeSurfaceOffset(uint32_t x, uint32_t y, uint32_t z, uint32_t array, uint32_t sampleNum, uint32_t lod, const SWR_SURFACE_STATE *pState)
{
    uint32_t offsetX = 0, offsetY = 0, offsetZ = 0;
    switch (pState->type)
    {
    case SURFACE_BUFFER:
    case SURFACE_STRUCTURED_BUFFER:
        offsetX = x * pState->pitch;
        return offsetX;
        break;
    case SURFACE_1D:
        ComputeSurfaceOffset1D<UseCachedOffsets>(x, array, lod, pState, offsetX);
        return TileSwizzle2D(offsetX, 0, pState);
        break;
    case SURFACE_2D:
        ComputeSurfaceOffset2D<UseCachedOffsets>(x, y, array, sampleNum, lod, pState, offsetX, offsetY);
        return TileSwizzle2D(offsetX, offsetY, pState);
    case SURFACE_3D:
        ComputeSurfaceOffset3D<UseCachedOffsets>(x, y, z, lod, pState, offsetX, offsetY, offsetZ);
        return TileSwizzle3D(offsetX, offsetY, offsetZ, pState);
        break;
    case SURFACE_CUBE:
        ComputeSurfaceOffset2D<UseCachedOffsets>(x, y, array, sampleNum, lod, pState, offsetX, offsetY);
        return TileSwizzle2D(offsetX, offsetY, pState);
        break;
    default: SWR_ASSERT(0, "Unsupported format");
    }

    return (uint32_t) NULL;
}

typedef void*(*PFN_COMPUTESURFADDR)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, const SWR_SURFACE_STATE*);

//////////////////////////////////////////////////////////////////////////
/// @brief Computes surface address at the given location and lod
/// @param x - x location in pixels
/// @param y - y location in rows
/// @param z - z location for 3D surfaces
/// @param array - array slice for 1D and 2D surfaces
/// @param lod - level of detail
/// @param pState - pointer to the surface state
template<bool UseCachedOffsets, bool IsRead>
INLINE
void* ComputeSurfaceAddress(uint32_t x, uint32_t y, uint32_t z, uint32_t array, uint32_t sampleNum, uint32_t lod, const SWR_SURFACE_STATE *pState)
{
    return pState->pBaseAddress + ComputeSurfaceOffset<UseCachedOffsets>(x, y, z, array, sampleNum, lod, pState);
}