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 "brw_defines.h"
9 #include "intel_buffer_objects.h"
10 #include "intel_mipmap_tree.h"
11 #include "intel_tex.h"
12 #include "intel_fbo.h"
14 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
16 static struct gl_texture_image
*
17 intelNewTextureImage(struct gl_context
* ctx
)
19 DBG("%s\n", __func__
);
21 return (struct gl_texture_image
*) CALLOC_STRUCT(intel_texture_image
);
25 intelDeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
27 /* nothing special (yet) for intel_texture_image */
28 _mesa_delete_texture_image(ctx
, img
);
32 static struct gl_texture_object
*
33 intelNewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
35 struct intel_texture_object
*obj
= CALLOC_STRUCT(intel_texture_object
);
39 DBG("%s\n", __func__
);
44 _mesa_initialize_texture_object(ctx
, &obj
->base
, name
, target
);
46 obj
->needs_validate
= true;
52 intelDeleteTextureObject(struct gl_context
*ctx
,
53 struct gl_texture_object
*texObj
)
55 struct intel_texture_object
*intelObj
= intel_texture_object(texObj
);
57 intel_miptree_release(&intelObj
->mt
);
58 _mesa_delete_texture_object(ctx
, texObj
);
62 intel_alloc_texture_image_buffer(struct gl_context
*ctx
,
63 struct gl_texture_image
*image
)
65 struct brw_context
*brw
= brw_context(ctx
);
66 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
67 struct gl_texture_object
*texobj
= image
->TexObject
;
68 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
70 assert(image
->Border
== 0);
72 /* Quantize sample count */
73 if (image
->NumSamples
) {
74 image
->NumSamples
= intel_quantize_num_samples(brw
->screen
, image
->NumSamples
);
75 if (!image
->NumSamples
)
79 /* Because the driver uses AllocTextureImageBuffer() internally, it may end
80 * up mismatched with FreeTextureImageBuffer(), but that is safe to call
83 ctx
->Driver
.FreeTextureImageBuffer(ctx
, image
);
85 if (!_swrast_init_texture_image(image
))
88 if (intel_texobj
->mt
&&
89 intel_miptree_match_image(intel_texobj
->mt
, image
)) {
90 intel_miptree_reference(&intel_image
->mt
, intel_texobj
->mt
);
91 DBG("%s: alloc obj %p level %d %dx%dx%d using object's miptree %p\n",
92 __func__
, texobj
, image
->Level
,
93 image
->Width
, image
->Height
, image
->Depth
, intel_texobj
->mt
);
95 intel_image
->mt
= intel_miptree_create_for_teximage(brw
, intel_texobj
,
101 /* Even if the object currently has a mipmap tree associated
102 * with it, this one is a more likely candidate to represent the
103 * whole object since our level didn't fit what was there
104 * before, and any lower levels would fit into our miptree.
106 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
108 DBG("%s: alloc obj %p level %d %dx%dx%d using new miptree %p\n",
109 __func__
, texobj
, image
->Level
,
110 image
->Width
, image
->Height
, image
->Depth
, intel_image
->mt
);
113 intel_texobj
->needs_validate
= true;
119 * ctx->Driver.AllocTextureStorage() handler.
121 * Compare this to _mesa_AllocTextureStorage_sw, which would call into
122 * intel_alloc_texture_image_buffer() above.
125 intel_alloc_texture_storage(struct gl_context
*ctx
,
126 struct gl_texture_object
*texobj
,
127 GLsizei levels
, GLsizei width
,
128 GLsizei height
, GLsizei depth
)
130 struct brw_context
*brw
= brw_context(ctx
);
131 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
132 struct gl_texture_image
*first_image
= texobj
->Image
[0][0];
133 int num_samples
= intel_quantize_num_samples(brw
->screen
,
134 first_image
->NumSamples
);
135 const int numFaces
= _mesa_num_tex_faces(texobj
->Target
);
139 /* If the object's current miptree doesn't match what we need, make a new
142 if (!intel_texobj
->mt
||
143 !intel_miptree_match_image(intel_texobj
->mt
, first_image
) ||
144 intel_texobj
->mt
->last_level
!= levels
- 1) {
145 intel_miptree_release(&intel_texobj
->mt
);
147 intel_get_image_dims(first_image
, &width
, &height
, &depth
);
148 intel_texobj
->mt
= intel_miptree_create(brw
, texobj
->Target
,
149 first_image
->TexFormat
,
151 width
, height
, depth
,
152 MAX2(num_samples
, 1),
153 MIPTREE_LAYOUT_TILING_ANY
);
155 if (intel_texobj
->mt
== NULL
) {
160 for (face
= 0; face
< numFaces
; face
++) {
161 for (level
= 0; level
< levels
; level
++) {
162 struct gl_texture_image
*image
= texobj
->Image
[face
][level
];
163 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
165 image
->NumSamples
= num_samples
;
167 _swrast_free_texture_image_buffer(ctx
, image
);
168 if (!_swrast_init_texture_image(image
))
171 intel_miptree_reference(&intel_image
->mt
, intel_texobj
->mt
);
175 /* The miptree is in a validated state, so no need to check later. */
176 intel_texobj
->needs_validate
= false;
177 intel_texobj
->validated_first_level
= 0;
178 intel_texobj
->validated_last_level
= levels
- 1;
179 intel_texobj
->_Format
= intel_texobj
->mt
->format
;
186 intel_free_texture_image_buffer(struct gl_context
* ctx
,
187 struct gl_texture_image
*texImage
)
189 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
191 DBG("%s\n", __func__
);
193 intel_miptree_release(&intelImage
->mt
);
195 _swrast_free_texture_image_buffer(ctx
, texImage
);
199 * Map texture memory/buffer into user space.
200 * Note: the region of interest parameters are ignored here.
201 * \param mode bitmask of GL_MAP_READ_BIT, GL_MAP_WRITE_BIT
202 * \param mapOut returns start of mapping of region of interest
203 * \param rowStrideOut returns row stride in bytes
206 intel_map_texture_image(struct gl_context
*ctx
,
207 struct gl_texture_image
*tex_image
,
209 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
214 struct brw_context
*brw
= brw_context(ctx
);
215 struct intel_texture_image
*intel_image
= intel_texture_image(tex_image
);
216 struct intel_mipmap_tree
*mt
= intel_image
->mt
;
219 /* Our texture data is always stored in a miptree. */
222 /* Check that our caller wasn't confused about how to map a 1D texture. */
223 assert(tex_image
->TexObject
->Target
!= GL_TEXTURE_1D_ARRAY
||
226 /* intel_miptree_map operates on a unified "slice" number that references the
227 * cube face, since it's all just slices to the miptree code.
229 if (tex_image
->TexObject
->Target
== GL_TEXTURE_CUBE_MAP
)
230 slice
= tex_image
->Face
;
232 intel_miptree_map(brw
, mt
,
233 tex_image
->Level
+ tex_image
->TexObject
->MinLevel
,
234 slice
+ tex_image
->TexObject
->MinLayer
,
236 (void **)map
, &stride
);
238 *out_stride
= stride
;
242 intel_unmap_texture_image(struct gl_context
*ctx
,
243 struct gl_texture_image
*tex_image
, GLuint slice
)
245 struct brw_context
*brw
= brw_context(ctx
);
246 struct intel_texture_image
*intel_image
= intel_texture_image(tex_image
);
247 struct intel_mipmap_tree
*mt
= intel_image
->mt
;
249 if (tex_image
->TexObject
->Target
== GL_TEXTURE_CUBE_MAP
)
250 slice
= tex_image
->Face
;
252 intel_miptree_unmap(brw
, mt
,
253 tex_image
->Level
+ tex_image
->TexObject
->MinLevel
,
254 slice
+ tex_image
->TexObject
->MinLayer
);
258 intel_texture_view(struct gl_context
*ctx
,
259 struct gl_texture_object
*texObj
,
260 struct gl_texture_object
*origTexObj
)
262 struct brw_context
*brw
= brw_context(ctx
);
263 struct intel_texture_object
*intel_tex
= intel_texture_object(texObj
);
264 struct intel_texture_object
*intel_orig_tex
= intel_texture_object(origTexObj
);
266 assert(intel_orig_tex
->mt
);
267 intel_miptree_reference(&intel_tex
->mt
, intel_orig_tex
->mt
);
269 /* Since we can only make views of immutable-format textures,
270 * we can assume that everything is in origTexObj's miptree.
272 * Mesa core has already made us a copy of all the teximage objects,
273 * except it hasn't copied our mt pointers, etc.
275 const int numFaces
= _mesa_num_tex_faces(texObj
->Target
);
276 const int numLevels
= texObj
->NumLevels
;
281 for (face
= 0; face
< numFaces
; face
++) {
282 for (level
= 0; level
< numLevels
; level
++) {
283 struct gl_texture_image
*image
= texObj
->Image
[face
][level
];
284 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
286 intel_miptree_reference(&intel_image
->mt
, intel_orig_tex
->mt
);
290 /* The miptree is in a validated state, so no need to check later. */
291 intel_tex
->needs_validate
= false;
292 intel_tex
->validated_first_level
= 0;
293 intel_tex
->validated_last_level
= numLevels
- 1;
295 /* Set the validated texture format, with the same adjustments that
296 * would have been applied to determine the underlying texture's
299 intel_tex
->_Format
= intel_depth_format_for_depthstencil_format(
300 intel_lower_compressed_format(brw
, texObj
->Image
[0][0]->TexFormat
));
306 intel_set_texture_storage_for_buffer_object(struct gl_context
*ctx
,
307 struct gl_texture_object
*tex_obj
,
308 struct gl_buffer_object
*buffer_obj
,
309 uint32_t buffer_offset
,
313 struct brw_context
*brw
= brw_context(ctx
);
314 struct intel_texture_object
*intel_texobj
= intel_texture_object(tex_obj
);
315 struct gl_texture_image
*image
= tex_obj
->Image
[0][0];
316 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
317 struct intel_buffer_object
*intel_buffer_obj
= intel_buffer_object(buffer_obj
);
320 /* Renderbuffers have the restriction that the buffer offset and
321 * surface pitch must be a multiple of the element size. If it's
322 * not, we have to fail and fall back to software.
324 int cpp
= _mesa_get_format_bytes(image
->TexFormat
);
325 if (buffer_offset
% cpp
|| row_stride
% cpp
) {
326 perf_debug("Bad PBO alignment; fallback to CPU mapping\n");
330 if (!brw
->mesa_format_supports_render
[image
->TexFormat
]) {
331 perf_debug("Non-renderable PBO format; fallback to CPU mapping\n");
336 assert(intel_texobj
->mt
== NULL
);
338 struct brw_bo
*bo
= intel_bufferobj_buffer(brw
, intel_buffer_obj
,
340 row_stride
* image
->Height
,
343 intel_miptree_create_for_bo(brw
, bo
,
346 image
->Width
, image
->Height
, image
->Depth
,
349 if (!intel_texobj
->mt
)
352 if (!_swrast_init_texture_image(image
))
355 intel_miptree_reference(&intel_image
->mt
, intel_texobj
->mt
);
357 /* The miptree is in a validated state, so no need to check later. */
358 intel_texobj
->needs_validate
= false;
359 intel_texobj
->validated_first_level
= 0;
360 intel_texobj
->validated_last_level
= 0;
361 intel_texobj
->_Format
= intel_texobj
->mt
->format
;
367 intel_texture_barrier(struct gl_context
*ctx
)
369 struct brw_context
*brw
= brw_context(ctx
);
372 brw_emit_pipe_control_flush(brw
,
373 PIPE_CONTROL_DEPTH_CACHE_FLUSH
|
374 PIPE_CONTROL_RENDER_TARGET_FLUSH
|
375 PIPE_CONTROL_CS_STALL
);
377 brw_emit_pipe_control_flush(brw
,
378 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE
);
380 brw_emit_mi_flush(brw
);
385 intelInitTextureFuncs(struct dd_function_table
*functions
)
387 functions
->NewTextureObject
= intelNewTextureObject
;
388 functions
->NewTextureImage
= intelNewTextureImage
;
389 functions
->DeleteTextureImage
= intelDeleteTextureImage
;
390 functions
->DeleteTexture
= intelDeleteTextureObject
;
391 functions
->AllocTextureImageBuffer
= intel_alloc_texture_image_buffer
;
392 functions
->FreeTextureImageBuffer
= intel_free_texture_image_buffer
;
393 functions
->AllocTextureStorage
= intel_alloc_texture_storage
;
394 functions
->MapTextureImage
= intel_map_texture_image
;
395 functions
->UnmapTextureImage
= intel_unmap_texture_image
;
396 functions
->TextureView
= intel_texture_view
;
397 functions
->SetTextureStorageForBufferObject
=
398 intel_set_texture_storage_for_buffer_object
;
399 functions
->TextureBarrier
= intel_texture_barrier
;