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