intel_tex.c revision bfc09e92ffbf3e03f5ea09421d2b8cbae1df53bf
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
21
22static struct gl_texture_object *
23intelNewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
24{
25   struct intel_texture_object *obj = CALLOC_STRUCT(intel_texture_object);
26
27   DBG("%s\n", __FUNCTION__);
28   _mesa_initialize_texture_object(&obj->base, name, target);
29
30   return &obj->base;
31}
32
33static void
34intelDeleteTextureObject(struct gl_context *ctx,
35			 struct gl_texture_object *texObj)
36{
37   struct intel_context *intel = intel_context(ctx);
38   struct intel_texture_object *intelObj = intel_texture_object(texObj);
39
40   if (intelObj->mt)
41      intel_miptree_release(intel, &intelObj->mt);
42
43   _mesa_delete_texture_object(ctx, texObj);
44}
45
46
47static void
48intel_free_texture_image_buffer(struct gl_context * ctx,
49				struct gl_texture_image *texImage)
50{
51   struct intel_context *intel = intel_context(ctx);
52   struct intel_texture_image *intelImage = intel_texture_image(texImage);
53
54   DBG("%s\n", __FUNCTION__);
55
56   if (intelImage->mt) {
57      intel_miptree_release(intel, &intelImage->mt);
58   }
59
60   if (texImage->Data) {
61      _mesa_free_texmemory(texImage->Data);
62      texImage->Data = NULL;
63   }
64
65   if (intelImage->depth_rb) {
66      _mesa_reference_renderbuffer(&intelImage->depth_rb, NULL);
67   }
68
69   if (intelImage->stencil_rb) {
70      _mesa_reference_renderbuffer(&intelImage->stencil_rb, NULL);
71   }
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(mt->format, &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   intel_region_unmap(intel, intel_image->mt->region);
145
146   if (intel_image->stencil_rb) {
147      /*
148       * The texture has packed depth/stencil format, but uses separate
149       * stencil. The texture's embedded stencil buffer contains the real
150       * stencil data, so copy that into the miptree.
151       */
152      intel_tex_image_s8z24_scatter(intel, intel_image);
153   }
154}
155
156/**
157 * Called via ctx->Driver.GenerateMipmap()
158 * This is basically a wrapper for _mesa_meta_GenerateMipmap() which checks
159 * if we'll be using software mipmap generation.  In that case, we need to
160 * map/unmap the base level texture image.
161 */
162static void
163intelGenerateMipmap(struct gl_context *ctx, GLenum target,
164                    struct gl_texture_object *texObj)
165{
166   if (_mesa_meta_check_generate_mipmap_fallback(ctx, target, texObj)) {
167      /* sw path: need to map texture images */
168      struct intel_context *intel = intel_context(ctx);
169      struct intel_texture_object *intelObj = intel_texture_object(texObj);
170      struct gl_texture_image *first_image = texObj->Image[0][texObj->BaseLevel];
171
172      fallback_debug("%s - fallback to swrast\n", __FUNCTION__);
173
174      intel_tex_map_level_images(intel, intelObj, texObj->BaseLevel);
175      _mesa_generate_mipmap(ctx, target, texObj);
176      intel_tex_unmap_level_images(intel, intelObj, texObj->BaseLevel);
177
178      if (!_mesa_is_format_compressed(first_image->TexFormat)) {
179         GLuint nr_faces = (texObj->Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
180         GLuint face, i;
181         for (face = 0; face < nr_faces; face++) {
182            for (i = texObj->BaseLevel + 1; i < texObj->MaxLevel; i++) {
183               struct intel_texture_image *intelImage =
184                  intel_texture_image(texObj->Image[face][i]);
185               if (!intelImage)
186                  break;
187               /* Unreference the miptree to signal that the new Data is a
188                * bare pointer from mesa.
189                */
190               intel_miptree_release(intel, &intelImage->mt);
191            }
192         }
193      }
194   }
195   else {
196      _mesa_meta_GenerateMipmap(ctx, target, texObj);
197   }
198}
199
200
201void
202intelInitTextureFuncs(struct dd_function_table *functions)
203{
204   functions->GenerateMipmap = intelGenerateMipmap;
205
206   functions->NewTextureObject = intelNewTextureObject;
207   functions->NewTextureImage = intelNewTextureImage;
208   functions->DeleteTexture = intelDeleteTextureObject;
209   functions->FreeTextureImageBuffer = intel_free_texture_image_buffer;
210   functions->MapTextureImage = intel_map_texture_image;
211   functions->UnmapTextureImage = intel_unmap_texture_image;
212}
213