1848b8605Smrg/**************************************************************************
2b8e80941Smrg *
3848b8605Smrg * Copyright 2007 VMware, Inc.
4848b8605Smrg * All Rights Reserved.
5b8e80941Smrg *
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:
13b8e80941Smrg *
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.
17b8e80941Smrg *
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.
25b8e80941Smrg *
26848b8605Smrg **************************************************************************/
27848b8605Smrg
28848b8605Smrg#ifndef ST_TEXTURE_H
29848b8605Smrg#define ST_TEXTURE_H
30848b8605Smrg
31848b8605Smrg
32848b8605Smrg#include "pipe/p_context.h"
33848b8605Smrg#include "util/u_sampler.h"
34b8e80941Smrg#include "util/simple_mtx.h"
35848b8605Smrg
36848b8605Smrg#include "main/mtypes.h"
37848b8605Smrg
38848b8605Smrg
39848b8605Smrgstruct pipe_resource;
40848b8605Smrg
41848b8605Smrg
42b8e80941Smrgstruct st_texture_image_transfer
43b8e80941Smrg{
44848b8605Smrg   struct pipe_transfer *transfer;
45848b8605Smrg
46b8e80941Smrg   /* For compressed texture fallback. */
47b8e80941Smrg   GLubyte *temp_data; /**< Temporary compressed texture storage. */
48b8e80941Smrg   unsigned temp_stride; /**< Stride of the compressed texture storage. */
49848b8605Smrg   GLubyte *map; /**< Saved map pointer of the uncompressed transfer. */
50848b8605Smrg};
51848b8605Smrg
52848b8605Smrg
53b8e80941Smrg/**
54b8e80941Smrg * Container for one context's validated sampler view.
55b8e80941Smrg */
56b8e80941Smrgstruct st_sampler_view
57b8e80941Smrg{
58b8e80941Smrg   struct pipe_sampler_view *view;
59b8e80941Smrg
60b8e80941Smrg   /** The context which created this view */
61b8e80941Smrg   struct st_context *st;
62b8e80941Smrg
63b8e80941Smrg   /** The glsl version of the shader seen during validation */
64b8e80941Smrg   bool glsl130_or_later;
65b8e80941Smrg   /** Derived from the sampler's sRGBDecode state during validation */
66b8e80941Smrg   bool srgb_skip_decode;
67b8e80941Smrg};
68b8e80941Smrg
69b8e80941Smrg
70b8e80941Smrg/**
71b8e80941Smrg * Container for per-context sampler views of a texture.
72b8e80941Smrg */
73b8e80941Smrgstruct st_sampler_views
74b8e80941Smrg{
75b8e80941Smrg   struct st_sampler_views *next;
76b8e80941Smrg   uint32_t max;
77b8e80941Smrg   uint32_t count;
78b8e80941Smrg   struct st_sampler_view views[0];
79b8e80941Smrg};
80b8e80941Smrg
81b8e80941Smrg
82848b8605Smrg/**
83848b8605Smrg * Subclass of gl_texure_image.
84848b8605Smrg */
85848b8605Smrgstruct st_texture_image
86848b8605Smrg{
87848b8605Smrg   struct gl_texture_image base;
88848b8605Smrg
89848b8605Smrg   /* If stImage->pt != NULL, image data is stored here.
90848b8605Smrg    * Else there is no image data.
91848b8605Smrg    */
92848b8605Smrg   struct pipe_resource *pt;
93848b8605Smrg
94848b8605Smrg   /* List of transfers, allocated on demand.
95848b8605Smrg    * transfer[layer] is a mapping for that layer.
96848b8605Smrg    */
97848b8605Smrg   struct st_texture_image_transfer *transfer;
98848b8605Smrg   unsigned num_transfers;
99b8e80941Smrg
100b8e80941Smrg   /* For compressed images unsupported by the driver. Keep track of
101b8e80941Smrg    * the original data. This is necessary for mapping/unmapping,
102b8e80941Smrg    * as well as image copies.
103b8e80941Smrg    */
104b8e80941Smrg   GLubyte *compressed_data;
105848b8605Smrg};
106848b8605Smrg
107848b8605Smrg
108848b8605Smrg/**
109848b8605Smrg * Subclass of gl_texure_object.
110848b8605Smrg */
111848b8605Smrgstruct st_texture_object
112848b8605Smrg{
113848b8605Smrg   struct gl_texture_object base;       /* The "parent" object */
114848b8605Smrg
115848b8605Smrg   /* The texture must include at levels [0..lastLevel] once validated:
116848b8605Smrg    */
117848b8605Smrg   GLuint lastLevel;
118848b8605Smrg
119b8e80941Smrg   unsigned int validated_first_level;
120b8e80941Smrg   unsigned int validated_last_level;
121848b8605Smrg
122848b8605Smrg   /* On validation any active images held in main memory or in other
123848b8605Smrg    * textures will be copied to this texture and the old storage freed.
124848b8605Smrg    */
125848b8605Smrg   struct pipe_resource *pt;
126848b8605Smrg
127b8e80941Smrg   /* Protect modifications of the sampler_views array */
128b8e80941Smrg   simple_mtx_t validate_mutex;
129848b8605Smrg
130b8e80941Smrg   /* Container of sampler views (one per context) attached to this texture
131848b8605Smrg    * object. Created lazily on first binding in context.
132b8e80941Smrg    *
133b8e80941Smrg    * Purely read-only accesses to the current context's own sampler view
134b8e80941Smrg    * require no locking. Another thread may simultaneously replace the
135b8e80941Smrg    * container object in order to grow the array, but the old container will
136b8e80941Smrg    * be kept alive.
137b8e80941Smrg    *
138b8e80941Smrg    * Writing to the container (even for modifying the current context's own
139b8e80941Smrg    * sampler view) always requires taking the validate_mutex to protect against
140b8e80941Smrg    * concurrent container switches.
141b8e80941Smrg    *
142b8e80941Smrg    * NULL'ing another context's sampler view is allowed only while
143b8e80941Smrg    * implementing an API call that modifies the texture: an application which
144b8e80941Smrg    * calls those while simultaneously reading the texture in another context
145b8e80941Smrg    * invokes undefined behavior. (TODO: a dubious violation of this rule is
146b8e80941Smrg    * st_finalize_texture, which is a lazy operation that corresponds to a
147b8e80941Smrg    * texture modification.)
148848b8605Smrg    */
149b8e80941Smrg   struct st_sampler_views *sampler_views;
150b8e80941Smrg
151b8e80941Smrg   /* Old sampler views container objects that have not been freed yet because
152b8e80941Smrg    * other threads/contexts may still be reading from them.
153b8e80941Smrg    */
154b8e80941Smrg   struct st_sampler_views *sampler_views_old;
155848b8605Smrg
156848b8605Smrg   /* True if this texture comes from the window system. Such a texture
157848b8605Smrg    * cannot be reallocated and the format can only be changed with a sampler
158848b8605Smrg    * view or a surface.
159848b8605Smrg    */
160848b8605Smrg   GLboolean surface_based;
161848b8605Smrg
162848b8605Smrg   /* If surface_based is true, this format should be used for all sampler
163848b8605Smrg    * views and surfaces instead of pt->format.
164848b8605Smrg    */
165848b8605Smrg   enum pipe_format surface_format;
166b8e80941Smrg
167b8e80941Smrg   /* When non-zero, samplers should use this level instead of the level
168b8e80941Smrg    * range specified by the GL state.
169b8e80941Smrg    *
170b8e80941Smrg    * This is used for EGL images, which may correspond to a single level out
171b8e80941Smrg    * of an imported pipe_resources with multiple mip levels.
172b8e80941Smrg    */
173b8e80941Smrg   uint level_override;
174b8e80941Smrg
175b8e80941Smrg   /* When non-zero, samplers should use this layer instead of the one
176b8e80941Smrg    * specified by the GL state.
177b8e80941Smrg    *
178b8e80941Smrg    * This is used for EGL images and VDPAU interop, where imported
179b8e80941Smrg    * pipe_resources may be cube, 3D, or array textures (containing layers
180b8e80941Smrg    * with different fields in the case of VDPAU) even though the GL state
181b8e80941Smrg    * describes one non-array texture per field.
182b8e80941Smrg    */
183b8e80941Smrg   uint layer_override;
184b8e80941Smrg
185b8e80941Smrg    /**
186b8e80941Smrg     * Set when the texture images of this texture object might not all be in
187b8e80941Smrg     * the pipe_resource *pt above.
188b8e80941Smrg     */
189b8e80941Smrg    bool needs_validation;
190848b8605Smrg};
191848b8605Smrg
192848b8605Smrg
193b8e80941Smrgstatic inline struct st_texture_image *
194848b8605Smrgst_texture_image(struct gl_texture_image *img)
195848b8605Smrg{
196848b8605Smrg   return (struct st_texture_image *) img;
197848b8605Smrg}
198848b8605Smrg
199b8e80941Smrgstatic inline const struct st_texture_image *
200b8e80941Smrgst_texture_image_const(const struct gl_texture_image *img)
201b8e80941Smrg{
202b8e80941Smrg   return (const struct st_texture_image *) img;
203b8e80941Smrg}
204b8e80941Smrg
205b8e80941Smrgstatic inline struct st_texture_object *
206848b8605Smrgst_texture_object(struct gl_texture_object *obj)
207848b8605Smrg{
208848b8605Smrg   return (struct st_texture_object *) obj;
209848b8605Smrg}
210848b8605Smrg
211b8e80941Smrgstatic inline const struct st_texture_object *
212848b8605Smrgst_texture_object_const(const struct gl_texture_object *obj)
213848b8605Smrg{
214848b8605Smrg   return (const struct st_texture_object *) obj;
215848b8605Smrg}
216848b8605Smrg
217848b8605Smrg
218b8e80941Smrgstatic inline struct pipe_resource *
219848b8605Smrgst_get_texobj_resource(struct gl_texture_object *texObj)
220848b8605Smrg{
221848b8605Smrg   struct st_texture_object *stObj = st_texture_object(texObj);
222848b8605Smrg   return stObj ? stObj->pt : NULL;
223848b8605Smrg}
224848b8605Smrg
225848b8605Smrg
226b8e80941Smrgstatic inline struct pipe_resource *
227848b8605Smrgst_get_stobj_resource(struct st_texture_object *stObj)
228848b8605Smrg{
229848b8605Smrg   return stObj ? stObj->pt : NULL;
230848b8605Smrg}
231848b8605Smrg
232848b8605Smrg
233b8e80941Smrgstatic inline struct st_texture_object *
234b8e80941Smrgst_get_texture_object(struct gl_context *ctx,
235b8e80941Smrg                      const struct gl_program *prog,
236b8e80941Smrg                      unsigned unit)
237848b8605Smrg{
238b8e80941Smrg   const GLuint texUnit = prog->SamplerUnits[unit];
239b8e80941Smrg   struct gl_texture_object *texObj = ctx->Texture.Unit[texUnit]._Current;
240848b8605Smrg
241b8e80941Smrg   if (!texObj)
242b8e80941Smrg      return NULL;
243848b8605Smrg
244b8e80941Smrg   return st_texture_object(texObj);
245848b8605Smrg}
246848b8605Smrg
247b8e80941Smrgstatic inline enum pipe_format
248b8e80941Smrgst_get_view_format(struct st_texture_object *stObj)
249848b8605Smrg{
250b8e80941Smrg   if (!stObj)
251b8e80941Smrg      return PIPE_FORMAT_NONE;
252b8e80941Smrg   return stObj->surface_based ? stObj->surface_format : stObj->pt->format;
253848b8605Smrg}
254848b8605Smrg
255848b8605Smrg
256848b8605Smrgextern struct pipe_resource *
257848b8605Smrgst_texture_create(struct st_context *st,
258848b8605Smrg                  enum pipe_texture_target target,
259b8e80941Smrg                  enum pipe_format format,
260848b8605Smrg                  GLuint last_level,
261848b8605Smrg                  GLuint width0,
262848b8605Smrg                  GLuint height0,
263848b8605Smrg                  GLuint depth0,
264848b8605Smrg                  GLuint layers,
265848b8605Smrg                  GLuint nr_samples,
266848b8605Smrg                  GLuint tex_usage );
267848b8605Smrg
268848b8605Smrg
269848b8605Smrgextern void
270848b8605Smrgst_gl_texture_dims_to_pipe_dims(GLenum texture,
271b8e80941Smrg                                unsigned widthIn,
272b8e80941Smrg                                uint16_t heightIn,
273b8e80941Smrg                                uint16_t depthIn,
274b8e80941Smrg                                unsigned *widthOut,
275b8e80941Smrg                                uint16_t *heightOut,
276b8e80941Smrg                                uint16_t *depthOut,
277b8e80941Smrg                                uint16_t *layersOut);
278848b8605Smrg
279848b8605Smrg/* Check if an image fits into an existing texture object.
280848b8605Smrg */
281848b8605Smrgextern GLboolean
282848b8605Smrgst_texture_match_image(struct st_context *st,
283848b8605Smrg                       const struct pipe_resource *pt,
284848b8605Smrg                       const struct gl_texture_image *image);
285848b8605Smrg
286848b8605Smrg/* Return a pointer to an image within a texture.  Return image stride as
287848b8605Smrg * well.
288848b8605Smrg */
289848b8605Smrgextern GLubyte *
290848b8605Smrgst_texture_image_map(struct st_context *st, struct st_texture_image *stImage,
291848b8605Smrg                     enum pipe_transfer_usage usage,
292848b8605Smrg                     GLuint x, GLuint y, GLuint z,
293848b8605Smrg                     GLuint w, GLuint h, GLuint d,
294848b8605Smrg                     struct pipe_transfer **transfer);
295848b8605Smrg
296848b8605Smrgextern void
297848b8605Smrgst_texture_image_unmap(struct st_context *st,
298848b8605Smrg                       struct st_texture_image *stImage, unsigned slice);
299848b8605Smrg
300848b8605Smrg
301848b8605Smrg/* Return pointers to each 2d slice within an image.  Indexed by depth
302848b8605Smrg * value.
303848b8605Smrg */
304848b8605Smrgextern const GLuint *
305848b8605Smrgst_texture_depth_offsets(struct pipe_resource *pt, GLuint level);
306848b8605Smrg
307848b8605Smrg/* Copy an image between two textures
308848b8605Smrg */
309848b8605Smrgextern void
310848b8605Smrgst_texture_image_copy(struct pipe_context *pipe,
311848b8605Smrg                      struct pipe_resource *dst, GLuint dstLevel,
312848b8605Smrg                      struct pipe_resource *src, GLuint srcLevel,
313848b8605Smrg                      GLuint face);
314848b8605Smrg
315848b8605Smrg
316848b8605Smrgextern struct pipe_resource *
317848b8605Smrgst_create_color_map_texture(struct gl_context *ctx);
318848b8605Smrg
319b8e80941Smrgvoid
320b8e80941Smrgst_destroy_bound_texture_handles(struct st_context *st);
321848b8605Smrg
322b8e80941Smrgvoid
323b8e80941Smrgst_destroy_bound_image_handles(struct st_context *st);
324848b8605Smrg
325b8e80941Smrgbool
326b8e80941Smrgst_compressed_format_fallback(struct st_context *st, mesa_format format);
327b8e80941Smrg
328b8e80941Smrgvoid
329b8e80941Smrgst_convert_image(const struct st_context *st, const struct gl_image_unit *u,
330b8e80941Smrg                 struct pipe_image_view *img, unsigned shader_access);
331b8e80941Smrg
332b8e80941Smrgvoid
333b8e80941Smrgst_convert_image_from_unit(const struct st_context *st,
334b8e80941Smrg                           struct pipe_image_view *img,
335b8e80941Smrg                           GLuint imgUnit,
336b8e80941Smrg                           unsigned shader_access);
337b8e80941Smrg
338b8e80941Smrgvoid
339b8e80941Smrgst_convert_sampler(const struct st_context *st,
340b8e80941Smrg                   const struct gl_texture_object *texobj,
341b8e80941Smrg                   const struct gl_sampler_object *msamp,
342b8e80941Smrg                   float tex_unit_lod_bias,
343b8e80941Smrg                   struct pipe_sampler_state *sampler);
344b8e80941Smrg
345b8e80941Smrgvoid
346b8e80941Smrgst_convert_sampler_from_unit(const struct st_context *st,
347b8e80941Smrg                             struct pipe_sampler_state *sampler,
348b8e80941Smrg                             GLuint texUnit);
349b8e80941Smrg
350b8e80941Smrgvoid
351b8e80941Smrgst_update_single_texture(struct st_context *st,
352b8e80941Smrg                         struct pipe_sampler_view **sampler_view,
353b8e80941Smrg                         GLuint texUnit, bool glsl130_or_later,
354b8e80941Smrg                         bool ignore_srgb_decode);
355b8e80941Smrg
356b8e80941Smrgvoid
357b8e80941Smrgst_make_bound_samplers_resident(struct st_context *st,
358b8e80941Smrg                                struct gl_program *prog);
359848b8605Smrg
360848b8605Smrgvoid
361b8e80941Smrgst_make_bound_images_resident(struct st_context *st,
362b8e80941Smrg                              struct gl_program *prog);
363848b8605Smrg
364848b8605Smrg#endif
365