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_mipmap_tree.h"
10 #include "intel_fbo.h"
12 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
14 static struct gl_texture_image
*
15 intelNewTextureImage(struct gl_context
* ctx
)
17 DBG("%s\n", __FUNCTION__
);
19 return (struct gl_texture_image
*) CALLOC_STRUCT(intel_texture_image
);
23 intelDeleteTextureImage(struct gl_context
* ctx
, struct gl_texture_image
*img
)
25 /* nothing special (yet) for intel_texture_image */
26 _mesa_delete_texture_image(ctx
, img
);
30 static struct gl_texture_object
*
31 intelNewTextureObject(struct gl_context
* ctx
, GLuint name
, GLenum target
)
33 struct intel_texture_object
*obj
= CALLOC_STRUCT(intel_texture_object
);
37 DBG("%s\n", __FUNCTION__
);
42 _mesa_initialize_texture_object(ctx
, &obj
->base
, name
, target
);
44 obj
->needs_validate
= true;
50 intelDeleteTextureObject(struct gl_context
*ctx
,
51 struct gl_texture_object
*texObj
)
53 struct intel_texture_object
*intelObj
= intel_texture_object(texObj
);
55 intel_miptree_release(&intelObj
->mt
);
56 _mesa_delete_texture_object(ctx
, texObj
);
60 intel_alloc_texture_image_buffer(struct gl_context
*ctx
,
61 struct gl_texture_image
*image
)
63 struct brw_context
*brw
= brw_context(ctx
);
64 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
65 struct gl_texture_object
*texobj
= image
->TexObject
;
66 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
68 assert(image
->Border
== 0);
70 /* Quantize sample count */
71 if (image
->NumSamples
) {
72 image
->NumSamples
= intel_quantize_num_samples(brw
->intelScreen
, image
->NumSamples
);
73 if (!image
->NumSamples
)
77 /* Because the driver uses AllocTextureImageBuffer() internally, it may end
78 * up mismatched with FreeTextureImageBuffer(), but that is safe to call
81 ctx
->Driver
.FreeTextureImageBuffer(ctx
, image
);
83 if (!_swrast_init_texture_image(image
))
86 if (intel_texobj
->mt
&&
87 intel_miptree_match_image(intel_texobj
->mt
, image
)) {
88 intel_miptree_reference(&intel_image
->mt
, intel_texobj
->mt
);
89 DBG("%s: alloc obj %p level %d %dx%dx%d using object's miptree %p\n",
90 __FUNCTION__
, texobj
, image
->Level
,
91 image
->Width
, image
->Height
, image
->Depth
, intel_texobj
->mt
);
93 intel_image
->mt
= intel_miptree_create_for_teximage(brw
, intel_texobj
,
97 /* Even if the object currently has a mipmap tree associated
98 * with it, this one is a more likely candidate to represent the
99 * whole object since our level didn't fit what was there
100 * before, and any lower levels would fit into our miptree.
102 intel_miptree_reference(&intel_texobj
->mt
, intel_image
->mt
);
104 DBG("%s: alloc obj %p level %d %dx%dx%d using new miptree %p\n",
105 __FUNCTION__
, texobj
, image
->Level
,
106 image
->Width
, image
->Height
, image
->Depth
, intel_image
->mt
);
109 intel_texobj
->needs_validate
= true;
115 * ctx->Driver.AllocTextureStorage() handler.
117 * Compare this to _mesa_alloc_texture_storage, which would call into
118 * intel_alloc_texture_image_buffer() above.
121 intel_alloc_texture_storage(struct gl_context
*ctx
,
122 struct gl_texture_object
*texobj
,
123 GLsizei levels
, GLsizei width
,
124 GLsizei height
, GLsizei depth
)
126 struct brw_context
*brw
= brw_context(ctx
);
127 struct intel_texture_object
*intel_texobj
= intel_texture_object(texobj
);
128 struct gl_texture_image
*first_image
= texobj
->Image
[0][0];
129 int num_samples
= intel_quantize_num_samples(brw
->intelScreen
,
130 first_image
->NumSamples
);
131 const int numFaces
= _mesa_num_tex_faces(texobj
->Target
);
135 /* If the object's current miptree doesn't match what we need, make a new
138 if (!intel_texobj
->mt
||
139 !intel_miptree_match_image(intel_texobj
->mt
, first_image
) ||
140 intel_texobj
->mt
->last_level
!= levels
- 1) {
141 intel_miptree_release(&intel_texobj
->mt
);
142 intel_texobj
->mt
= intel_miptree_create(brw
, texobj
->Target
,
143 first_image
->TexFormat
,
145 width
, height
, depth
,
146 false, /* expect_accelerated */
148 INTEL_MIPTREE_TILING_ANY
);
152 for (face
= 0; face
< numFaces
; face
++) {
153 for (level
= 0; level
< levels
; level
++) {
154 struct gl_texture_image
*image
= texobj
->Image
[face
][level
];
155 struct intel_texture_image
*intel_image
= intel_texture_image(image
);
157 image
->NumSamples
= num_samples
;
159 _swrast_free_texture_image_buffer(ctx
, image
);
160 if (!_swrast_init_texture_image(image
))
163 intel_miptree_reference(&intel_image
->mt
, intel_texobj
->mt
);
167 /* The miptree is in a validated state, so no need to check later. */
168 intel_texobj
->needs_validate
= false;
169 intel_texobj
->validated_first_level
= 0;
170 intel_texobj
->validated_last_level
= levels
- 1;
177 intel_free_texture_image_buffer(struct gl_context
* ctx
,
178 struct gl_texture_image
*texImage
)
180 struct intel_texture_image
*intelImage
= intel_texture_image(texImage
);
182 DBG("%s\n", __FUNCTION__
);
184 intel_miptree_release(&intelImage
->mt
);
186 _swrast_free_texture_image_buffer(ctx
, texImage
);
190 * Map texture memory/buffer into user space.
191 * Note: the region of interest parameters are ignored here.
192 * \param mode bitmask of GL_MAP_READ_BIT, GL_MAP_WRITE_BIT
193 * \param mapOut returns start of mapping of region of interest
194 * \param rowStrideOut returns row stride in bytes
197 intel_map_texture_image(struct gl_context
*ctx
,
198 struct gl_texture_image
*tex_image
,
200 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
205 struct brw_context
*brw
= brw_context(ctx
);
206 struct intel_texture_image
*intel_image
= intel_texture_image(tex_image
);
207 struct intel_mipmap_tree
*mt
= intel_image
->mt
;
209 /* Our texture data is always stored in a miptree. */
212 /* Check that our caller wasn't confused about how to map a 1D texture. */
213 assert(tex_image
->TexObject
->Target
!= GL_TEXTURE_1D_ARRAY
||
216 /* intel_miptree_map operates on a unified "slice" number that references the
217 * cube face, since it's all just slices to the miptree code.
219 if (tex_image
->TexObject
->Target
== GL_TEXTURE_CUBE_MAP
)
220 slice
= tex_image
->Face
;
222 intel_miptree_map(brw
, mt
, tex_image
->Level
, slice
, x
, y
, w
, h
, mode
,
223 (void **)map
, stride
);
227 intel_unmap_texture_image(struct gl_context
*ctx
,
228 struct gl_texture_image
*tex_image
, GLuint slice
)
230 struct brw_context
*brw
= brw_context(ctx
);
231 struct intel_texture_image
*intel_image
= intel_texture_image(tex_image
);
232 struct intel_mipmap_tree
*mt
= intel_image
->mt
;
234 if (tex_image
->TexObject
->Target
== GL_TEXTURE_CUBE_MAP
)
235 slice
= tex_image
->Face
;
237 intel_miptree_unmap(brw
, mt
, tex_image
->Level
, slice
);
241 intelInitTextureFuncs(struct dd_function_table
*functions
)
243 functions
->NewTextureObject
= intelNewTextureObject
;
244 functions
->NewTextureImage
= intelNewTextureImage
;
245 functions
->DeleteTextureImage
= intelDeleteTextureImage
;
246 functions
->DeleteTexture
= intelDeleteTextureObject
;
247 functions
->AllocTextureImageBuffer
= intel_alloc_texture_image_buffer
;
248 functions
->FreeTextureImageBuffer
= intel_free_texture_image_buffer
;
249 functions
->AllocTextureStorage
= intel_alloc_texture_storage
;
250 functions
->MapTextureImage
= intel_map_texture_image
;
251 functions
->UnmapTextureImage
= intel_unmap_texture_image
;