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", __func__
);
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", __func__
);
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 __func__
, 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 __func__
, 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
,
148 MIPTREE_LAYOUT_TILING_ANY
);
150 if (intel_texobj
->mt
== NULL
) {
155 for (face
= 0; face
< numFaces
; face
++) {
156 for (level
= 0; level
< levels
; level
++) {
157 struct gl_texture_image
*image
= texobj
->Image
[face
][level
];
158 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
160 image
->NumSamples
= num_samples
;
162 _swrast_free_texture_image_buffer(ctx
, image
);
163 if (!_swrast_init_texture_image(image
))
166 intel_miptree_reference(&intel_image
->mt
, intel_texobj
->mt
);
170 /* The miptree is in a validated state, so no need to check later. */
171 intel_texobj
->needs_validate
= false;
172 intel_texobj
->validated_first_level
= 0;
173 intel_texobj
->validated_last_level
= levels
- 1;
174 intel_texobj
->_Format
= intel_texobj
->mt
->format
;
181 intel_free_texture_image_buffer(struct gl_context
* ctx
,
182 struct gl_texture_image
*texImage
)
184 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
186 DBG("%s\n", __func__
);
188 intel_miptree_release(&intelImage
->mt
);
190 _swrast_free_texture_image_buffer(ctx
, texImage
);
194 * Map texture memory/buffer into user space.
195 * Note: the region of interest parameters are ignored here.
196 * \param mode bitmask of GL_MAP_READ_BIT, GL_MAP_WRITE_BIT
197 * \param mapOut returns start of mapping of region of interest
198 * \param rowStrideOut returns row stride in bytes
201 intel_map_texture_image(struct gl_context
*ctx
,
202 struct gl_texture_image
*tex_image
,
204 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
209 struct brw_context
*brw
= brw_context(ctx
);
210 struct intel_texture_image
*intel_image
= intel_texture_image(tex_image
);
211 struct intel_mipmap_tree
*mt
= intel_image
->mt
;
214 /* Our texture data is always stored in a miptree. */
217 /* Check that our caller wasn't confused about how to map a 1D texture. */
218 assert(tex_image
->TexObject
->Target
!= GL_TEXTURE_1D_ARRAY
||
221 /* intel_miptree_map operates on a unified "slice" number that references the
222 * cube face, since it's all just slices to the miptree code.
224 if (tex_image
->TexObject
->Target
== GL_TEXTURE_CUBE_MAP
)
225 slice
= tex_image
->Face
;
227 intel_miptree_map(brw
, mt
,
228 tex_image
->Level
+ tex_image
->TexObject
->MinLevel
,
229 slice
+ tex_image
->TexObject
->MinLayer
,
231 (void **)map
, &stride
);
233 *out_stride
= stride
;
237 intel_unmap_texture_image(struct gl_context
*ctx
,
238 struct gl_texture_image
*tex_image
, GLuint slice
)
240 struct brw_context
*brw
= brw_context(ctx
);
241 struct intel_texture_image
*intel_image
= intel_texture_image(tex_image
);
242 struct intel_mipmap_tree
*mt
= intel_image
->mt
;
244 if (tex_image
->TexObject
->Target
== GL_TEXTURE_CUBE_MAP
)
245 slice
= tex_image
->Face
;
247 intel_miptree_unmap(brw
, mt
,
248 tex_image
->Level
+ tex_image
->TexObject
->MinLevel
,
249 slice
+ tex_image
->TexObject
->MinLayer
);
253 intel_texture_view(struct gl_context
*ctx
,
254 struct gl_texture_object
*texObj
,
255 struct gl_texture_object
*origTexObj
)
257 struct brw_context
*brw
= brw_context(ctx
);
258 struct intel_texture_object
*intel_tex
= intel_texture_object(texObj
);
259 struct intel_texture_object
*intel_orig_tex
= intel_texture_object(origTexObj
);
261 assert(intel_orig_tex
->mt
);
262 intel_miptree_reference(&intel_tex
->mt
, intel_orig_tex
->mt
);
264 /* Since we can only make views of immutable-format textures,
265 * we can assume that everything is in origTexObj's miptree.
267 * Mesa core has already made us a copy of all the teximage objects,
268 * except it hasn't copied our mt pointers, etc.
270 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
271 const int numLevels
= texObj
->NumLevels
;
276 for (face
= 0; face
< numFaces
; face
++) {
277 for (level
= 0; level
< numLevels
; level
++) {
278 struct gl_texture_image
*image
= texObj
->Image
[face
][level
];
279 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
281 intel_miptree_reference(&intel_image
->mt
, intel_orig_tex
->mt
);
285 /* The miptree is in a validated state, so no need to check later. */
286 intel_tex
->needs_validate
= false;
287 intel_tex
->validated_first_level
= 0;
288 intel_tex
->validated_last_level
= numLevels
- 1;
290 /* Set the validated texture format, with the same adjustments that
291 * would have been applied to determine the underlying texture's
294 intel_tex
->_Format
= intel_depth_format_for_depthstencil_format(
295 intel_lower_compressed_format(brw
, texObj
->Image
[0][0]->TexFormat
));
301 intel_set_texture_storage_for_buffer_object(struct gl_context
*ctx
,
302 struct gl_texture_object
*tex_obj
,
303 struct gl_buffer_object
*buffer_obj
,
304 uint32_t buffer_offset
,
308 struct brw_context
*brw
= brw_context(ctx
);
309 struct intel_texture_object
*intel_texobj
= intel_texture_object(tex_obj
);
310 struct gl_texture_image
*image
= tex_obj
->Image
[0][0];
311 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
312 struct intel_buffer_object
*intel_buffer_obj
= intel_buffer_object(buffer_obj
);
315 /* Renderbuffers have the restriction that the buffer offset and
316 * surface pitch must be a multiple of the element size. If it's
317 * not, we have to fail and fall back to software.
319 int cpp
= _mesa_get_format_bytes(image
->TexFormat
);
320 if (buffer_offset
% cpp
|| row_stride
% cpp
) {
321 perf_debug("Bad PBO alignment; fallback to CPU mapping\n");
325 if (!brw
->format_supported_as_render_target
[image
->TexFormat
]) {
326 perf_debug("Non-renderable PBO format; fallback to CPU mapping\n");
331 assert(intel_texobj
->mt
== NULL
);
333 drm_intel_bo
*bo
= intel_bufferobj_buffer(brw
, intel_buffer_obj
,
335 row_stride
* image
->Height
);
337 intel_miptree_create_for_bo(brw
, bo
,
340 image
->Width
, image
->Height
, image
->Depth
,
343 if (!intel_texobj
->mt
)
346 if (!_swrast_init_texture_image(image
))
349 intel_miptree_reference(&intel_image
->mt
, intel_texobj
->mt
);
351 /* The miptree is in a validated state, so no need to check later. */
352 intel_texobj
->needs_validate
= false;
353 intel_texobj
->validated_first_level
= 0;
354 intel_texobj
->validated_last_level
= 0;
355 intel_texobj
->_Format
= intel_texobj
->mt
->format
;
361 intel_texture_barrier(struct gl_context
*ctx
)
363 struct brw_context
*brw
= brw_context(ctx
);
365 brw_emit_mi_flush(brw
);
369 intelInitTextureFuncs(struct dd_function_table
*functions
)
371 functions
->NewTextureObject
= intelNewTextureObject
;
372 functions
->NewTextureImage
= intelNewTextureImage
;
373 functions
->DeleteTextureImage
= intelDeleteTextureImage
;
374 functions
->DeleteTexture
= intelDeleteTextureObject
;
375 functions
->AllocTextureImageBuffer
= intel_alloc_texture_image_buffer
;
376 functions
->FreeTextureImageBuffer
= intel_free_texture_image_buffer
;
377 functions
->AllocTextureStorage
= intel_alloc_texture_storage
;
378 functions
->MapTextureImage
= intel_map_texture_image
;
379 functions
->UnmapTextureImage
= intel_unmap_texture_image
;
380 functions
->TextureView
= intel_texture_view
;
381 functions
->SetTextureStorageForBufferObject
=
382 intel_set_texture_storage_for_buffer_object
;
383 functions
->TextureBarrier
= intel_texture_barrier
;