• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "brw_buffer_objects.h"
10 #include "brw_mipmap_tree.h"
11 #include "brw_tex.h"
12 #include "brw_fbo.h"
13 #include "brw_state.h"
14 #include "util/u_memory.h"
15 
16 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
17 
18 static struct gl_texture_image *
brw_new_texture_image(struct gl_context * ctx)19 brw_new_texture_image(struct gl_context *ctx)
20 {
21    DBG("%s\n", __func__);
22    (void) ctx;
23    return (struct gl_texture_image *) CALLOC_STRUCT(brw_texture_image);
24 }
25 
26 static void
brw_delete_texture_image(struct gl_context * ctx,struct gl_texture_image * img)27 brw_delete_texture_image(struct gl_context *ctx, struct gl_texture_image *img)
28 {
29    /* nothing special (yet) for brw_texture_image */
30    _mesa_delete_texture_image(ctx, img);
31 }
32 
33 
34 static struct gl_texture_object *
brw_new_texture_object(struct gl_context * ctx,GLuint name,GLenum target)35 brw_new_texture_object(struct gl_context *ctx, GLuint name, GLenum target)
36 {
37    struct brw_texture_object *obj = CALLOC_STRUCT(brw_texture_object);
38 
39    (void) ctx;
40 
41    DBG("%s\n", __func__);
42 
43    if (obj == NULL)
44       return NULL;
45 
46    _mesa_initialize_texture_object(ctx, &obj->base, name, target);
47 
48    obj->needs_validate = true;
49 
50    return &obj->base;
51 }
52 
53 static void
brw_delete_texture_object(struct gl_context * ctx,struct gl_texture_object * texObj)54 brw_delete_texture_object(struct gl_context *ctx,
55                           struct gl_texture_object *texObj)
56 {
57    struct brw_texture_object *brw_obj = brw_texture_object(texObj);
58 
59    brw_miptree_release(&brw_obj->mt);
60    _mesa_delete_texture_object(ctx, texObj);
61 }
62 
63 static GLboolean
brw_alloc_texture_image_buffer(struct gl_context * ctx,struct gl_texture_image * image)64 brw_alloc_texture_image_buffer(struct gl_context *ctx,
65                                struct gl_texture_image *image)
66 {
67    struct brw_context *brw = brw_context(ctx);
68    struct brw_texture_image *intel_image = brw_texture_image(image);
69    struct gl_texture_object *texobj = image->TexObject;
70    struct brw_texture_object *intel_texobj = brw_texture_object(texobj);
71 
72    assert(image->Border == 0);
73 
74    /* Quantize sample count */
75    if (image->NumSamples) {
76       image->NumSamples = brw_quantize_num_samples(brw->screen, image->NumSamples);
77       if (!image->NumSamples)
78          return false;
79    }
80 
81    /* Because the driver uses AllocTextureImageBuffer() internally, it may end
82     * up mismatched with FreeTextureImageBuffer(), but that is safe to call
83     * multiple times.
84     */
85    ctx->Driver.FreeTextureImageBuffer(ctx, image);
86 
87    if (!_swrast_init_texture_image(image))
88       return false;
89 
90    if (intel_texobj->mt &&
91        brw_miptree_match_image(intel_texobj->mt, image)) {
92       brw_miptree_reference(&intel_image->mt, intel_texobj->mt);
93       DBG("%s: alloc obj %p level %d %dx%dx%d using object's miptree %p\n",
94           __func__, texobj, image->Level,
95           image->Width, image->Height, image->Depth, intel_texobj->mt);
96    } else {
97       intel_image->mt = brw_miptree_create_for_teximage(brw, intel_texobj,
98                                                           intel_image,
99                                                           MIPTREE_CREATE_DEFAULT);
100       if (!intel_image->mt)
101          return false;
102 
103       /* Even if the object currently has a mipmap tree associated
104        * with it, this one is a more likely candidate to represent the
105        * whole object since our level didn't fit what was there
106        * before, and any lower levels would fit into our miptree.
107        */
108       brw_miptree_reference(&intel_texobj->mt, intel_image->mt);
109 
110       DBG("%s: alloc obj %p level %d %dx%dx%d using new miptree %p\n",
111           __func__, texobj, image->Level,
112           image->Width, image->Height, image->Depth, intel_image->mt);
113    }
114 
115    intel_texobj->needs_validate = true;
116 
117    return true;
118 }
119 
120 /**
121  * ctx->Driver.AllocTextureStorage() handler.
122  *
123  * Compare this to _mesa_AllocTextureStorage_sw, which would call into
124  * brw_alloc_texture_image_buffer() above.
125  */
126 static GLboolean
brw_alloc_texture_storage(struct gl_context * ctx,struct gl_texture_object * texobj,GLsizei levels,GLsizei width,GLsizei height,GLsizei depth)127 brw_alloc_texture_storage(struct gl_context *ctx,
128                           struct gl_texture_object *texobj,
129                           GLsizei levels, GLsizei width,
130                           GLsizei height, GLsizei depth)
131 {
132    struct brw_context *brw = brw_context(ctx);
133    struct brw_texture_object *intel_texobj = brw_texture_object(texobj);
134    struct gl_texture_image *first_image = texobj->Image[0][0];
135    int num_samples = brw_quantize_num_samples(brw->screen,
136                                                 first_image->NumSamples);
137    const int numFaces = _mesa_num_tex_faces(texobj->Target);
138    int face;
139    int level;
140 
141    /* If the object's current miptree doesn't match what we need, make a new
142     * one.
143     */
144    if (!intel_texobj->mt ||
145        !brw_miptree_match_image(intel_texobj->mt, first_image) ||
146        intel_texobj->mt->last_level != levels - 1) {
147       brw_miptree_release(&intel_texobj->mt);
148 
149       brw_get_image_dims(first_image, &width, &height, &depth);
150       intel_texobj->mt = brw_miptree_create(brw, texobj->Target,
151                                               first_image->TexFormat,
152                                               0, levels - 1,
153                                               width, height, depth,
154                                               MAX2(num_samples, 1),
155                                               MIPTREE_CREATE_DEFAULT);
156 
157       if (intel_texobj->mt == NULL) {
158          return false;
159       }
160    }
161 
162    for (face = 0; face < numFaces; face++) {
163       for (level = 0; level < levels; level++) {
164          struct gl_texture_image *image = texobj->Image[face][level];
165          struct brw_texture_image *intel_image = brw_texture_image(image);
166 
167          image->NumSamples = num_samples;
168 
169          _swrast_free_texture_image_buffer(ctx, image);
170          if (!_swrast_init_texture_image(image))
171             return false;
172 
173          brw_miptree_reference(&intel_image->mt, intel_texobj->mt);
174       }
175    }
176 
177    /* The miptree is in a validated state, so no need to check later. */
178    intel_texobj->needs_validate = false;
179    intel_texobj->validated_first_level = 0;
180    intel_texobj->validated_last_level = levels - 1;
181    intel_texobj->_Format = first_image->TexFormat;
182 
183    return true;
184 }
185 
186 
187 static void
brw_free_texture_image_buffer(struct gl_context * ctx,struct gl_texture_image * texImage)188 brw_free_texture_image_buffer(struct gl_context * ctx,
189                               struct gl_texture_image *texImage)
190 {
191    struct brw_texture_image *brw_image = brw_texture_image(texImage);
192 
193    DBG("%s\n", __func__);
194 
195    brw_miptree_release(&brw_image->mt);
196 
197    _swrast_free_texture_image_buffer(ctx, texImage);
198 }
199 
200 /**
201  * Map texture memory/buffer into user space.
202  * Note: the region of interest parameters are ignored here.
203  * \param mode  bitmask of GL_MAP_READ_BIT, GL_MAP_WRITE_BIT
204  * \param mapOut  returns start of mapping of region of interest
205  * \param rowStrideOut  returns row stride in bytes
206  */
207 static void
brw_map_texture_image(struct gl_context * ctx,struct gl_texture_image * tex_image,GLuint slice,GLuint x,GLuint y,GLuint w,GLuint h,GLbitfield mode,GLubyte ** map,GLint * out_stride)208 brw_map_texture_image(struct gl_context *ctx,
209                       struct gl_texture_image *tex_image,
210                       GLuint slice,
211                       GLuint x, GLuint y, GLuint w, GLuint h,
212                       GLbitfield mode,
213                       GLubyte **map,
214                       GLint *out_stride)
215 {
216    struct brw_context *brw = brw_context(ctx);
217    struct brw_texture_image *intel_image = brw_texture_image(tex_image);
218    struct brw_mipmap_tree *mt = intel_image->mt;
219    ptrdiff_t stride;
220 
221    /* Our texture data is always stored in a miptree. */
222    assert(mt);
223 
224    /* Check that our caller wasn't confused about how to map a 1D texture. */
225    assert(tex_image->TexObject->Target != GL_TEXTURE_1D_ARRAY || h == 1);
226 
227    /* brw_miptree_map operates on a unified "slice" number that references the
228     * cube face, since it's all just slices to the miptree code.
229     */
230    if (tex_image->TexObject->Target == GL_TEXTURE_CUBE_MAP)
231       slice = tex_image->Face;
232 
233    brw_miptree_map(brw, mt,
234                      tex_image->Level + tex_image->TexObject->Attrib.MinLevel,
235                      slice + tex_image->TexObject->Attrib.MinLayer,
236                      x, y, w, h, mode,
237                      (void **)map, &stride);
238 
239    *out_stride = stride;
240 }
241 
242 static void
brw_unmap_texture_image(struct gl_context * ctx,struct gl_texture_image * tex_image,GLuint slice)243 brw_unmap_texture_image(struct gl_context *ctx,
244                         struct gl_texture_image *tex_image, GLuint slice)
245 {
246    struct brw_context *brw = brw_context(ctx);
247    struct brw_texture_image *intel_image = brw_texture_image(tex_image);
248    struct brw_mipmap_tree *mt = intel_image->mt;
249 
250    if (tex_image->TexObject->Target == GL_TEXTURE_CUBE_MAP)
251       slice = tex_image->Face;
252 
253    brw_miptree_unmap(brw, mt,
254          tex_image->Level + tex_image->TexObject->Attrib.MinLevel,
255          slice + tex_image->TexObject->Attrib.MinLayer);
256 }
257 
258 static GLboolean
brw_texture_view(struct gl_context * ctx,struct gl_texture_object * texObj,struct gl_texture_object * origTexObj)259 brw_texture_view(struct gl_context *ctx,
260                  struct gl_texture_object *texObj,
261                  struct gl_texture_object *origTexObj)
262 {
263    struct brw_context *brw = brw_context(ctx);
264    struct brw_texture_object *intel_tex = brw_texture_object(texObj);
265    struct brw_texture_object *intel_orig_tex = brw_texture_object(origTexObj);
266 
267    assert(intel_orig_tex->mt);
268    brw_miptree_reference(&intel_tex->mt, intel_orig_tex->mt);
269 
270    /* Since we can only make views of immutable-format textures,
271     * we can assume that everything is in origTexObj's miptree.
272     *
273     * Mesa core has already made us a copy of all the teximage objects,
274     * except it hasn't copied our mt pointers, etc.
275     */
276    const int numFaces = _mesa_num_tex_faces(texObj->Target);
277    const int numLevels = texObj->Attrib.NumLevels;
278 
279    int face;
280    int level;
281 
282    for (face = 0; face < numFaces; face++) {
283       for (level = 0; level < numLevels; level++) {
284          struct gl_texture_image *image = texObj->Image[face][level];
285          struct brw_texture_image *intel_image = brw_texture_image(image);
286 
287          brw_miptree_reference(&intel_image->mt, intel_orig_tex->mt);
288       }
289    }
290 
291    /* The miptree is in a validated state, so no need to check later. */
292    intel_tex->needs_validate = false;
293    intel_tex->validated_first_level = 0;
294    intel_tex->validated_last_level = numLevels - 1;
295 
296    /* Set the validated texture format, with the same adjustments that
297     * would have been applied to determine the underlying texture's
298     * mt->format.
299     */
300    intel_tex->_Format = brw_depth_format_for_depthstencil_format(
301          brw_lower_compressed_format(brw, texObj->Image[0][0]->TexFormat));
302 
303    return GL_TRUE;
304 }
305 
306 static void
brw_texture_barrier(struct gl_context * ctx)307 brw_texture_barrier(struct gl_context *ctx)
308 {
309    struct brw_context *brw = brw_context(ctx);
310    const struct intel_device_info *devinfo = &brw->screen->devinfo;
311 
312    if (devinfo->ver >= 6) {
313       brw_emit_pipe_control_flush(brw,
314                                   PIPE_CONTROL_DEPTH_CACHE_FLUSH |
315                                   PIPE_CONTROL_RENDER_TARGET_FLUSH |
316                                   PIPE_CONTROL_CS_STALL);
317 
318       brw_emit_pipe_control_flush(brw,
319                                   PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
320    } else {
321       brw_emit_mi_flush(brw);
322    }
323 }
324 
325 /* Return the usual surface usage flags for the given format. */
326 static isl_surf_usage_flags_t
isl_surf_usage(mesa_format format)327 isl_surf_usage(mesa_format format)
328 {
329    switch(_mesa_get_format_base_format(format)) {
330    case GL_DEPTH_COMPONENT:
331       return ISL_SURF_USAGE_DEPTH_BIT | ISL_SURF_USAGE_TEXTURE_BIT;
332    case GL_DEPTH_STENCIL:
333       return ISL_SURF_USAGE_DEPTH_BIT | ISL_SURF_USAGE_STENCIL_BIT |
334              ISL_SURF_USAGE_TEXTURE_BIT;
335    case GL_STENCIL_INDEX:
336       return ISL_SURF_USAGE_STENCIL_BIT | ISL_SURF_USAGE_TEXTURE_BIT;
337    default:
338       return ISL_SURF_USAGE_RENDER_TARGET_BIT | ISL_SURF_USAGE_TEXTURE_BIT;
339    }
340 }
341 
342 static GLboolean
intel_texture_for_memory_object(struct gl_context * ctx,struct gl_texture_object * tex_obj,struct gl_memory_object * mem_obj,GLsizei levels,GLsizei width,GLsizei height,GLsizei depth,GLuint64 offset)343 intel_texture_for_memory_object(struct gl_context *ctx,
344                                           struct gl_texture_object *tex_obj,
345                                           struct gl_memory_object *mem_obj,
346                                           GLsizei levels, GLsizei width,
347                                           GLsizei height, GLsizei depth,
348                                           GLuint64 offset)
349 {
350    struct brw_context *brw = brw_context(ctx);
351    struct brw_memory_object *intel_memobj = brw_memory_object(mem_obj);
352    struct brw_texture_object *intel_texobj = brw_texture_object(tex_obj);
353    struct gl_texture_image *image = tex_obj->Image[0][0];
354    struct isl_surf surf;
355 
356    /* Only color formats are supported. */
357    if (!_mesa_is_format_color_format(image->TexFormat))
358       return GL_FALSE;
359 
360    isl_tiling_flags_t tiling_flags = ISL_TILING_ANY_MASK;
361    if (tex_obj->TextureTiling == GL_LINEAR_TILING_EXT)
362       tiling_flags = ISL_TILING_LINEAR_BIT;
363 
364    UNUSED const bool isl_surf_created_successfully =
365       isl_surf_init(&brw->screen->isl_dev, &surf,
366                     .dim = get_isl_surf_dim(tex_obj->Target),
367                     .format = brw_isl_format_for_mesa_format(image->TexFormat),
368                     .width = width,
369                     .height = height,
370                     .depth = depth,
371                     .levels = levels,
372                     .array_len = tex_obj->Target == GL_TEXTURE_3D ? 1 : depth,
373                     .samples = MAX2(image->NumSamples, 1),
374                     .usage = isl_surf_usage(image->TexFormat),
375                     .tiling_flags = tiling_flags);
376 
377    assert(isl_surf_created_successfully);
378 
379    intel_texobj->mt = brw_miptree_create_for_bo(brw,
380                                                 intel_memobj->bo,
381                                                 image->TexFormat,
382                                                 offset,
383                                                 width,
384                                                 height,
385                                                 depth,
386                                                 surf.row_pitch_B,
387                                                 surf.tiling,
388                                                 MIPTREE_CREATE_NO_AUX);
389    assert(intel_texobj->mt);
390    brw_alloc_texture_image_buffer(ctx, image);
391 
392    intel_texobj->needs_validate = false;
393    intel_texobj->validated_first_level = 0;
394    intel_texobj->validated_last_level = levels - 1;
395    intel_texobj->_Format = image->TexFormat;
396 
397    return GL_TRUE;
398 }
399 
400 void
brw_init_texture_functions(struct dd_function_table * functions)401 brw_init_texture_functions(struct dd_function_table *functions)
402 {
403    functions->NewTextureObject = brw_new_texture_object;
404    functions->NewTextureImage = brw_new_texture_image;
405    functions->DeleteTextureImage = brw_delete_texture_image;
406    functions->DeleteTexture = brw_delete_texture_object;
407    functions->AllocTextureImageBuffer = brw_alloc_texture_image_buffer;
408    functions->FreeTextureImageBuffer = brw_free_texture_image_buffer;
409    functions->AllocTextureStorage = brw_alloc_texture_storage;
410    functions->MapTextureImage = brw_map_texture_image;
411    functions->UnmapTextureImage = brw_unmap_texture_image;
412    functions->TextureView = brw_texture_view;
413    functions->TextureBarrier = brw_texture_barrier;
414    functions->SetTextureStorageForMemoryObject = intel_texture_for_memory_object;
415 }
416