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