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 "brw_context.h"
8 #include "intel_buffer_objects.h"
9 #include "intel_mipmap_tree.h"
10 #include "intel_tex.h"
11 #include "intel_fbo.h"
13 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
15 static struct gl_texture_image
*
16 intelNewTextureImage(struct gl_context
* ctx
)
18 DBG("%s\n", __FUNCTION__
);
20 return (struct gl_texture_image
*) CALLOC_STRUCT(intel_texture_image
);
24 intelDeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
26 /* nothing special (yet) for intel_texture_image */
27 _mesa_delete_texture_image(ctx
, img
);
31 static struct gl_texture_object
*
32 intelNewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
34 struct intel_texture_object
*obj
= CALLOC_STRUCT(intel_texture_object
);
38 DBG("%s\n", __FUNCTION__
);
43 _mesa_initialize_texture_object(ctx
, &obj
->base
, name
, target
);
45 obj
->needs_validate
= true;
51 intelDeleteTextureObject(struct gl_context
*ctx
,
52 struct gl_texture_object
*texObj
)
54 struct intel_texture_object
*intelObj
= intel_texture_object(texObj
);
56 intel_miptree_release(&intelObj
->mt
);
57 _mesa_delete_texture_object(ctx
, texObj
);
61 intel_alloc_texture_image_buffer(struct gl_context
*ctx
,
62 struct gl_texture_image
*image
)
64 struct brw_context
*brw
= brw_context(ctx
);
65 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
66 struct gl_texture_object
*texobj
= image
->TexObject
;
67 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
69 assert(image
->Border
== 0);
71 /* Quantize sample count */
72 if (image
->NumSamples
) {
73 image
->NumSamples
= intel_quantize_num_samples(brw
->intelScreen
, image
->NumSamples
);
74 if (!image
->NumSamples
)
78 /* Because the driver uses AllocTextureImageBuffer() internally, it may end
79 * up mismatched with FreeTextureImageBuffer(), but that is safe to call
82 ctx
->Driver
.FreeTextureImageBuffer(ctx
, image
);
84 if (!_swrast_init_texture_image(image
))
87 if (intel_texobj
->mt
&&
88 intel_miptree_match_image(intel_texobj
->mt
, image
)) {
89 intel_miptree_reference(&intel_image
->mt
, intel_texobj
->mt
);
90 DBG("%s: alloc obj %p level %d %dx%dx%d using object's miptree %p\n",
91 __FUNCTION__
, texobj
, image
->Level
,
92 image
->Width
, image
->Height
, image
->Depth
, intel_texobj
->mt
);
94 intel_image
->mt
= intel_miptree_create_for_teximage(brw
, intel_texobj
,
98 /* Even if the object currently has a mipmap tree associated
99 * with it, this one is a more likely candidate to represent the
100 * whole object since our level didn't fit what was there
101 * before, and any lower levels would fit into our miptree.
103 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
105 DBG("%s: alloc obj %p level %d %dx%dx%d using new miptree %p\n",
106 __FUNCTION__
, texobj
, image
->Level
,
107 image
->Width
, image
->Height
, image
->Depth
, intel_image
->mt
);
110 intel_texobj
->needs_validate
= true;
116 * ctx->Driver.AllocTextureStorage() handler.
118 * Compare this to _mesa_AllocTextureStorage_sw, which would call into
119 * intel_alloc_texture_image_buffer() above.
122 intel_alloc_texture_storage(struct gl_context
*ctx
,
123 struct gl_texture_object
*texobj
,
124 GLsizei levels
, GLsizei width
,
125 GLsizei height
, GLsizei depth
)
127 struct brw_context
*brw
= brw_context(ctx
);
128 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
129 struct gl_texture_image
*first_image
= texobj
->Image
[0][0];
130 int num_samples
= intel_quantize_num_samples(brw
->intelScreen
,
131 first_image
->NumSamples
);
132 const int numFaces
= _mesa_num_tex_faces(texobj
->Target
);
136 /* If the object's current miptree doesn't match what we need, make a new
139 if (!intel_texobj
->mt
||
140 !intel_miptree_match_image(intel_texobj
->mt
, first_image
) ||
141 intel_texobj
->mt
->last_level
!= levels
- 1) {
142 intel_miptree_release(&intel_texobj
->mt
);
143 intel_texobj
->mt
= intel_miptree_create(brw
, texobj
->Target
,
144 first_image
->TexFormat
,
146 width
, height
, depth
,
147 false, /* expect_accelerated */
149 INTEL_MIPTREE_TILING_ANY
,
152 if (intel_texobj
->mt
== NULL
) {
157 for (face
= 0; face
< numFaces
; face
++) {
158 for (level
= 0; level
< levels
; level
++) {
159 struct gl_texture_image
*image
= texobj
->Image
[face
][level
];
160 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
162 image
->NumSamples
= num_samples
;
164 _swrast_free_texture_image_buffer(ctx
, image
);
165 if (!_swrast_init_texture_image(image
))
168 intel_miptree_reference(&intel_image
->mt
, intel_texobj
->mt
);
172 /* The miptree is in a validated state, so no need to check later. */
173 intel_texobj
->needs_validate
= false;
174 intel_texobj
->validated_first_level
= 0;
175 intel_texobj
->validated_last_level
= levels
- 1;
176 intel_texobj
->_Format
= intel_texobj
->mt
->format
;
183 intel_free_texture_image_buffer(struct gl_context
* ctx
,
184 struct gl_texture_image
*texImage
)
186 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
188 DBG("%s\n", __FUNCTION__
);
190 intel_miptree_release(&intelImage
->mt
);
192 _swrast_free_texture_image_buffer(ctx
, texImage
);
196 * Map texture memory/buffer into user space.
197 * Note: the region of interest parameters are ignored here.
198 * \param mode bitmask of GL_MAP_READ_BIT, GL_MAP_WRITE_BIT
199 * \param mapOut returns start of mapping of region of interest
200 * \param rowStrideOut returns row stride in bytes
203 intel_map_texture_image(struct gl_context
*ctx
,
204 struct gl_texture_image
*tex_image
,
206 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
211 struct brw_context
*brw
= brw_context(ctx
);
212 struct intel_texture_image
*intel_image
= intel_texture_image(tex_image
);
213 struct intel_mipmap_tree
*mt
= intel_image
->mt
;
216 /* Our texture data is always stored in a miptree. */
219 /* Check that our caller wasn't confused about how to map a 1D texture. */
220 assert(tex_image
->TexObject
->Target
!= GL_TEXTURE_1D_ARRAY
||
223 /* intel_miptree_map operates on a unified "slice" number that references the
224 * cube face, since it's all just slices to the miptree code.
226 if (tex_image
->TexObject
->Target
== GL_TEXTURE_CUBE_MAP
)
227 slice
= tex_image
->Face
;
229 intel_miptree_map(brw
, mt
,
230 tex_image
->Level
+ tex_image
->TexObject
->MinLevel
,
231 slice
+ tex_image
->TexObject
->MinLayer
,
233 (void **)map
, &stride
);
235 *out_stride
= stride
;
239 intel_unmap_texture_image(struct gl_context
*ctx
,
240 struct gl_texture_image
*tex_image
, GLuint slice
)
242 struct brw_context
*brw
= brw_context(ctx
);
243 struct intel_texture_image
*intel_image
= intel_texture_image(tex_image
);
244 struct intel_mipmap_tree
*mt
= intel_image
->mt
;
246 if (tex_image
->TexObject
->Target
== GL_TEXTURE_CUBE_MAP
)
247 slice
= tex_image
->Face
;
249 intel_miptree_unmap(brw
, mt
,
250 tex_image
->Level
+ tex_image
->TexObject
->MinLevel
,
251 slice
+ tex_image
->TexObject
->MinLayer
);
255 intel_texture_view(struct gl_context
*ctx
,
256 struct gl_texture_object
*texObj
,
257 struct gl_texture_object
*origTexObj
)
259 struct brw_context
*brw
= brw_context(ctx
);
260 struct intel_texture_object
*intel_tex
= intel_texture_object(texObj
);
261 struct intel_texture_object
*intel_orig_tex
= intel_texture_object(origTexObj
);
263 assert(intel_orig_tex
->mt
);
264 intel_miptree_reference(&intel_tex
->mt
, intel_orig_tex
->mt
);
266 /* Since we can only make views of immutable-format textures,
267 * we can assume that everything is in origTexObj's miptree.
269 * Mesa core has already made us a copy of all the teximage objects,
270 * except it hasn't copied our mt pointers, etc.
272 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
273 const int numLevels
= texObj
->NumLevels
;
278 for (face
= 0; face
< numFaces
; face
++) {
279 for (level
= 0; level
< numLevels
; level
++) {
280 struct gl_texture_image
*image
= texObj
->Image
[face
][level
];
281 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
283 intel_miptree_reference(&intel_image
->mt
, intel_orig_tex
->mt
);
287 /* The miptree is in a validated state, so no need to check later. */
288 intel_tex
->needs_validate
= false;
289 intel_tex
->validated_first_level
= 0;
290 intel_tex
->validated_last_level
= numLevels
- 1;
292 /* Set the validated texture format, with the same adjustments that
293 * would have been applied to determine the underlying texture's
296 intel_tex
->_Format
= intel_depth_format_for_depthstencil_format(
297 intel_lower_compressed_format(brw
, texObj
->Image
[0][0]->TexFormat
));
303 intel_set_texture_storage_for_buffer_object(struct gl_context
*ctx
,
304 struct gl_texture_object
*tex_obj
,
305 struct gl_buffer_object
*buffer_obj
,
306 uint32_t buffer_offset
,
310 struct brw_context
*brw
= brw_context(ctx
);
311 struct intel_texture_object
*intel_texobj
= intel_texture_object(tex_obj
);
312 struct gl_texture_image
*image
= tex_obj
->Image
[0][0];
313 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
314 struct intel_buffer_object
*intel_buffer_obj
= intel_buffer_object(buffer_obj
);
317 /* Renderbuffers have the restriction that the buffer offset and
318 * surface pitch must be a multiple of the element size. If it's
319 * not, we have to fail and fall back to software.
321 int cpp
= _mesa_get_format_bytes(image
->TexFormat
);
322 if (buffer_offset
% cpp
|| row_stride
% cpp
) {
323 perf_debug("Bad PBO alignment; fallback to CPU mapping\n");
327 if (!brw
->format_supported_as_render_target
[image
->TexFormat
]) {
328 perf_debug("Non-renderable PBO format; fallback to CPU mapping\n");
333 assert(intel_texobj
->mt
== NULL
);
335 drm_intel_bo
*bo
= intel_bufferobj_buffer(brw
, intel_buffer_obj
,
337 row_stride
* image
->Height
);
339 intel_miptree_create_for_bo(brw
, bo
,
342 image
->Width
, image
->Height
, image
->Depth
,
344 if (!intel_texobj
->mt
)
347 if (!_swrast_init_texture_image(image
))
350 intel_miptree_reference(&intel_image
->mt
, intel_texobj
->mt
);
352 /* The miptree is in a validated state, so no need to check later. */
353 intel_texobj
->needs_validate
= false;
354 intel_texobj
->validated_first_level
= 0;
355 intel_texobj
->validated_last_level
= 0;
356 intel_texobj
->_Format
= intel_texobj
->mt
->format
;
362 intelInitTextureFuncs(struct dd_function_table
*functions
)
364 functions
->NewTextureObject
= intelNewTextureObject
;
365 functions
->NewTextureImage
= intelNewTextureImage
;
366 functions
->DeleteTextureImage
= intelDeleteTextureImage
;
367 functions
->DeleteTexture
= intelDeleteTextureObject
;
368 functions
->AllocTextureImageBuffer
= intel_alloc_texture_image_buffer
;
369 functions
->FreeTextureImageBuffer
= intel_free_texture_image_buffer
;
370 functions
->AllocTextureStorage
= intel_alloc_texture_storage
;
371 functions
->MapTextureImage
= intel_map_texture_image
;
372 functions
->UnmapTextureImage
= intel_unmap_texture_image
;
373 functions
->TextureView
= intel_texture_view
;
374 functions
->SetTextureStorageForBufferObject
=
375 intel_set_texture_storage_for_buffer_object
;