glsl: Lower UBO and SSBO access in glsl linker
[mesa.git] / src / mesa / drivers / dri / i965 / intel_tex.c
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"
12
13 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
14
15 static struct gl_texture_image *
16 intelNewTextureImage(struct gl_context * ctx)
17 {
18 DBG("%s\n", __func__);
19 (void) ctx;
20 return (struct gl_texture_image *) CALLOC_STRUCT(intel_texture_image);
21 }
22
23 static void
24 intelDeleteTextureImage(struct gl_context * ctx, struct gl_texture_image *img)
25 {
26 /* nothing special (yet) for intel_texture_image */
27 _mesa_delete_texture_image(ctx, img);
28 }
29
30
31 static struct gl_texture_object *
32 intelNewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
33 {
34 struct intel_texture_object *obj = CALLOC_STRUCT(intel_texture_object);
35
36 (void) ctx;
37
38 DBG("%s\n", __func__);
39
40 if (obj == NULL)
41 return NULL;
42
43 _mesa_initialize_texture_object(ctx, &obj->base, name, target);
44
45 obj->needs_validate = true;
46
47 return &obj->base;
48 }
49
50 static void
51 intelDeleteTextureObject(struct gl_context *ctx,
52 struct gl_texture_object *texObj)
53 {
54 struct intel_texture_object *intelObj = intel_texture_object(texObj);
55
56 intel_miptree_release(&intelObj->mt);
57 _mesa_delete_texture_object(ctx, texObj);
58 }
59
60 static GLboolean
61 intel_alloc_texture_image_buffer(struct gl_context *ctx,
62 struct gl_texture_image *image)
63 {
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);
68
69 assert(image->Border == 0);
70
71 /* Quantize sample count */
72 if (image->NumSamples) {
73 image->NumSamples = intel_quantize_num_samples(brw->intelScreen, image->NumSamples);
74 if (!image->NumSamples)
75 return false;
76 }
77
78 /* Because the driver uses AllocTextureImageBuffer() internally, it may end
79 * up mismatched with FreeTextureImageBuffer(), but that is safe to call
80 * multiple times.
81 */
82 ctx->Driver.FreeTextureImageBuffer(ctx, image);
83
84 if (!_swrast_init_texture_image(image))
85 return false;
86
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);
93 } else {
94 intel_image->mt = intel_miptree_create_for_teximage(brw, intel_texobj,
95 intel_image,
96 0);
97
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.
102 */
103 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
104
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);
108 }
109
110 intel_texobj->needs_validate = true;
111
112 return true;
113 }
114
115 /**
116 * ctx->Driver.AllocTextureStorage() handler.
117 *
118 * Compare this to _mesa_AllocTextureStorage_sw, which would call into
119 * intel_alloc_texture_image_buffer() above.
120 */
121 static GLboolean
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)
126 {
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);
133 int face;
134 int level;
135
136 /* If the object's current miptree doesn't match what we need, make a new
137 * one.
138 */
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,
145 0, levels - 1,
146 width, height, depth,
147 num_samples,
148 MIPTREE_LAYOUT_TILING_ANY);
149
150 if (intel_texobj->mt == NULL) {
151 return false;
152 }
153 }
154
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);
159
160 image->NumSamples = num_samples;
161
162 _swrast_free_texture_image_buffer(ctx, image);
163 if (!_swrast_init_texture_image(image))
164 return false;
165
166 intel_miptree_reference(&intel_image->mt, intel_texobj->mt);
167 }
168 }
169
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;
175
176 return true;
177 }
178
179
180 static void
181 intel_free_texture_image_buffer(struct gl_context * ctx,
182 struct gl_texture_image *texImage)
183 {
184 struct intel_texture_image *intelImage = intel_texture_image(texImage);
185
186 DBG("%s\n", __func__);
187
188 intel_miptree_release(&intelImage->mt);
189
190 _swrast_free_texture_image_buffer(ctx, texImage);
191 }
192
193 /**
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
199 */
200 static void
201 intel_map_texture_image(struct gl_context *ctx,
202 struct gl_texture_image *tex_image,
203 GLuint slice,
204 GLuint x, GLuint y, GLuint w, GLuint h,
205 GLbitfield mode,
206 GLubyte **map,
207 GLint *out_stride)
208 {
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;
212 ptrdiff_t stride;
213
214 /* Our texture data is always stored in a miptree. */
215 assert(mt);
216
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 ||
219 h == 1);
220
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.
223 */
224 if (tex_image->TexObject->Target == GL_TEXTURE_CUBE_MAP)
225 slice = tex_image->Face;
226
227 intel_miptree_map(brw, mt,
228 tex_image->Level + tex_image->TexObject->MinLevel,
229 slice + tex_image->TexObject->MinLayer,
230 x, y, w, h, mode,
231 (void **)map, &stride);
232
233 *out_stride = stride;
234 }
235
236 static void
237 intel_unmap_texture_image(struct gl_context *ctx,
238 struct gl_texture_image *tex_image, GLuint slice)
239 {
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;
243
244 if (tex_image->TexObject->Target == GL_TEXTURE_CUBE_MAP)
245 slice = tex_image->Face;
246
247 intel_miptree_unmap(brw, mt,
248 tex_image->Level + tex_image->TexObject->MinLevel,
249 slice + tex_image->TexObject->MinLayer);
250 }
251
252 static GLboolean
253 intel_texture_view(struct gl_context *ctx,
254 struct gl_texture_object *texObj,
255 struct gl_texture_object *origTexObj)
256 {
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);
260
261 assert(intel_orig_tex->mt);
262 intel_miptree_reference(&intel_tex->mt, intel_orig_tex->mt);
263
264 /* Since we can only make views of immutable-format textures,
265 * we can assume that everything is in origTexObj's miptree.
266 *
267 * Mesa core has already made us a copy of all the teximage objects,
268 * except it hasn't copied our mt pointers, etc.
269 */
270 const int numFaces = _mesa_num_tex_faces(texObj->Target);
271 const int numLevels = texObj->NumLevels;
272
273 int face;
274 int level;
275
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);
280
281 intel_miptree_reference(&intel_image->mt, intel_orig_tex->mt);
282 }
283 }
284
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;
289
290 /* Set the validated texture format, with the same adjustments that
291 * would have been applied to determine the underlying texture's
292 * mt->format.
293 */
294 intel_tex->_Format = intel_depth_format_for_depthstencil_format(
295 intel_lower_compressed_format(brw, texObj->Image[0][0]->TexFormat));
296
297 return GL_TRUE;
298 }
299
300 static bool
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,
305 uint32_t row_stride,
306 bool read_only)
307 {
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);
313
314 if (!read_only) {
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.
318 */
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");
322 return false;
323 }
324
325 if (!brw->format_supported_as_render_target[image->TexFormat]) {
326 perf_debug("Non-renderable PBO format; fallback to CPU mapping\n");
327 return false;
328 }
329 }
330
331 assert(intel_texobj->mt == NULL);
332
333 drm_intel_bo *bo = intel_bufferobj_buffer(brw, intel_buffer_obj,
334 buffer_offset,
335 row_stride * image->Height);
336 intel_texobj->mt =
337 intel_miptree_create_for_bo(brw, bo,
338 image->TexFormat,
339 buffer_offset,
340 image->Width, image->Height, image->Depth,
341 row_stride,
342 0);
343 if (!intel_texobj->mt)
344 return false;
345
346 if (!_swrast_init_texture_image(image))
347 return false;
348
349 intel_miptree_reference(&intel_image->mt, intel_texobj->mt);
350
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;
356
357 return true;
358 }
359
360 static void
361 intel_texture_barrier(struct gl_context *ctx)
362 {
363 struct brw_context *brw = brw_context(ctx);
364
365 brw_emit_mi_flush(brw);
366 }
367
368 void
369 intelInitTextureFuncs(struct dd_function_table *functions)
370 {
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;
384 }