intel_tex.c revision 19cfe1e035fdaf03b7a3560c47f1b8d59a221902
1#include "swrast/swrast.h"
2#include "main/renderbuffer.h"
3#include "main/texobj.h"
4#include "main/teximage.h"
5#include "main/mipmap.h"
6#include "drivers/common/meta.h"
7#include "intel_context.h"
8#include "intel_mipmap_tree.h"
9#include "intel_tex.h"
10
11#define FILE_DEBUG_FLAG DEBUG_TEXTURE
12
13static struct gl_texture_image *
14intelNewTextureImage(struct gl_context * ctx)
15{
16   DBG("%s\n", __FUNCTION__);
17   (void) ctx;
18   return (struct gl_texture_image *) CALLOC_STRUCT(intel_texture_image);
19}
20
21static void
22intelDeleteTextureImage(struct gl_context * ctx, struct gl_texture_image *img)
23{
24   /* nothing special (yet) for intel_texture_image */
25   _mesa_delete_texture_image(ctx, img);
26}
27
28
29static struct gl_texture_object *
30intelNewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
31{
32   struct intel_texture_object *obj = CALLOC_STRUCT(intel_texture_object);
33
34   (void) ctx;
35
36   DBG("%s\n", __FUNCTION__);
37   _mesa_initialize_texture_object(&obj->base, name, target);
38
39   return &obj->base;
40}
41
42static void
43intelDeleteTextureObject(struct gl_context *ctx,
44			 struct gl_texture_object *texObj)
45{
46   struct intel_context *intel = intel_context(ctx);
47   struct intel_texture_object *intelObj = intel_texture_object(texObj);
48
49   intel_miptree_release(intel, &intelObj->mt);
50   _mesa_delete_texture_object(ctx, texObj);
51}
52
53
54static void
55intel_free_texture_image_buffer(struct gl_context * ctx,
56				struct gl_texture_image *texImage)
57{
58   struct intel_context *intel = intel_context(ctx);
59   struct intel_texture_image *intelImage = intel_texture_image(texImage);
60
61   DBG("%s\n", __FUNCTION__);
62
63   intel_miptree_release(intel, &intelImage->mt);
64
65   if (texImage->Data) {
66      _mesa_free_texmemory(texImage->Data);
67      texImage->Data = NULL;
68   }
69
70   _mesa_reference_renderbuffer(&intelImage->depth_rb, NULL);
71   _mesa_reference_renderbuffer(&intelImage->stencil_rb, NULL);
72}
73
74/**
75 * Map texture memory/buffer into user space.
76 * Note: the region of interest parameters are ignored here.
77 * \param mapOut  returns start of mapping of region of interest
78 * \param rowStrideOut  returns row stride in bytes
79 */
80static void
81intel_map_texture_image(struct gl_context *ctx,
82			struct gl_texture_image *tex_image,
83			GLuint slice,
84			GLuint x, GLuint y, GLuint w, GLuint h,
85			GLbitfield mode,
86			GLubyte **map,
87			GLint *stride)
88{
89   struct intel_context *intel = intel_context(ctx);
90   struct intel_texture_image *intel_image = intel_texture_image(tex_image);
91   struct intel_mipmap_tree *mt = intel_image->mt;
92   unsigned int bw, bh;
93
94   if (intel_image->stencil_rb) {
95      /*
96       * The texture has packed depth/stencil format, but uses separate
97       * stencil. The texture's embedded stencil buffer contains the real
98       * stencil data, so copy that into the miptree.
99       */
100      intel_tex_image_s8z24_gather(intel, intel_image);
101   }
102
103   /* For compressed formats, the stride is the number of bytes per
104    * row of blocks.  intel_miptree_get_image_offset() already does
105    * the divide.
106    */
107   _mesa_get_format_block_size(tex_image->TexFormat, &bw, &bh);
108   assert(y % bh == 0);
109   y /= bh;
110
111   if (likely(mt)) {
112      void *base = intel_region_map(intel, mt->region);
113      unsigned int image_x, image_y;
114
115      intel_miptree_get_image_offset(mt, tex_image->Level, tex_image->Face,
116				     slice, &image_x, &image_y);
117      x += image_x;
118      y += image_y;
119
120      *stride = mt->region->pitch * mt->cpp;
121      *map = base + y * *stride + x * mt->cpp;
122   } else {
123      /* texture data is in malloc'd memory */
124      GLuint width = tex_image->Width;
125      GLuint height = ALIGN(tex_image->Height, bh) / bh;
126      GLuint texelSize = _mesa_get_format_bytes(tex_image->TexFormat);
127
128      assert(map);
129
130      *stride = _mesa_format_row_stride(tex_image->TexFormat, width);
131      *map = tex_image->Data + (slice * height + y) * *stride + x * texelSize;
132
133      return;
134   }
135}
136
137static void
138intel_unmap_texture_image(struct gl_context *ctx,
139			  struct gl_texture_image *tex_image, GLuint slice)
140{
141   struct intel_context *intel = intel_context(ctx);
142   struct intel_texture_image *intel_image = intel_texture_image(tex_image);
143
144   if (intel_image->mt)
145      intel_region_unmap(intel, intel_image->mt->region);
146
147   if (intel_image->stencil_rb) {
148      /*
149       * The texture has packed depth/stencil format, but uses separate
150       * stencil. The texture's embedded stencil buffer contains the real
151       * stencil data, so copy that into the miptree.
152       */
153      intel_tex_image_s8z24_scatter(intel, intel_image);
154   }
155}
156
157/**
158 * Called via ctx->Driver.GenerateMipmap()
159 * This is basically a wrapper for _mesa_meta_GenerateMipmap() which checks
160 * if we'll be using software mipmap generation.  In that case, we need to
161 * map/unmap the base level texture image.
162 */
163static void
164intelGenerateMipmap(struct gl_context *ctx, GLenum target,
165                    struct gl_texture_object *texObj)
166{
167   if (_mesa_meta_check_generate_mipmap_fallback(ctx, target, texObj)) {
168      /* sw path: need to map texture images */
169      struct intel_context *intel = intel_context(ctx);
170      struct intel_texture_object *intelObj = intel_texture_object(texObj);
171      struct gl_texture_image *first_image = texObj->Image[0][texObj->BaseLevel];
172
173      fallback_debug("%s - fallback to swrast\n", __FUNCTION__);
174
175      intel_tex_map_level_images(intel, intelObj, texObj->BaseLevel);
176      _mesa_generate_mipmap(ctx, target, texObj);
177      intel_tex_unmap_level_images(intel, intelObj, texObj->BaseLevel);
178
179      if (!_mesa_is_format_compressed(first_image->TexFormat)) {
180         GLuint nr_faces = (texObj->Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
181         GLuint face, i;
182         for (face = 0; face < nr_faces; face++) {
183            for (i = texObj->BaseLevel + 1; i < texObj->MaxLevel; i++) {
184               struct intel_texture_image *intelImage =
185                  intel_texture_image(texObj->Image[face][i]);
186               if (!intelImage)
187                  break;
188               /* Unreference the miptree to signal that the new Data is a
189                * bare pointer from mesa.
190                */
191               intel_miptree_release(intel, &intelImage->mt);
192            }
193         }
194      }
195   }
196   else {
197      _mesa_meta_GenerateMipmap(ctx, target, texObj);
198   }
199}
200
201
202void
203intelInitTextureFuncs(struct dd_function_table *functions)
204{
205   functions->GenerateMipmap = intelGenerateMipmap;
206
207   functions->NewTextureObject = intelNewTextureObject;
208   functions->NewTextureImage = intelNewTextureImage;
209   functions->DeleteTextureImage = intelDeleteTextureImage;
210   functions->DeleteTexture = intelDeleteTextureObject;
211   functions->FreeTextureImageBuffer = intel_free_texture_image_buffer;
212   functions->MapTextureImage = intel_map_texture_image;
213   functions->UnmapTextureImage = intel_unmap_texture_image;
214}
215