146c3cf18315345effd15a69987294c1195843e2aBrian Paul/**************************************************************************
246c3cf18315345effd15a69987294c1195843e2aBrian Paul *
3877128505431adaf817dc8069172ebe4a1cdf5d8José Fonseca * Copyright 2007 VMware, Inc.
446c3cf18315345effd15a69987294c1195843e2aBrian Paul * All Rights Reserved.
546c3cf18315345effd15a69987294c1195843e2aBrian Paul *
646c3cf18315345effd15a69987294c1195843e2aBrian Paul * Permission is hereby granted, free of charge, to any person obtaining a
746c3cf18315345effd15a69987294c1195843e2aBrian Paul * copy of this software and associated documentation files (the
846c3cf18315345effd15a69987294c1195843e2aBrian Paul * "Software"), to deal in the Software without restriction, including
946c3cf18315345effd15a69987294c1195843e2aBrian Paul * without limitation the rights to use, copy, modify, merge, publish,
1046c3cf18315345effd15a69987294c1195843e2aBrian Paul * distribute, sub license, and/or sell copies of the Software, and to
1146c3cf18315345effd15a69987294c1195843e2aBrian Paul * permit persons to whom the Software is furnished to do so, subject to
1246c3cf18315345effd15a69987294c1195843e2aBrian Paul * the following conditions:
1346c3cf18315345effd15a69987294c1195843e2aBrian Paul *
1446c3cf18315345effd15a69987294c1195843e2aBrian Paul * The above copyright notice and this permission notice (including the
1546c3cf18315345effd15a69987294c1195843e2aBrian Paul * next paragraph) shall be included in all copies or substantial portions
1646c3cf18315345effd15a69987294c1195843e2aBrian Paul * of the Software.
1746c3cf18315345effd15a69987294c1195843e2aBrian Paul *
1846c3cf18315345effd15a69987294c1195843e2aBrian Paul * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1946c3cf18315345effd15a69987294c1195843e2aBrian Paul * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2046c3cf18315345effd15a69987294c1195843e2aBrian Paul * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21877128505431adaf817dc8069172ebe4a1cdf5d8José Fonseca * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
2246c3cf18315345effd15a69987294c1195843e2aBrian Paul * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
2346c3cf18315345effd15a69987294c1195843e2aBrian Paul * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
2446c3cf18315345effd15a69987294c1195843e2aBrian Paul * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2546c3cf18315345effd15a69987294c1195843e2aBrian Paul *
2646c3cf18315345effd15a69987294c1195843e2aBrian Paul **************************************************************************/
2746c3cf18315345effd15a69987294c1195843e2aBrian Paul
2846c3cf18315345effd15a69987294c1195843e2aBrian Paul#ifndef SP_TILE_CACHE_H
2946c3cf18315345effd15a69987294c1195843e2aBrian Paul#define SP_TILE_CACHE_H
3046c3cf18315345effd15a69987294c1195843e2aBrian Paul
3146c3cf18315345effd15a69987294c1195843e2aBrian Paul
3276a4fd098f44ae4f226d4747b9fdaf9db5d40270Brian Paul#include "pipe/p_compiler.h"
33af4f75c8fe1c53b8def966f480e7fda8021f1b63Brian Paul#include "sp_texture.h"
3446c3cf18315345effd15a69987294c1195843e2aBrian Paul
3546c3cf18315345effd15a69987294c1195843e2aBrian Paul
3646c3cf18315345effd15a69987294c1195843e2aBrian Paulstruct softpipe_tile_cache;
3746c3cf18315345effd15a69987294c1195843e2aBrian Paul
3846c3cf18315345effd15a69987294c1195843e2aBrian Paul
397e8396399824108d62dc3e02b2af0422e98aab8eBrian Paul/**
407e8396399824108d62dc3e02b2af0422e98aab8eBrian Paul * Cache tile size (width and height). This needs to be a power of two.
417e8396399824108d62dc3e02b2af0422e98aab8eBrian Paul */
42af4f75c8fe1c53b8def966f480e7fda8021f1b63Brian Paul#define TILE_SIZE_LOG2 6
43af4f75c8fe1c53b8def966f480e7fda8021f1b63Brian Paul#define TILE_SIZE (1 << TILE_SIZE_LOG2)
4446c3cf18315345effd15a69987294c1195843e2aBrian Paul
4546c3cf18315345effd15a69987294c1195843e2aBrian Paul
46af4f75c8fe1c53b8def966f480e7fda8021f1b63Brian Paul#define TILE_ADDR_BITS (SP_MAX_TEXTURE_2D_LEVELS - 1 - TILE_SIZE_LOG2)
47af4f75c8fe1c53b8def966f480e7fda8021f1b63Brian Paul
48af4f75c8fe1c53b8def966f480e7fda8021f1b63Brian Paul
49af4f75c8fe1c53b8def966f480e7fda8021f1b63Brian Paul/**
50af4f75c8fe1c53b8def966f480e7fda8021f1b63Brian Paul * Surface tile address as a union for fast compares.
51f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell */
52f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwellunion tile_address {
53f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell   struct {
54af4f75c8fe1c53b8def966f480e7fda8021f1b63Brian Paul      unsigned x:TILE_ADDR_BITS;     /* 16K / TILE_SIZE */
55af4f75c8fe1c53b8def966f480e7fda8021f1b63Brian Paul      unsigned y:TILE_ADDR_BITS;     /* 16K / TILE_SIZE */
56f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell      unsigned invalid:1;
57dc8fc39adac79cb8cbf59fc7ae5536536dceb52aDave Airlie      unsigned layer:8;
58dc8fc39adac79cb8cbf59fc7ae5536536dceb52aDave Airlie      unsigned pad:7;
59f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell   } bits;
60f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell   unsigned value;
61f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell};
62f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell
6346c3cf18315345effd15a69987294c1195843e2aBrian Paul
6446c3cf18315345effd15a69987294c1195843e2aBrian Paulstruct softpipe_cached_tile
6546c3cf18315345effd15a69987294c1195843e2aBrian Paul{
6646c3cf18315345effd15a69987294c1195843e2aBrian Paul   union {
6746c3cf18315345effd15a69987294c1195843e2aBrian Paul      float color[TILE_SIZE][TILE_SIZE][4];
68127a493fe2a2f7fd8515b278185322dfacdad357Brian Paul      uint color32[TILE_SIZE][TILE_SIZE];
697e8396399824108d62dc3e02b2af0422e98aab8eBrian Paul      uint depth32[TILE_SIZE][TILE_SIZE];
707e8396399824108d62dc3e02b2af0422e98aab8eBrian Paul      ushort depth16[TILE_SIZE][TILE_SIZE];
717e8396399824108d62dc3e02b2af0422e98aab8eBrian Paul      ubyte stencil8[TILE_SIZE][TILE_SIZE];
72396ac41fc285f0d7c8545f2b32ba373693a7a326Dave Airlie      uint colorui128[TILE_SIZE][TILE_SIZE][4];
73396ac41fc285f0d7c8545f2b32ba373693a7a326Dave Airlie      int colori128[TILE_SIZE][TILE_SIZE][4];
74e763b6e78825f11aa3e9e2368ba8fc47313a7848Morgan Armand      uint64_t depth64[TILE_SIZE][TILE_SIZE];
75127a493fe2a2f7fd8515b278185322dfacdad357Brian Paul      ubyte any[1];
7646c3cf18315345effd15a69987294c1195843e2aBrian Paul   } data;
7746c3cf18315345effd15a69987294c1195843e2aBrian Paul};
7846c3cf18315345effd15a69987294c1195843e2aBrian Paul
79f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell#define NUM_ENTRIES 50
80f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell
81f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell
82f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwellstruct softpipe_tile_cache
83f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell{
84b43c182f19c6291c88420fa12714f952c2b461fbKeith Whitwell   struct pipe_context *pipe;
85f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell   struct pipe_surface *surface;  /**< the surface we're caching */
86dc8fc39adac79cb8cbf59fc7ae5536536dceb52aDave Airlie   struct pipe_transfer **transfer;
87dc8fc39adac79cb8cbf59fc7ae5536536dceb52aDave Airlie   void **transfer_map;
88dc8fc39adac79cb8cbf59fc7ae5536536dceb52aDave Airlie   int num_maps;
89f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell
90029c099b54b24a4ecbe63f5fbe2df6c91da79b63Brian Paul   union tile_address tile_addrs[NUM_ENTRIES];
91029c099b54b24a4ecbe63f5fbe2df6c91da79b63Brian Paul   struct softpipe_cached_tile *entries[NUM_ENTRIES];
92dc8fc39adac79cb8cbf59fc7ae5536536dceb52aDave Airlie   uint *clear_flags;
93dc8fc39adac79cb8cbf59fc7ae5536536dceb52aDave Airlie   uint clear_flags_size;
94396ac41fc285f0d7c8545f2b32ba373693a7a326Dave Airlie   union pipe_color_union clear_color; /**< for color bufs */
95e763b6e78825f11aa3e9e2368ba8fc47313a7848Morgan Armand   uint64_t clear_val;        /**< for z+stencil */
96f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell   boolean depth_stencil; /**< Is the surface a depth/stencil format? */
97f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell
98029c099b54b24a4ecbe63f5fbe2df6c91da79b63Brian Paul   struct softpipe_cached_tile *tile;  /**< scratch tile for clears */
99f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell
100029c099b54b24a4ecbe63f5fbe2df6c91da79b63Brian Paul   union tile_address last_tile_addr;
101f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell   struct softpipe_cached_tile *last_tile;  /**< most recently retrieved tile */
102f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell};
103f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell
10446c3cf18315345effd15a69987294c1195843e2aBrian Paul
10546c3cf18315345effd15a69987294c1195843e2aBrian Paulextern struct softpipe_tile_cache *
106b43c182f19c6291c88420fa12714f952c2b461fbKeith Whitwellsp_create_tile_cache( struct pipe_context *pipe );
10746c3cf18315345effd15a69987294c1195843e2aBrian Paul
10846c3cf18315345effd15a69987294c1195843e2aBrian Paulextern void
10946c3cf18315345effd15a69987294c1195843e2aBrian Paulsp_destroy_tile_cache(struct softpipe_tile_cache *tc);
11046c3cf18315345effd15a69987294c1195843e2aBrian Paul
11146c3cf18315345effd15a69987294c1195843e2aBrian Paulextern void
11257df347bb8a119e1d00fe808e30a0623e0ae8563Brian Paulsp_tile_cache_set_surface(struct softpipe_tile_cache *tc,
113f6a73c3f2815c4c84563c186bba6c8e67bb42ae9Brian Paul                          struct pipe_surface *sps);
11446c3cf18315345effd15a69987294c1195843e2aBrian Paul
115f6a73c3f2815c4c84563c186bba6c8e67bb42ae9Brian Paulextern struct pipe_surface *
11657df347bb8a119e1d00fe808e30a0623e0ae8563Brian Paulsp_tile_cache_get_surface(struct softpipe_tile_cache *tc);
117c76efb96b405e43e3261d1dc9e8812fdb2cfbac8Michel Dänzer
118c76efb96b405e43e3261d1dc9e8812fdb2cfbac8Michel Dänzerextern void
119aa5db684382bd8662a83ca09ed000e4a5a1013f9Keith Whitwellsp_flush_tile_cache(struct softpipe_tile_cache *tc);
1207e8396399824108d62dc3e02b2af0422e98aab8eBrian Paul
1217e8396399824108d62dc3e02b2af0422e98aab8eBrian Paulextern void
122396ac41fc285f0d7c8545f2b32ba373693a7a326Dave Airliesp_tile_cache_clear(struct softpipe_tile_cache *tc,
123396ac41fc285f0d7c8545f2b32ba373693a7a326Dave Airlie                    const union pipe_color_union *color,
124e763b6e78825f11aa3e9e2368ba8fc47313a7848Morgan Armand                    uint64_t clearValue);
12546c3cf18315345effd15a69987294c1195843e2aBrian Paul
12646c3cf18315345effd15a69987294c1195843e2aBrian Paulextern struct softpipe_cached_tile *
12719097907ef042b97bbbda39b34bf3212f4cf154aKeith Whitwellsp_find_cached_tile(struct softpipe_tile_cache *tc,
12819097907ef042b97bbbda39b34bf3212f4cf154aKeith Whitwell                    union tile_address addr );
12946c3cf18315345effd15a69987294c1195843e2aBrian Paul
130f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell
131a2a1a5805fd617e7f3cc8be44dd79b50da07ebb9Ilia Mirkinstatic inline union tile_address
132f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwelltile_address( unsigned x,
133dc8fc39adac79cb8cbf59fc7ae5536536dceb52aDave Airlie              unsigned y, unsigned layer )
134f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell{
135f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell   union tile_address addr;
136f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell
137f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell   addr.value = 0;
138f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell   addr.bits.x = x / TILE_SIZE;
139f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell   addr.bits.y = y / TILE_SIZE;
140dc8fc39adac79cb8cbf59fc7ae5536536dceb52aDave Airlie   addr.bits.layer = layer;
141f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell   return addr;
142f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell}
143f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell
144f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell/* Quickly retrieve tile if it matches last lookup.
145f911c3b9897b90132c8621a72bfeb824eb3b01e5Keith Whitwell */
146a2a1a5805fd617e7f3cc8be44dd79b50da07ebb9Ilia Mirkinstatic inline struct softpipe_cached_tile *
14719097907ef042b97bbbda39b34bf3212f4cf154aKeith Whitwellsp_get_cached_tile(struct softpipe_tile_cache *tc,
148dc8fc39adac79cb8cbf59fc7ae5536536dceb52aDave Airlie                   int x, int y, int layer )
14919097907ef042b97bbbda39b34bf3212f4cf154aKeith Whitwell{
150dc8fc39adac79cb8cbf59fc7ae5536536dceb52aDave Airlie   union tile_address addr = tile_address( x, y, layer );
15119097907ef042b97bbbda39b34bf3212f4cf154aKeith Whitwell
152029c099b54b24a4ecbe63f5fbe2df6c91da79b63Brian Paul   if (tc->last_tile_addr.value == addr.value)
15319097907ef042b97bbbda39b34bf3212f4cf154aKeith Whitwell      return tc->last_tile;
15419097907ef042b97bbbda39b34bf3212f4cf154aKeith Whitwell
15519097907ef042b97bbbda39b34bf3212f4cf154aKeith Whitwell   return sp_find_cached_tile( tc, addr );
15619097907ef042b97bbbda39b34bf3212f4cf154aKeith Whitwell}
15719097907ef042b97bbbda39b34bf3212f4cf154aKeith Whitwell
15819097907ef042b97bbbda39b34bf3212f4cf154aKeith Whitwell
159b3204c2aff3f3d442ada04f241f352155a3af205Brian Paul
16046c3cf18315345effd15a69987294c1195843e2aBrian Paul
16146c3cf18315345effd15a69987294c1195843e2aBrian Paul#endif /* SP_TILE_CACHE_H */
16246c3cf18315345effd15a69987294c1195843e2aBrian Paul
163