14a49301eSmrg/**************************************************************************
2b9abf16eSmaya *
3af69d88dSmrg * Copyright 2007 VMware, Inc.
44a49301eSmrg * All Rights Reserved.
5b9abf16eSmaya *
64a49301eSmrg * Permission is hereby granted, free of charge, to any person obtaining a
74a49301eSmrg * copy of this software and associated documentation files (the
84a49301eSmrg * "Software"), to deal in the Software without restriction, including
94a49301eSmrg * without limitation the rights to use, copy, modify, merge, publish,
104a49301eSmrg * distribute, sub license, and/or sell copies of the Software, and to
114a49301eSmrg * permit persons to whom the Software is furnished to do so, subject to
124a49301eSmrg * the following conditions:
13b9abf16eSmaya *
144a49301eSmrg * The above copyright notice and this permission notice (including the
154a49301eSmrg * next paragraph) shall be included in all copies or substantial portions
164a49301eSmrg * of the Software.
17b9abf16eSmaya *
184a49301eSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
194a49301eSmrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
204a49301eSmrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21af69d88dSmrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
224a49301eSmrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
234a49301eSmrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
244a49301eSmrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25b9abf16eSmaya *
264a49301eSmrg **************************************************************************/
274a49301eSmrg
284a49301eSmrg#ifndef ST_TEXTURE_H
294a49301eSmrg#define ST_TEXTURE_H
304a49301eSmrg
314a49301eSmrg
323464ebd5Sriastradh#include "pipe/p_context.h"
333464ebd5Sriastradh#include "util/u_sampler.h"
3401e04c3fSmrg#include "util/simple_mtx.h"
353464ebd5Sriastradh
364a49301eSmrg#include "main/mtypes.h"
374a49301eSmrg
383464ebd5Sriastradh
393464ebd5Sriastradhstruct pipe_resource;
404a49301eSmrg
414a49301eSmrg
42b9abf16eSmayastruct st_texture_image_transfer
43b9abf16eSmaya{
44af69d88dSmrg   struct pipe_transfer *transfer;
45af69d88dSmrg
4601e04c3fSmrg   /* For compressed texture fallback. */
4701e04c3fSmrg   GLubyte *temp_data; /**< Temporary compressed texture storage. */
4801e04c3fSmrg   unsigned temp_stride; /**< Stride of the compressed texture storage. */
49af69d88dSmrg   GLubyte *map; /**< Saved map pointer of the uncompressed transfer. */
50af69d88dSmrg};
51af69d88dSmrg
52af69d88dSmrg
5301e04c3fSmrg/**
5401e04c3fSmrg * Container for one context's validated sampler view.
5501e04c3fSmrg */
56b9abf16eSmayastruct st_sampler_view
57b9abf16eSmaya{
5801e04c3fSmrg   struct pipe_sampler_view *view;
5901e04c3fSmrg
60b9abf16eSmaya   /** The context which created this view */
61b9abf16eSmaya   struct st_context *st;
62b9abf16eSmaya
6301e04c3fSmrg   /** The glsl version of the shader seen during validation */
6401e04c3fSmrg   bool glsl130_or_later;
6501e04c3fSmrg   /** Derived from the sampler's sRGBDecode state during validation */
6601e04c3fSmrg   bool srgb_skip_decode;
677ec681f3Smrg
687ec681f3Smrg   /* This mechanism allows passing sampler view references to the driver
697ec681f3Smrg    * without using atomics to increase the reference count.
707ec681f3Smrg    *
717ec681f3Smrg    * This private refcount can be decremented without atomics but only one
727ec681f3Smrg    * context (st above) can use this counter (so that it's only used by
737ec681f3Smrg    * 1 thread).
747ec681f3Smrg    *
757ec681f3Smrg    * This number is atomically added to view->reference.count at
767ec681f3Smrg    * initialization. If it's never used, the same number is atomically
777ec681f3Smrg    * subtracted from view->reference.count before destruction. If this
787ec681f3Smrg    * number is decremented, we can pass one reference to the driver without
797ec681f3Smrg    * touching reference.count with atomics. At destruction we only subtract
807ec681f3Smrg    * the number of references we have not returned. This can possibly turn
817ec681f3Smrg    * a million atomic increments into 1 add and 1 subtract atomic op over
827ec681f3Smrg    * the whole lifetime of an app.
837ec681f3Smrg    */
847ec681f3Smrg   int private_refcount;
8501e04c3fSmrg};
8601e04c3fSmrg
8701e04c3fSmrg
8801e04c3fSmrg/**
8901e04c3fSmrg * Container for per-context sampler views of a texture.
9001e04c3fSmrg */
91b9abf16eSmayastruct st_sampler_views
92b9abf16eSmaya{
9301e04c3fSmrg   struct st_sampler_views *next;
9401e04c3fSmrg   uint32_t max;
9501e04c3fSmrg   uint32_t count;
9601e04c3fSmrg   struct st_sampler_view views[0];
9701e04c3fSmrg};
9801e04c3fSmrg
997ec681f3Smrgstruct st_compressed_data
1007ec681f3Smrg{
1017ec681f3Smrg   struct pipe_reference reference;
1027ec681f3Smrg   GLubyte *ptr;
1037ec681f3Smrg};
1047ec681f3Smrg
105b9abf16eSmaya
1063464ebd5Sriastradh/**
1073464ebd5Sriastradh * Subclass of gl_texure_image.
1083464ebd5Sriastradh */
1094a49301eSmrgstruct st_texture_image
1104a49301eSmrg{
1114a49301eSmrg   struct gl_texture_image base;
1124a49301eSmrg
1134a49301eSmrg   /* If stImage->pt != NULL, image data is stored here.
1144a49301eSmrg    * Else there is no image data.
1154a49301eSmrg    */
1163464ebd5Sriastradh   struct pipe_resource *pt;
1174a49301eSmrg
118af69d88dSmrg   /* List of transfers, allocated on demand.
119af69d88dSmrg    * transfer[layer] is a mapping for that layer.
120af69d88dSmrg    */
121af69d88dSmrg   struct st_texture_image_transfer *transfer;
122af69d88dSmrg   unsigned num_transfers;
12301e04c3fSmrg
12401e04c3fSmrg   /* For compressed images unsupported by the driver. Keep track of
12501e04c3fSmrg    * the original data. This is necessary for mapping/unmapping,
12601e04c3fSmrg    * as well as image copies.
12701e04c3fSmrg    */
1287ec681f3Smrg   struct st_compressed_data* compressed_data;
1294a49301eSmrg};
1304a49301eSmrg
1314a49301eSmrg
1323464ebd5Sriastradh/**
1333464ebd5Sriastradh * Subclass of gl_texure_object.
1343464ebd5Sriastradh */
1354a49301eSmrgstruct st_texture_object
1364a49301eSmrg{
1374a49301eSmrg   struct gl_texture_object base;       /* The "parent" object */
1384a49301eSmrg
1394a49301eSmrg   /* The texture must include at levels [0..lastLevel] once validated:
1404a49301eSmrg    */
1414a49301eSmrg   GLuint lastLevel;
1424a49301eSmrg
14301e04c3fSmrg   unsigned int validated_first_level;
14401e04c3fSmrg   unsigned int validated_last_level;
1453464ebd5Sriastradh
1464a49301eSmrg   /* On validation any active images held in main memory or in other
1474a49301eSmrg    * textures will be copied to this texture and the old storage freed.
1484a49301eSmrg    */
1493464ebd5Sriastradh   struct pipe_resource *pt;
1504a49301eSmrg
15101e04c3fSmrg   /* Protect modifications of the sampler_views array */
15201e04c3fSmrg   simple_mtx_t validate_mutex;
153af69d88dSmrg
15401e04c3fSmrg   /* Container of sampler views (one per context) attached to this texture
155af69d88dSmrg    * object. Created lazily on first binding in context.
15601e04c3fSmrg    *
15701e04c3fSmrg    * Purely read-only accesses to the current context's own sampler view
15801e04c3fSmrg    * require no locking. Another thread may simultaneously replace the
15901e04c3fSmrg    * container object in order to grow the array, but the old container will
16001e04c3fSmrg    * be kept alive.
16101e04c3fSmrg    *
16201e04c3fSmrg    * Writing to the container (even for modifying the current context's own
16301e04c3fSmrg    * sampler view) always requires taking the validate_mutex to protect against
16401e04c3fSmrg    * concurrent container switches.
16501e04c3fSmrg    *
16601e04c3fSmrg    * NULL'ing another context's sampler view is allowed only while
16701e04c3fSmrg    * implementing an API call that modifies the texture: an application which
16801e04c3fSmrg    * calls those while simultaneously reading the texture in another context
16901e04c3fSmrg    * invokes undefined behavior. (TODO: a dubious violation of this rule is
17001e04c3fSmrg    * st_finalize_texture, which is a lazy operation that corresponds to a
17101e04c3fSmrg    * texture modification.)
1723464ebd5Sriastradh    */
17301e04c3fSmrg   struct st_sampler_views *sampler_views;
17401e04c3fSmrg
17501e04c3fSmrg   /* Old sampler views container objects that have not been freed yet because
17601e04c3fSmrg    * other threads/contexts may still be reading from them.
17701e04c3fSmrg    */
17801e04c3fSmrg   struct st_sampler_views *sampler_views_old;
1794a49301eSmrg
180af69d88dSmrg   /* True if this texture comes from the window system. Such a texture
181af69d88dSmrg    * cannot be reallocated and the format can only be changed with a sampler
182af69d88dSmrg    * view or a surface.
1834a49301eSmrg    */
1844a49301eSmrg   GLboolean surface_based;
185af69d88dSmrg
186af69d88dSmrg   /* If surface_based is true, this format should be used for all sampler
187af69d88dSmrg    * views and surfaces instead of pt->format.
188af69d88dSmrg    */
189af69d88dSmrg   enum pipe_format surface_format;
19001e04c3fSmrg
1917ec681f3Smrg   /* When non-negative, samplers should use this level instead of the level
19201e04c3fSmrg    * range specified by the GL state.
19301e04c3fSmrg    *
19401e04c3fSmrg    * This is used for EGL images, which may correspond to a single level out
19501e04c3fSmrg    * of an imported pipe_resources with multiple mip levels.
19601e04c3fSmrg    */
1977ec681f3Smrg   int level_override;
19801e04c3fSmrg
1997ec681f3Smrg   /* When non-negative, samplers should use this layer instead of the one
20001e04c3fSmrg    * specified by the GL state.
20101e04c3fSmrg    *
20201e04c3fSmrg    * This is used for EGL images and VDPAU interop, where imported
20301e04c3fSmrg    * pipe_resources may be cube, 3D, or array textures (containing layers
20401e04c3fSmrg    * with different fields in the case of VDPAU) even though the GL state
20501e04c3fSmrg    * describes one non-array texture per field.
20601e04c3fSmrg    */
2077ec681f3Smrg   int layer_override;
20801e04c3fSmrg
20901e04c3fSmrg    /**
21001e04c3fSmrg     * Set when the texture images of this texture object might not all be in
21101e04c3fSmrg     * the pipe_resource *pt above.
21201e04c3fSmrg     */
21301e04c3fSmrg    bool needs_validation;
2144a49301eSmrg};
2154a49301eSmrg
2164a49301eSmrg
21701e04c3fSmrgstatic inline struct st_texture_image *
2184a49301eSmrgst_texture_image(struct gl_texture_image *img)
2194a49301eSmrg{
2204a49301eSmrg   return (struct st_texture_image *) img;
2214a49301eSmrg}
2224a49301eSmrg
22301e04c3fSmrgstatic inline const struct st_texture_image *
22401e04c3fSmrgst_texture_image_const(const struct gl_texture_image *img)
22501e04c3fSmrg{
22601e04c3fSmrg   return (const struct st_texture_image *) img;
22701e04c3fSmrg}
22801e04c3fSmrg
22901e04c3fSmrgstatic inline struct st_texture_object *
2304a49301eSmrgst_texture_object(struct gl_texture_object *obj)
2314a49301eSmrg{
2324a49301eSmrg   return (struct st_texture_object *) obj;
2334a49301eSmrg}
2344a49301eSmrg
23501e04c3fSmrgstatic inline const struct st_texture_object *
236af69d88dSmrgst_texture_object_const(const struct gl_texture_object *obj)
237af69d88dSmrg{
238af69d88dSmrg   return (const struct st_texture_object *) obj;
239af69d88dSmrg}
240af69d88dSmrg
2414a49301eSmrg
24201e04c3fSmrgstatic inline struct pipe_resource *
2433464ebd5Sriastradhst_get_texobj_resource(struct gl_texture_object *texObj)
2444a49301eSmrg{
2454a49301eSmrg   struct st_texture_object *stObj = st_texture_object(texObj);
2464a49301eSmrg   return stObj ? stObj->pt : NULL;
2474a49301eSmrg}
2484a49301eSmrg
2494a49301eSmrg
25001e04c3fSmrgstatic inline struct pipe_resource *
2513464ebd5Sriastradhst_get_stobj_resource(struct st_texture_object *stObj)
2524a49301eSmrg{
2534a49301eSmrg   return stObj ? stObj->pt : NULL;
2544a49301eSmrg}
2554a49301eSmrg
2564a49301eSmrg
25701e04c3fSmrgstatic inline struct st_texture_object *
25801e04c3fSmrgst_get_texture_object(struct gl_context *ctx,
25901e04c3fSmrg                      const struct gl_program *prog,
26001e04c3fSmrg                      unsigned unit)
2613464ebd5Sriastradh{
26201e04c3fSmrg   const GLuint texUnit = prog->SamplerUnits[unit];
26301e04c3fSmrg   struct gl_texture_object *texObj = ctx->Texture.Unit[texUnit]._Current;
2643464ebd5Sriastradh
26501e04c3fSmrg   if (!texObj)
26601e04c3fSmrg      return NULL;
2673464ebd5Sriastradh
26801e04c3fSmrg   return st_texture_object(texObj);
2693464ebd5Sriastradh}
2703464ebd5Sriastradh
27101e04c3fSmrgstatic inline enum pipe_format
27201e04c3fSmrgst_get_view_format(struct st_texture_object *stObj)
2733464ebd5Sriastradh{
27401e04c3fSmrg   if (!stObj)
27501e04c3fSmrg      return PIPE_FORMAT_NONE;
27601e04c3fSmrg   return stObj->surface_based ? stObj->surface_format : stObj->pt->format;
2773464ebd5Sriastradh}
2783464ebd5Sriastradh
2793464ebd5Sriastradh
2803464ebd5Sriastradhextern struct pipe_resource *
2814a49301eSmrgst_texture_create(struct st_context *st,
2824a49301eSmrg                  enum pipe_texture_target target,
283b9abf16eSmaya                  enum pipe_format format,
2844a49301eSmrg                  GLuint last_level,
2854a49301eSmrg                  GLuint width0,
2864a49301eSmrg                  GLuint height0,
2874a49301eSmrg                  GLuint depth0,
2883464ebd5Sriastradh                  GLuint layers,
289af69d88dSmrg                  GLuint nr_samples,
2904a49301eSmrg                  GLuint tex_usage );
2914a49301eSmrg
2924a49301eSmrg
2933464ebd5Sriastradhextern void
2943464ebd5Sriastradhst_gl_texture_dims_to_pipe_dims(GLenum texture,
29501e04c3fSmrg                                unsigned widthIn,
29601e04c3fSmrg                                uint16_t heightIn,
29701e04c3fSmrg                                uint16_t depthIn,
29801e04c3fSmrg                                unsigned *widthOut,
29901e04c3fSmrg                                uint16_t *heightOut,
30001e04c3fSmrg                                uint16_t *depthOut,
30101e04c3fSmrg                                uint16_t *layersOut);
3023464ebd5Sriastradh
3034a49301eSmrg/* Check if an image fits into an existing texture object.
3044a49301eSmrg */
3054a49301eSmrgextern GLboolean
306af69d88dSmrgst_texture_match_image(struct st_context *st,
307af69d88dSmrg                       const struct pipe_resource *pt,
308af69d88dSmrg                       const struct gl_texture_image *image);
3094a49301eSmrg
3104a49301eSmrg/* Return a pointer to an image within a texture.  Return image stride as
3114a49301eSmrg * well.
3124a49301eSmrg */
3134a49301eSmrgextern GLubyte *
314af69d88dSmrgst_texture_image_map(struct st_context *st, struct st_texture_image *stImage,
3157ec681f3Smrg                     enum pipe_map_flags usage,
316af69d88dSmrg                     GLuint x, GLuint y, GLuint z,
317af69d88dSmrg                     GLuint w, GLuint h, GLuint d,
318af69d88dSmrg                     struct pipe_transfer **transfer);
3194a49301eSmrg
3204a49301eSmrgextern void
3214a49301eSmrgst_texture_image_unmap(struct st_context *st,
322af69d88dSmrg                       struct st_texture_image *stImage, unsigned slice);
3234a49301eSmrg
3244a49301eSmrg
3254a49301eSmrg/* Return pointers to each 2d slice within an image.  Indexed by depth
3264a49301eSmrg * value.
3274a49301eSmrg */
3284a49301eSmrgextern const GLuint *
3293464ebd5Sriastradhst_texture_depth_offsets(struct pipe_resource *pt, GLuint level);
3304a49301eSmrg
3314a49301eSmrg/* Copy an image between two textures
3324a49301eSmrg */
3334a49301eSmrgextern void
3344a49301eSmrgst_texture_image_copy(struct pipe_context *pipe,
3353464ebd5Sriastradh                      struct pipe_resource *dst, GLuint dstLevel,
3363464ebd5Sriastradh                      struct pipe_resource *src, GLuint srcLevel,
3374a49301eSmrg                      GLuint face);
3384a49301eSmrg
339af69d88dSmrg
340af69d88dSmrgextern struct pipe_resource *
341af69d88dSmrgst_create_color_map_texture(struct gl_context *ctx);
342af69d88dSmrg
34301e04c3fSmrgvoid
34401e04c3fSmrgst_destroy_bound_texture_handles(struct st_context *st);
345af69d88dSmrg
34601e04c3fSmrgvoid
34701e04c3fSmrgst_destroy_bound_image_handles(struct st_context *st);
348af69d88dSmrg
3497ec681f3Smrgbool
3507ec681f3Smrgst_astc_format_fallback(const struct st_context *st, mesa_format format);
3517ec681f3Smrg
35201e04c3fSmrgbool
35301e04c3fSmrgst_compressed_format_fallback(struct st_context *st, mesa_format format);
35401e04c3fSmrg
35501e04c3fSmrgvoid
35601e04c3fSmrgst_convert_image(const struct st_context *st, const struct gl_image_unit *u,
35701e04c3fSmrg                 struct pipe_image_view *img, unsigned shader_access);
35801e04c3fSmrg
35901e04c3fSmrgvoid
36001e04c3fSmrgst_convert_image_from_unit(const struct st_context *st,
36101e04c3fSmrg                           struct pipe_image_view *img,
36201e04c3fSmrg                           GLuint imgUnit,
36301e04c3fSmrg                           unsigned shader_access);
36401e04c3fSmrg
36501e04c3fSmrgvoid
36601e04c3fSmrgst_convert_sampler(const struct st_context *st,
36701e04c3fSmrg                   const struct gl_texture_object *texobj,
36801e04c3fSmrg                   const struct gl_sampler_object *msamp,
36901e04c3fSmrg                   float tex_unit_lod_bias,
3707ec681f3Smrg                   struct pipe_sampler_state *sampler,
3717ec681f3Smrg                   bool seamless_cube_map);
37201e04c3fSmrg
37301e04c3fSmrgvoid
37401e04c3fSmrgst_convert_sampler_from_unit(const struct st_context *st,
37501e04c3fSmrg                             struct pipe_sampler_state *sampler,
37601e04c3fSmrg                             GLuint texUnit);
37701e04c3fSmrg
3787ec681f3Smrgstruct pipe_sampler_view *
37901e04c3fSmrgst_update_single_texture(struct st_context *st,
38001e04c3fSmrg                         GLuint texUnit, bool glsl130_or_later,
3817ec681f3Smrg                         bool ignore_srgb_decode, bool get_reference);
3827ec681f3Smrg
3837ec681f3Smrgunsigned
3847ec681f3Smrgst_get_sampler_views(struct st_context *st,
3857ec681f3Smrg                     enum pipe_shader_type shader_stage,
3867ec681f3Smrg                     const struct gl_program *prog,
3877ec681f3Smrg                     struct pipe_sampler_view **sampler_views);
38801e04c3fSmrg
38901e04c3fSmrgvoid
39001e04c3fSmrgst_make_bound_samplers_resident(struct st_context *st,
39101e04c3fSmrg                                struct gl_program *prog);
392af69d88dSmrg
393af69d88dSmrgvoid
39401e04c3fSmrgst_make_bound_images_resident(struct st_context *st,
39501e04c3fSmrg                              struct gl_program *prog);
396af69d88dSmrg
3974a49301eSmrg#endif
398