1848b8605Smrg/**************************************************************************
2848b8605Smrg *
3848b8605Smrg * Copyright 2007 VMware, Inc.
4848b8605Smrg * All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the
8848b8605Smrg * "Software"), to deal in the Software without restriction, including
9848b8605Smrg * without limitation the rights to use, copy, modify, merge, publish,
10848b8605Smrg * distribute, sub license, and/or sell copies of the Software, and to
11848b8605Smrg * permit persons to whom the Software is furnished to do so, subject to
12848b8605Smrg * the following conditions:
13848b8605Smrg *
14848b8605Smrg * The above copyright notice and this permission notice (including the
15848b8605Smrg * next paragraph) shall be included in all copies or substantial portions
16848b8605Smrg * of the Software.
17848b8605Smrg *
18848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20848b8605Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21848b8605Smrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22848b8605Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23848b8605Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24848b8605Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25848b8605Smrg *
26848b8605Smrg **************************************************************************/
27848b8605Smrg
28848b8605Smrg#ifndef LP_TEXTURE_H
29848b8605Smrg#define LP_TEXTURE_H
30848b8605Smrg
31848b8605Smrg
32848b8605Smrg#include "pipe/p_state.h"
33848b8605Smrg#include "util/u_debug.h"
34848b8605Smrg#include "lp_limits.h"
35848b8605Smrg
36848b8605Smrg
37848b8605Smrgenum lp_texture_usage
38848b8605Smrg{
39848b8605Smrg   LP_TEX_USAGE_READ = 100,
40848b8605Smrg   LP_TEX_USAGE_READ_WRITE,
41848b8605Smrg   LP_TEX_USAGE_WRITE_ALL
42848b8605Smrg};
43848b8605Smrg
44848b8605Smrg
45848b8605Smrgstruct pipe_context;
46848b8605Smrgstruct pipe_screen;
47848b8605Smrgstruct llvmpipe_context;
48848b8605Smrg
49848b8605Smrgstruct sw_displaytarget;
50848b8605Smrg
51848b8605Smrg
52848b8605Smrg/**
53848b8605Smrg * llvmpipe subclass of pipe_resource.  A texture, drawing surface,
54848b8605Smrg * vertex buffer, const buffer, etc.
55848b8605Smrg * Textures are stored differently than other types of objects such as
56848b8605Smrg * vertex buffers and const buffers.
57848b8605Smrg * The latter are simple malloc'd blocks of memory.
58848b8605Smrg */
59848b8605Smrgstruct llvmpipe_resource
60848b8605Smrg{
61848b8605Smrg   struct pipe_resource base;
62848b8605Smrg
63848b8605Smrg   /** Row stride in bytes */
64848b8605Smrg   unsigned row_stride[LP_MAX_TEXTURE_LEVELS];
65848b8605Smrg   /** Image stride (for cube maps, array or 3D textures) in bytes */
66848b8605Smrg   unsigned img_stride[LP_MAX_TEXTURE_LEVELS];
67848b8605Smrg   /** Offset to start of mipmap level, in bytes */
68848b8605Smrg   unsigned mip_offsets[LP_MAX_TEXTURE_LEVELS];
69848b8605Smrg   /** allocated total size (for non-display target texture resources only) */
70848b8605Smrg   unsigned total_alloc_size;
71848b8605Smrg
72848b8605Smrg   /**
73848b8605Smrg    * Display target, for textures with the PIPE_BIND_DISPLAY_TARGET
74848b8605Smrg    * usage.
75848b8605Smrg    */
76848b8605Smrg   struct sw_displaytarget *dt;
77848b8605Smrg
78848b8605Smrg   /**
79848b8605Smrg    * Malloc'ed data for regular textures, or a mapping to dt above.
80848b8605Smrg    */
81848b8605Smrg   void *tex_data;
82848b8605Smrg
83848b8605Smrg   /**
84848b8605Smrg    * Data for non-texture resources.
85848b8605Smrg    */
86848b8605Smrg   void *data;
87848b8605Smrg
88848b8605Smrg   boolean userBuffer;  /** Is this a user-space buffer? */
89848b8605Smrg   unsigned timestamp;
90848b8605Smrg
91848b8605Smrg   unsigned id;  /**< temporary, for debugging */
92848b8605Smrg
93848b8605Smrg#ifdef DEBUG
94848b8605Smrg   /** for linked list */
95848b8605Smrg   struct llvmpipe_resource *prev, *next;
96848b8605Smrg#endif
97848b8605Smrg};
98848b8605Smrg
99848b8605Smrg
100848b8605Smrgstruct llvmpipe_transfer
101848b8605Smrg{
102848b8605Smrg   struct pipe_transfer base;
103848b8605Smrg
104848b8605Smrg   unsigned long offset;
105848b8605Smrg};
106848b8605Smrg
107848b8605Smrg
108848b8605Smrg/** cast wrappers */
109b8e80941Smrgstatic inline struct llvmpipe_resource *
110848b8605Smrgllvmpipe_resource(struct pipe_resource *pt)
111848b8605Smrg{
112848b8605Smrg   return (struct llvmpipe_resource *) pt;
113848b8605Smrg}
114848b8605Smrg
115848b8605Smrg
116b8e80941Smrgstatic inline const struct llvmpipe_resource *
117848b8605Smrgllvmpipe_resource_const(const struct pipe_resource *pt)
118848b8605Smrg{
119848b8605Smrg   return (const struct llvmpipe_resource *) pt;
120848b8605Smrg}
121848b8605Smrg
122848b8605Smrg
123b8e80941Smrgstatic inline struct llvmpipe_transfer *
124848b8605Smrgllvmpipe_transfer(struct pipe_transfer *pt)
125848b8605Smrg{
126848b8605Smrg   return (struct llvmpipe_transfer *) pt;
127848b8605Smrg}
128848b8605Smrg
129848b8605Smrg
130848b8605Smrgvoid llvmpipe_init_screen_resource_funcs(struct pipe_screen *screen);
131848b8605Smrgvoid llvmpipe_init_context_resource_funcs(struct pipe_context *pipe);
132848b8605Smrg
133848b8605Smrg
134b8e80941Smrgstatic inline boolean
135848b8605Smrgllvmpipe_resource_is_texture(const struct pipe_resource *resource)
136848b8605Smrg{
137848b8605Smrg   switch (resource->target) {
138848b8605Smrg   case PIPE_BUFFER:
139848b8605Smrg      return FALSE;
140848b8605Smrg   case PIPE_TEXTURE_1D:
141848b8605Smrg   case PIPE_TEXTURE_1D_ARRAY:
142848b8605Smrg   case PIPE_TEXTURE_2D:
143848b8605Smrg   case PIPE_TEXTURE_2D_ARRAY:
144848b8605Smrg   case PIPE_TEXTURE_RECT:
145848b8605Smrg   case PIPE_TEXTURE_3D:
146848b8605Smrg   case PIPE_TEXTURE_CUBE:
147848b8605Smrg   case PIPE_TEXTURE_CUBE_ARRAY:
148848b8605Smrg      return TRUE;
149848b8605Smrg   default:
150848b8605Smrg      assert(0);
151848b8605Smrg      return FALSE;
152848b8605Smrg   }
153848b8605Smrg}
154848b8605Smrg
155848b8605Smrg
156b8e80941Smrgstatic inline boolean
157848b8605Smrgllvmpipe_resource_is_1d(const struct pipe_resource *resource)
158848b8605Smrg{
159848b8605Smrg   switch (resource->target) {
160848b8605Smrg   case PIPE_BUFFER:
161848b8605Smrg   case PIPE_TEXTURE_1D:
162848b8605Smrg   case PIPE_TEXTURE_1D_ARRAY:
163848b8605Smrg      return TRUE;
164848b8605Smrg   case PIPE_TEXTURE_2D:
165848b8605Smrg   case PIPE_TEXTURE_2D_ARRAY:
166848b8605Smrg   case PIPE_TEXTURE_RECT:
167848b8605Smrg   case PIPE_TEXTURE_3D:
168848b8605Smrg   case PIPE_TEXTURE_CUBE:
169848b8605Smrg   case PIPE_TEXTURE_CUBE_ARRAY:
170848b8605Smrg      return FALSE;
171848b8605Smrg   default:
172848b8605Smrg      assert(0);
173848b8605Smrg      return FALSE;
174848b8605Smrg   }
175848b8605Smrg}
176848b8605Smrg
177848b8605Smrg
178b8e80941Smrgstatic inline unsigned
179848b8605Smrgllvmpipe_layer_stride(struct pipe_resource *resource,
180848b8605Smrg                      unsigned level)
181848b8605Smrg{
182848b8605Smrg   struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
183848b8605Smrg   assert(level < LP_MAX_TEXTURE_2D_LEVELS);
184848b8605Smrg   return lpr->img_stride[level];
185848b8605Smrg}
186848b8605Smrg
187848b8605Smrg
188b8e80941Smrgstatic inline unsigned
189848b8605Smrgllvmpipe_resource_stride(struct pipe_resource *resource,
190848b8605Smrg                         unsigned level)
191848b8605Smrg{
192848b8605Smrg   struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
193848b8605Smrg   assert(level < LP_MAX_TEXTURE_2D_LEVELS);
194848b8605Smrg   return lpr->row_stride[level];
195848b8605Smrg}
196848b8605Smrg
197848b8605Smrg
198848b8605Smrgvoid *
199848b8605Smrgllvmpipe_resource_map(struct pipe_resource *resource,
200848b8605Smrg                      unsigned level,
201848b8605Smrg                      unsigned layer,
202848b8605Smrg                      enum lp_texture_usage tex_usage);
203848b8605Smrg
204848b8605Smrgvoid
205848b8605Smrgllvmpipe_resource_unmap(struct pipe_resource *resource,
206848b8605Smrg                        unsigned level,
207848b8605Smrg                        unsigned layer);
208848b8605Smrg
209848b8605Smrg
210848b8605Smrgvoid *
211848b8605Smrgllvmpipe_resource_data(struct pipe_resource *resource);
212848b8605Smrg
213848b8605Smrg
214848b8605Smrgunsigned
215848b8605Smrgllvmpipe_resource_size(const struct pipe_resource *resource);
216848b8605Smrg
217848b8605Smrg
218848b8605Smrgubyte *
219848b8605Smrgllvmpipe_get_texture_image_address(struct llvmpipe_resource *lpr,
220848b8605Smrg                                   unsigned face_slice, unsigned level);
221848b8605Smrg
222848b8605Smrg
223848b8605Smrgextern void
224848b8605Smrgllvmpipe_print_resources(void);
225848b8605Smrg
226848b8605Smrg
227848b8605Smrg#define LP_UNREFERENCED         0
228848b8605Smrg#define LP_REFERENCED_FOR_READ  (1 << 0)
229848b8605Smrg#define LP_REFERENCED_FOR_WRITE (1 << 1)
230848b8605Smrg
231848b8605Smrgunsigned int
232848b8605Smrgllvmpipe_is_resource_referenced( struct pipe_context *pipe,
233848b8605Smrg                                 struct pipe_resource *presource,
234848b8605Smrg                                 unsigned level);
235848b8605Smrg
236848b8605Smrgunsigned
237848b8605Smrgllvmpipe_get_format_alignment(enum pipe_format format);
238848b8605Smrg
239848b8605Smrg#endif /* LP_TEXTURE_H */
240