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