intel: Add an AllocTextureImageBuffer() implementation using miptrees.
[mesa.git] / src / mesa / drivers / dri / intel / intel_tex.c
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
13 static struct gl_texture_image *
14 intelNewTextureImage(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 static void
22 intelDeleteTextureImage(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
29 static struct gl_texture_object *
30 intelNewTextureObject(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
42 static void
43 intelDeleteTextureObject(struct gl_context *ctx,
44 struct gl_texture_object *texObj)
45 {
46 struct intel_texture_object *intelObj = intel_texture_object(texObj);
47
48 intel_miptree_release(&intelObj->mt);
49 _mesa_delete_texture_object(ctx, texObj);
50 }
51
52 static GLboolean
53 intel_alloc_texture_image_buffer(struct gl_context *ctx,
54 struct gl_texture_image *image,
55 gl_format format, GLsizei width,
56 GLsizei height, GLsizei depth)
57 {
58 struct intel_context *intel = intel_context(ctx);
59 struct intel_texture_image *intel_image = intel_texture_image(image);
60 struct gl_texture_object *texobj = image->TexObject;
61 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
62
63 if (intel_texobj->mt &&
64 intel_miptree_match_image(intel_texobj->mt, image)) {
65 intel_miptree_reference(&intel_image->mt, intel_texobj->mt);
66 DBG("%s: alloc obj %p level %d %dx%dx%d using object's miptree %p\n",
67 __FUNCTION__, texobj, image->Level,
68 width, height, depth, intel_texobj->mt);
69 return true;
70 } else if (image->Border == 0) {
71 intel_image->mt = intel_miptree_create_for_teximage(intel, intel_texobj,
72 intel_image,
73 false);
74
75 /* Even if the object currently has a mipmap tree associated
76 * with it, this one is a more likely candidate to represent the
77 * whole object since our level didn't fit what was there
78 * before, and any lower levels would fit into our miptree.
79 */
80 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
81
82 DBG("%s: alloc obj %p level %d %dx%dx%d using new miptree %p\n",
83 __FUNCTION__, texobj, image->Level,
84 width, height, depth, intel_image->mt);
85 return true;
86 }
87
88 DBG("%s: alloc obj %p level %d %dx%dx%d using swrast\n",
89 __FUNCTION__, texobj, image->Level, width, height, depth);
90
91 return _swrast_alloc_texture_image_buffer(ctx, image, format,
92 width, height, depth);
93 }
94
95 static void
96 intel_free_texture_image_buffer(struct gl_context * ctx,
97 struct gl_texture_image *texImage)
98 {
99 struct intel_texture_image *intelImage = intel_texture_image(texImage);
100
101 DBG("%s\n", __FUNCTION__);
102
103 intel_miptree_release(&intelImage->mt);
104
105 if (texImage->Data) {
106 _mesa_free_texmemory(texImage->Data);
107 texImage->Data = NULL;
108 }
109
110 _mesa_reference_renderbuffer(&intelImage->depth_rb, NULL);
111 _mesa_reference_renderbuffer(&intelImage->stencil_rb, NULL);
112 }
113
114 /**
115 * Map texture memory/buffer into user space.
116 * Note: the region of interest parameters are ignored here.
117 * \param mapOut returns start of mapping of region of interest
118 * \param rowStrideOut returns row stride in bytes
119 */
120 static void
121 intel_map_texture_image(struct gl_context *ctx,
122 struct gl_texture_image *tex_image,
123 GLuint slice,
124 GLuint x, GLuint y, GLuint w, GLuint h,
125 GLbitfield mode,
126 GLubyte **map,
127 GLint *stride)
128 {
129 struct intel_context *intel = intel_context(ctx);
130 struct intel_texture_image *intel_image = intel_texture_image(tex_image);
131 struct intel_mipmap_tree *mt = intel_image->mt;
132 unsigned int bw, bh;
133
134 if (intel_image->stencil_rb) {
135 /*
136 * The texture has packed depth/stencil format, but uses separate
137 * stencil. The texture's embedded stencil buffer contains the real
138 * stencil data, so copy that into the miptree.
139 */
140 intel_tex_image_s8z24_gather(intel, intel_image);
141 }
142
143 /* For compressed formats, the stride is the number of bytes per
144 * row of blocks. intel_miptree_get_image_offset() already does
145 * the divide.
146 */
147 _mesa_get_format_block_size(tex_image->TexFormat, &bw, &bh);
148 assert(y % bh == 0);
149 y /= bh;
150
151 if (likely(mt)) {
152 void *base = intel_region_map(intel, mt->region);
153 unsigned int image_x, image_y;
154
155 intel_miptree_get_image_offset(mt, tex_image->Level, tex_image->Face,
156 slice, &image_x, &image_y);
157 x += image_x;
158 y += image_y;
159
160 *stride = mt->region->pitch * mt->cpp;
161 *map = base + y * *stride + x * mt->cpp;
162 } else {
163 /* texture data is in malloc'd memory */
164 GLuint width = tex_image->Width;
165 GLuint height = ALIGN(tex_image->Height, bh) / bh;
166 GLuint texelSize = _mesa_get_format_bytes(tex_image->TexFormat);
167
168 assert(map);
169
170 *stride = _mesa_format_row_stride(tex_image->TexFormat, width);
171 *map = tex_image->Data + (slice * height + y) * *stride + x * texelSize;
172
173 return;
174 }
175 }
176
177 static void
178 intel_unmap_texture_image(struct gl_context *ctx,
179 struct gl_texture_image *tex_image, GLuint slice)
180 {
181 struct intel_context *intel = intel_context(ctx);
182 struct intel_texture_image *intel_image = intel_texture_image(tex_image);
183
184 if (intel_image->mt)
185 intel_region_unmap(intel, intel_image->mt->region);
186
187 if (intel_image->stencil_rb) {
188 /*
189 * The texture has packed depth/stencil format, but uses separate
190 * stencil. The texture's embedded stencil buffer contains the real
191 * stencil data, so copy that into the miptree.
192 */
193 intel_tex_image_s8z24_scatter(intel, intel_image);
194 }
195 }
196
197 /**
198 * Called via ctx->Driver.GenerateMipmap()
199 * This is basically a wrapper for _mesa_meta_GenerateMipmap() which checks
200 * if we'll be using software mipmap generation. In that case, we need to
201 * map/unmap the base level texture image.
202 */
203 static void
204 intelGenerateMipmap(struct gl_context *ctx, GLenum target,
205 struct gl_texture_object *texObj)
206 {
207 if (_mesa_meta_check_generate_mipmap_fallback(ctx, target, texObj)) {
208 fallback_debug("%s - fallback to swrast\n", __FUNCTION__);
209
210 _mesa_generate_mipmap(ctx, target, texObj);
211 }
212 else {
213 _mesa_meta_GenerateMipmap(ctx, target, texObj);
214 }
215 }
216
217
218 void
219 intelInitTextureFuncs(struct dd_function_table *functions)
220 {
221 functions->GenerateMipmap = intelGenerateMipmap;
222
223 functions->NewTextureObject = intelNewTextureObject;
224 functions->NewTextureImage = intelNewTextureImage;
225 functions->DeleteTextureImage = intelDeleteTextureImage;
226 functions->DeleteTexture = intelDeleteTextureObject;
227 functions->AllocTextureImageBuffer = intel_alloc_texture_image_buffer;
228 functions->FreeTextureImageBuffer = intel_free_texture_image_buffer;
229 functions->MapTextureImage = intel_map_texture_image;
230 functions->UnmapTextureImage = intel_unmap_texture_image;
231 }