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"
11 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
13 static struct gl_texture_image
*
14 intelNewTextureImage(struct gl_context
* ctx
)
16 DBG("%s\n", __FUNCTION__
);
18 return (struct gl_texture_image
*) CALLOC_STRUCT(intel_texture_image
);
22 intelDeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
24 /* nothing special (yet) for intel_texture_image */
25 _mesa_delete_texture_image(ctx
, img
);
29 static struct gl_texture_object
*
30 intelNewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
32 struct intel_texture_object
*obj
= CALLOC_STRUCT(intel_texture_object
);
36 DBG("%s\n", __FUNCTION__
);
37 _mesa_initialize_texture_object(&obj
->base
, name
, target
);
43 intelDeleteTextureObject(struct gl_context
*ctx
,
44 struct gl_texture_object
*texObj
)
46 struct intel_texture_object
*intelObj
= intel_texture_object(texObj
);
48 intel_miptree_release(&intelObj
->mt
);
49 _mesa_delete_texture_object(ctx
, texObj
);
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
)
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
);
64 assert(image
->Border
== 0);
66 /* Because the driver uses AllocTextureImageBuffer() internally, it may end
67 * up mismatched with FreeTextureImageBuffer(), but that is safe to call
70 ctx
->Driver
.FreeTextureImageBuffer(ctx
, image
);
72 if (intel
->must_use_separate_stencil
73 && image
->TexFormat
== MESA_FORMAT_S8_Z24
) {
74 intel_tex_image_s8z24_create_renderbuffers(intel
, intel_image
);
77 /* Allocate the swrast_texture_image::ImageOffsets array now */
78 switch (texobj
->Target
) {
80 case GL_TEXTURE_2D_ARRAY
:
81 slices
= image
->Depth
;
83 case GL_TEXTURE_1D_ARRAY
:
84 slices
= image
->Height
;
89 assert(!intel_image
->base
.ImageOffsets
);
90 intel_image
->base
.ImageOffsets
= malloc(slices
* sizeof(GLuint
));
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
);
99 intel_image
->mt
= intel_miptree_create_for_teximage(intel
, intel_texobj
,
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.
108 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
110 if (intel
->must_use_separate_stencil
111 && image
->TexFormat
== MESA_FORMAT_S8_Z24
) {
112 intel_tex_image_s8z24_create_renderbuffers(intel
, intel_image
);
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
);
124 intel_free_texture_image_buffer(struct gl_context
* ctx
,
125 struct gl_texture_image
*texImage
)
127 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
129 DBG("%s\n", __FUNCTION__
);
131 intel_miptree_release(&intelImage
->mt
);
133 if (intelImage
->base
.Buffer
) {
134 _mesa_align_free(intelImage
->base
.Buffer
);
135 intelImage
->base
.Buffer
= NULL
;
138 if (intelImage
->base
.ImageOffsets
) {
139 free(intelImage
->base
.ImageOffsets
);
140 intelImage
->base
.ImageOffsets
= NULL
;
143 _mesa_reference_renderbuffer(&intelImage
->depth_rb
, NULL
);
144 _mesa_reference_renderbuffer(&intelImage
->stencil_rb
, NULL
);
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
155 intel_map_texture_image(struct gl_context
*ctx
,
156 struct gl_texture_image
*tex_image
,
158 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
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
;
168 unsigned int image_x
, image_y
;
170 /* Our texture data is always stored in a miptree. */
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
||
177 if (intel_image
->stencil_rb
) {
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.
183 intel_tex_image_s8z24_gather(intel
, intel_image
);
186 /* For compressed formats, the stride is the number of bytes per
187 * row of blocks. intel_miptree_get_image_offset() already does
190 _mesa_get_format_block_size(tex_image
->TexFormat
, &bw
, &bh
);
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
);
200 *stride
= mt
->region
->pitch
* mt
->cpp
;
201 *map
= base
+ y
* *stride
+ x
* mt
->cpp
;
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
);
209 intel_unmap_texture_image(struct gl_context
*ctx
,
210 struct gl_texture_image
*tex_image
, GLuint slice
)
212 struct intel_context
*intel
= intel_context(ctx
);
213 struct intel_texture_image
*intel_image
= intel_texture_image(tex_image
);
215 intel_region_unmap(intel
, intel_image
->mt
->region
);
217 if (intel_image
->stencil_rb
) {
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.
223 intel_tex_image_s8z24_scatter(intel
, intel_image
);
228 intelInitTextureFuncs(struct dd_function_table
*functions
)
230 functions
->NewTextureObject
= intelNewTextureObject
;
231 functions
->NewTextureImage
= intelNewTextureImage
;
232 functions
->DeleteTextureImage
= intelDeleteTextureImage
;
233 functions
->DeleteTexture
= intelDeleteTextureObject
;
234 functions
->AllocTextureImageBuffer
= intel_alloc_texture_image_buffer
;
235 functions
->FreeTextureImageBuffer
= intel_free_texture_image_buffer
;
236 functions
->MapTextureImage
= intel_map_texture_image
;
237 functions
->UnmapTextureImage
= intel_unmap_texture_image
;