i965: add support for multisample textures
[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 #include "intel_fbo.h"
11
12 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
13
14 static struct gl_texture_image *
15 intelNewTextureImage(struct gl_context * ctx)
16 {
17 DBG("%s\n", __FUNCTION__);
18 (void) ctx;
19 return (struct gl_texture_image *) CALLOC_STRUCT(intel_texture_image);
20 }
21
22 static void
23 intelDeleteTextureImage(struct gl_context * ctx, struct gl_texture_image *img)
24 {
25 /* nothing special (yet) for intel_texture_image */
26 _mesa_delete_texture_image(ctx, img);
27 }
28
29
30 static struct gl_texture_object *
31 intelNewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
32 {
33 struct intel_texture_object *obj = CALLOC_STRUCT(intel_texture_object);
34
35 (void) ctx;
36
37 DBG("%s\n", __FUNCTION__);
38 _mesa_initialize_texture_object(&obj->base, name, target);
39
40 obj->needs_validate = true;
41
42 return &obj->base;
43 }
44
45 static void
46 intelDeleteTextureObject(struct gl_context *ctx,
47 struct gl_texture_object *texObj)
48 {
49 struct intel_texture_object *intelObj = intel_texture_object(texObj);
50
51 intel_miptree_release(&intelObj->mt);
52 _mesa_delete_texture_object(ctx, texObj);
53 }
54
55 static GLboolean
56 intel_alloc_texture_image_buffer(struct gl_context *ctx,
57 struct gl_texture_image *image)
58 {
59 struct intel_context *intel = intel_context(ctx);
60 struct intel_texture_image *intel_image = intel_texture_image(image);
61 struct gl_texture_object *texobj = image->TexObject;
62 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
63 GLuint slices;
64
65 assert(image->Border == 0);
66
67 /* Quantize sample count */
68 if (image->NumSamples) {
69 image->NumSamples = intel_quantize_num_samples(intel->intelScreen, image->NumSamples);
70 if (!image->NumSamples)
71 return false;
72 }
73
74 /* Because the driver uses AllocTextureImageBuffer() internally, it may end
75 * up mismatched with FreeTextureImageBuffer(), but that is safe to call
76 * multiple times.
77 */
78 ctx->Driver.FreeTextureImageBuffer(ctx, image);
79
80 /* Allocate the swrast_texture_image::ImageOffsets array now */
81 switch (texobj->Target) {
82 case GL_TEXTURE_3D:
83 case GL_TEXTURE_2D_ARRAY:
84 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
85 slices = image->Depth;
86 break;
87 case GL_TEXTURE_1D_ARRAY:
88 slices = image->Height;
89 break;
90 default:
91 slices = 1;
92 }
93 assert(!intel_image->base.ImageOffsets);
94 intel_image->base.ImageOffsets = malloc(slices * sizeof(GLuint));
95
96 _swrast_init_texture_image(image);
97
98 if (intel_texobj->mt &&
99 intel_miptree_match_image(intel_texobj->mt, image)) {
100 intel_miptree_reference(&intel_image->mt, intel_texobj->mt);
101 DBG("%s: alloc obj %p level %d %dx%dx%d using object's miptree %p\n",
102 __FUNCTION__, texobj, image->Level,
103 image->Width, image->Height, image->Depth, intel_texobj->mt);
104 } else {
105 intel_image->mt = intel_miptree_create_for_teximage(intel, intel_texobj,
106 intel_image,
107 false);
108
109 /* Even if the object currently has a mipmap tree associated
110 * with it, this one is a more likely candidate to represent the
111 * whole object since our level didn't fit what was there
112 * before, and any lower levels would fit into our miptree.
113 */
114 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
115
116 DBG("%s: alloc obj %p level %d %dx%dx%d using new miptree %p\n",
117 __FUNCTION__, texobj, image->Level,
118 image->Width, image->Height, image->Depth, intel_image->mt);
119 }
120
121 intel_texobj->needs_validate = true;
122
123 return true;
124 }
125
126 /**
127 * Called via ctx->Driver.AllocTextureStorage()
128 * Just have to allocate memory for the texture images.
129 */
130 static GLboolean
131 intel_alloc_texture_storage(struct gl_context *ctx,
132 struct gl_texture_object *texObj,
133 GLsizei levels, GLsizei width,
134 GLsizei height, GLsizei depth)
135 {
136 const int numFaces = _mesa_num_tex_faces(texObj->Target);
137 int face;
138 int level;
139
140 for (face = 0; face < numFaces; face++) {
141 for (level = 0; level < levels; level++) {
142 struct gl_texture_image *const texImage = texObj->Image[face][level];
143 if (!intel_alloc_texture_image_buffer(ctx, texImage))
144 return false;
145 }
146 }
147
148 return true;
149 }
150
151 static void
152 intel_free_texture_image_buffer(struct gl_context * ctx,
153 struct gl_texture_image *texImage)
154 {
155 struct intel_texture_image *intelImage = intel_texture_image(texImage);
156
157 DBG("%s\n", __FUNCTION__);
158
159 intel_miptree_release(&intelImage->mt);
160
161 if (intelImage->base.Buffer) {
162 _mesa_align_free(intelImage->base.Buffer);
163 intelImage->base.Buffer = NULL;
164 }
165
166 free(intelImage->base.ImageOffsets);
167 intelImage->base.ImageOffsets = NULL;
168 }
169
170 /**
171 * Map texture memory/buffer into user space.
172 * Note: the region of interest parameters are ignored here.
173 * \param mode bitmask of GL_MAP_READ_BIT, GL_MAP_WRITE_BIT
174 * \param mapOut returns start of mapping of region of interest
175 * \param rowStrideOut returns row stride in bytes
176 */
177 static void
178 intel_map_texture_image(struct gl_context *ctx,
179 struct gl_texture_image *tex_image,
180 GLuint slice,
181 GLuint x, GLuint y, GLuint w, GLuint h,
182 GLbitfield mode,
183 GLubyte **map,
184 GLint *stride)
185 {
186 struct intel_context *intel = intel_context(ctx);
187 struct intel_texture_image *intel_image = intel_texture_image(tex_image);
188 struct intel_mipmap_tree *mt = intel_image->mt;
189
190 /* Our texture data is always stored in a miptree. */
191 assert(mt);
192
193 /* Check that our caller wasn't confused about how to map a 1D texture. */
194 assert(tex_image->TexObject->Target != GL_TEXTURE_1D_ARRAY ||
195 h == 1);
196
197 /* intel_miptree_map operates on a unified "slice" number that references the
198 * cube face, since it's all just slices to the miptree code.
199 */
200 if (tex_image->TexObject->Target == GL_TEXTURE_CUBE_MAP)
201 slice = tex_image->Face;
202
203 intel_miptree_map(intel, mt, tex_image->Level, slice, x, y, w, h, mode,
204 (void **)map, stride);
205 }
206
207 static void
208 intel_unmap_texture_image(struct gl_context *ctx,
209 struct gl_texture_image *tex_image, GLuint slice)
210 {
211 struct intel_context *intel = intel_context(ctx);
212 struct intel_texture_image *intel_image = intel_texture_image(tex_image);
213 struct intel_mipmap_tree *mt = intel_image->mt;
214
215 if (tex_image->TexObject->Target == GL_TEXTURE_CUBE_MAP)
216 slice = tex_image->Face;
217
218 intel_miptree_unmap(intel, mt, tex_image->Level, slice);
219 }
220
221 void
222 intelInitTextureFuncs(struct dd_function_table *functions)
223 {
224 functions->NewTextureObject = intelNewTextureObject;
225 functions->NewTextureImage = intelNewTextureImage;
226 functions->DeleteTextureImage = intelDeleteTextureImage;
227 functions->DeleteTexture = intelDeleteTextureObject;
228 functions->AllocTextureImageBuffer = intel_alloc_texture_image_buffer;
229 functions->FreeTextureImageBuffer = intel_free_texture_image_buffer;
230 functions->AllocTextureStorage = intel_alloc_texture_storage;
231 functions->MapTextureImage = intel_map_texture_image;
232 functions->UnmapTextureImage = intel_unmap_texture_image;
233 }