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
);
63 /* Because the driver uses AllocTextureImageBuffer() internally, it may end
64 * up mismatched with FreeTextureImageBuffer(), but that is safe to call
67 ctx
->Driver
.FreeTextureImageBuffer(ctx
, image
);
69 if (intel
->must_use_separate_stencil
70 && image
->TexFormat
== MESA_FORMAT_S8_Z24
) {
71 intel_tex_image_s8z24_create_renderbuffers(intel
, intel_image
);
74 if (intel_texobj
->mt
&&
75 intel_miptree_match_image(intel_texobj
->mt
, image
)) {
76 intel_miptree_reference(&intel_image
->mt
, intel_texobj
->mt
);
77 DBG("%s: alloc obj %p level %d %dx%dx%d using object's miptree %p\n",
78 __FUNCTION__
, texobj
, image
->Level
,
79 width
, height
, depth
, intel_texobj
->mt
);
81 } else if (image
->Border
== 0) {
82 intel_image
->mt
= intel_miptree_create_for_teximage(intel
, intel_texobj
,
86 /* Even if the object currently has a mipmap tree associated
87 * with it, this one is a more likely candidate to represent the
88 * whole object since our level didn't fit what was there
89 * before, and any lower levels would fit into our miptree.
91 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
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
);
99 DBG("%s: alloc obj %p level %d %dx%dx%d using swrast\n",
100 __FUNCTION__
, texobj
, image
->Level
, width
, height
, depth
);
102 return _swrast_alloc_texture_image_buffer(ctx
, image
, format
,
103 width
, height
, depth
);
107 intel_free_texture_image_buffer(struct gl_context
* ctx
,
108 struct gl_texture_image
*texImage
)
110 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
112 DBG("%s\n", __FUNCTION__
);
114 intel_miptree_release(&intelImage
->mt
);
116 if (texImage
->Data
) {
117 _mesa_free_texmemory(texImage
->Data
);
118 texImage
->Data
= NULL
;
121 _mesa_reference_renderbuffer(&intelImage
->depth_rb
, NULL
);
122 _mesa_reference_renderbuffer(&intelImage
->stencil_rb
, NULL
);
126 * Map texture memory/buffer into user space.
127 * Note: the region of interest parameters are ignored here.
128 * \param mapOut returns start of mapping of region of interest
129 * \param rowStrideOut returns row stride in bytes
132 intel_map_texture_image(struct gl_context
*ctx
,
133 struct gl_texture_image
*tex_image
,
135 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
140 struct intel_context
*intel
= intel_context(ctx
);
141 struct intel_texture_image
*intel_image
= intel_texture_image(tex_image
);
142 struct intel_mipmap_tree
*mt
= intel_image
->mt
;
145 if (intel_image
->stencil_rb
) {
147 * The texture has packed depth/stencil format, but uses separate
148 * stencil. The texture's embedded stencil buffer contains the real
149 * stencil data, so copy that into the miptree.
151 intel_tex_image_s8z24_gather(intel
, intel_image
);
154 /* For compressed formats, the stride is the number of bytes per
155 * row of blocks. intel_miptree_get_image_offset() already does
158 _mesa_get_format_block_size(tex_image
->TexFormat
, &bw
, &bh
);
163 void *base
= intel_region_map(intel
, mt
->region
);
164 unsigned int image_x
, image_y
;
166 intel_miptree_get_image_offset(mt
, tex_image
->Level
, tex_image
->Face
,
167 slice
, &image_x
, &image_y
);
171 *stride
= mt
->region
->pitch
* mt
->cpp
;
172 *map
= base
+ y
* *stride
+ x
* mt
->cpp
;
174 DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __FUNCTION__
,
175 x
- image_x
, y
- image_y
, w
, h
,
176 mt
, x
, y
, *map
, *stride
);
178 /* texture data is in malloc'd memory */
179 GLuint width
= tex_image
->Width
;
180 GLuint height
= ALIGN(tex_image
->Height
, bh
) / bh
;
181 GLuint texelSize
= _mesa_get_format_bytes(tex_image
->TexFormat
);
185 *stride
= _mesa_format_row_stride(tex_image
->TexFormat
, width
);
186 *map
= tex_image
->Data
+ (slice
* height
+ y
) * *stride
+ x
* texelSize
;
188 DBG("%s: %d,%d %dx%d from data %p = %p/%d\n", __FUNCTION__
,
190 tex_image
->Data
, *map
, *stride
);
195 intel_unmap_texture_image(struct gl_context
*ctx
,
196 struct gl_texture_image
*tex_image
, GLuint slice
)
198 struct intel_context
*intel
= intel_context(ctx
);
199 struct intel_texture_image
*intel_image
= intel_texture_image(tex_image
);
202 intel_region_unmap(intel
, intel_image
->mt
->region
);
204 if (intel_image
->stencil_rb
) {
206 * The texture has packed depth/stencil format, but uses separate
207 * stencil. The texture's embedded stencil buffer contains the real
208 * stencil data, so copy that into the miptree.
210 intel_tex_image_s8z24_scatter(intel
, intel_image
);
215 * Called via ctx->Driver.GenerateMipmap()
216 * This is basically a wrapper for _mesa_meta_GenerateMipmap() which checks
217 * if we'll be using software mipmap generation. In that case, we need to
218 * map/unmap the base level texture image.
221 intelGenerateMipmap(struct gl_context
*ctx
, GLenum target
,
222 struct gl_texture_object
*texObj
)
224 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, texObj
)) {
225 fallback_debug("%s - fallback to swrast\n", __FUNCTION__
);
227 _mesa_generate_mipmap(ctx
, target
, texObj
);
230 _mesa_meta_GenerateMipmap(ctx
, target
, texObj
);
236 intelInitTextureFuncs(struct dd_function_table
*functions
)
238 functions
->GenerateMipmap
= intelGenerateMipmap
;
240 functions
->NewTextureObject
= intelNewTextureObject
;
241 functions
->NewTextureImage
= intelNewTextureImage
;
242 functions
->DeleteTextureImage
= intelDeleteTextureImage
;
243 functions
->DeleteTexture
= intelDeleteTextureObject
;
244 functions
->AllocTextureImageBuffer
= intel_alloc_texture_image_buffer
;
245 functions
->FreeTextureImageBuffer
= intel_free_texture_image_buffer
;
246 functions
->MapTextureImage
= intel_map_texture_image
;
247 functions
->UnmapTextureImage
= intel_unmap_texture_image
;