i965: fix DEPTH_TEXTURE_MODE (bug #14220)
[mesa.git] / src / mesa / drivers / dri / i965 / brw_wm_surface_state.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32
33 #include "mtypes.h"
34 #include "texformat.h"
35 #include "texstore.h"
36
37 #include "intel_mipmap_tree.h"
38 #include "intel_batchbuffer.h"
39 #include "intel_tex.h"
40
41
42 #include "brw_context.h"
43 #include "brw_state.h"
44 #include "brw_defines.h"
45
46
47 static GLuint translate_tex_target( GLenum target )
48 {
49 switch (target) {
50 case GL_TEXTURE_1D:
51 return BRW_SURFACE_1D;
52
53 case GL_TEXTURE_RECTANGLE_NV:
54 return BRW_SURFACE_2D;
55
56 case GL_TEXTURE_2D:
57 return BRW_SURFACE_2D;
58
59 case GL_TEXTURE_3D:
60 return BRW_SURFACE_3D;
61
62 case GL_TEXTURE_CUBE_MAP:
63 return BRW_SURFACE_CUBE;
64
65 default:
66 assert(0);
67 return 0;
68 }
69 }
70
71
72 static GLuint translate_tex_format( GLuint mesa_format, GLenum depth_mode )
73 {
74 switch( mesa_format ) {
75 case MESA_FORMAT_L8:
76 return BRW_SURFACEFORMAT_L8_UNORM;
77
78 case MESA_FORMAT_I8:
79 return BRW_SURFACEFORMAT_I8_UNORM;
80
81 case MESA_FORMAT_A8:
82 return BRW_SURFACEFORMAT_A8_UNORM;
83
84 case MESA_FORMAT_AL88:
85 return BRW_SURFACEFORMAT_L8A8_UNORM;
86
87 case MESA_FORMAT_RGB888:
88 assert(0); /* not supported for sampling */
89 return BRW_SURFACEFORMAT_R8G8B8_UNORM;
90
91 case MESA_FORMAT_ARGB8888:
92 return BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
93
94 case MESA_FORMAT_RGBA8888_REV:
95 return BRW_SURFACEFORMAT_R8G8B8A8_UNORM;
96
97 case MESA_FORMAT_RGB565:
98 return BRW_SURFACEFORMAT_B5G6R5_UNORM;
99
100 case MESA_FORMAT_ARGB1555:
101 return BRW_SURFACEFORMAT_B5G5R5A1_UNORM;
102
103 case MESA_FORMAT_ARGB4444:
104 return BRW_SURFACEFORMAT_B4G4R4A4_UNORM;
105
106 case MESA_FORMAT_YCBCR_REV:
107 return BRW_SURFACEFORMAT_YCRCB_NORMAL;
108
109 case MESA_FORMAT_YCBCR:
110 return BRW_SURFACEFORMAT_YCRCB_SWAPUVY;
111
112 case MESA_FORMAT_RGB_FXT1:
113 case MESA_FORMAT_RGBA_FXT1:
114 return BRW_SURFACEFORMAT_FXT1;
115
116 case MESA_FORMAT_Z16:
117 if (depth_mode == GL_INTENSITY)
118 return BRW_SURFACEFORMAT_I16_UNORM;
119 else if (depth_mode == GL_ALPHA)
120 return BRW_SURFACEFORMAT_A16_UNORM;
121 else
122 return BRW_SURFACEFORMAT_L16_UNORM;
123
124 case MESA_FORMAT_RGB_DXT1:
125 return BRW_SURFACEFORMAT_DXT1_RGB;
126
127 case MESA_FORMAT_RGBA_DXT1:
128 return BRW_SURFACEFORMAT_BC1_UNORM;
129
130 case MESA_FORMAT_RGBA_DXT3:
131 return BRW_SURFACEFORMAT_BC2_UNORM;
132
133 case MESA_FORMAT_RGBA_DXT5:
134 return BRW_SURFACEFORMAT_BC3_UNORM;
135
136 case MESA_FORMAT_SRGBA8:
137 return BRW_SURFACEFORMAT_R8G8B8A8_UNORM_SRGB;
138 case MESA_FORMAT_SRGB_DXT1:
139 return BRW_SURFACEFORMAT_BC1_UNORM_SRGB;
140
141 case MESA_FORMAT_Z24_S8:
142 return BRW_SURFACEFORMAT_I24X8_UNORM;
143
144 default:
145 assert(0);
146 return 0;
147 }
148 }
149
150 struct brw_wm_surface_key {
151 GLenum target, depthmode;
152 dri_bo *bo;
153 GLint format;
154 GLint first_level, last_level;
155 GLint width, height, depth;
156 GLint pitch, cpp;
157 GLboolean tiled;
158 };
159
160 static dri_bo *
161 brw_create_texture_surface( struct brw_context *brw,
162 struct brw_wm_surface_key *key )
163 {
164 struct brw_surface_state surf;
165 dri_bo *bo;
166
167 memset(&surf, 0, sizeof(surf));
168
169 surf.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
170 surf.ss0.surface_type = translate_tex_target(key->target);
171 surf.ss0.surface_format = translate_tex_format(key->format, key->depthmode);
172
173 /* This is ok for all textures with channel width 8bit or less:
174 */
175 /* surf.ss0.data_return_format = BRW_SURFACERETURNFORMAT_S1; */
176
177 surf.ss1.base_addr = key->bo->offset; /* reloc */
178
179 surf.ss2.mip_count = key->last_level - key->first_level;
180 surf.ss2.width = key->width - 1;
181 surf.ss2.height = key->height - 1;
182
183 surf.ss3.tile_walk = BRW_TILEWALK_XMAJOR;
184 surf.ss3.tiled_surface = key->tiled;
185 surf.ss3.pitch = (key->pitch * key->cpp) - 1;
186 surf.ss3.depth = key->depth - 1;
187
188 surf.ss4.min_lod = 0;
189
190 if (key->target == GL_TEXTURE_CUBE_MAP) {
191 surf.ss0.cube_pos_x = 1;
192 surf.ss0.cube_pos_y = 1;
193 surf.ss0.cube_pos_z = 1;
194 surf.ss0.cube_neg_x = 1;
195 surf.ss0.cube_neg_y = 1;
196 surf.ss0.cube_neg_z = 1;
197 }
198
199 bo = brw_upload_cache(&brw->cache, BRW_SS_SURFACE,
200 key, sizeof(*key),
201 &key->bo, 1,
202 &surf, sizeof(surf),
203 NULL, NULL);
204
205 /* Emit relocation to surface contents */
206 dri_emit_reloc(bo,
207 DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_READ,
208 0,
209 offsetof(struct brw_surface_state, ss1),
210 key->bo);
211
212 return bo;
213 }
214
215 static int
216 brw_update_texture_surface( GLcontext *ctx, GLuint unit )
217 {
218 struct brw_context *brw = brw_context(ctx);
219 struct gl_texture_object *tObj = brw->attribs.Texture->Unit[unit]._Current;
220 struct intel_texture_object *intelObj = intel_texture_object(tObj);
221 struct gl_texture_image *firstImage = tObj->Image[0][intelObj->firstLevel];
222 struct brw_wm_surface_key key;
223 int ret = 0;
224
225 memset(&key, 0, sizeof(key));
226 key.target = tObj->Target;
227 key.depthmode = tObj->DepthMode;
228 key.format = firstImage->TexFormat->MesaFormat;
229 key.bo = intelObj->mt->region->buffer;
230 key.first_level = intelObj->firstLevel;
231 key.last_level = intelObj->lastLevel;
232 key.width = firstImage->Width;
233 key.height = firstImage->Height;
234 key.pitch = intelObj->mt->pitch;
235 key.cpp = intelObj->mt->cpp;
236 key.depth = firstImage->Depth;
237 key.tiled = intelObj->mt->region->tiled;
238
239 ret |= dri_bufmgr_check_aperture_space(key.bo);
240
241 dri_bo_unreference(brw->wm.surf_bo[unit + MAX_DRAW_BUFFERS]);
242 brw->wm.surf_bo[unit + MAX_DRAW_BUFFERS] = brw_search_cache(&brw->cache, BRW_SS_SURFACE,
243 &key, sizeof(key),
244 &key.bo, 1,
245 NULL);
246 if (brw->wm.surf_bo[unit + MAX_DRAW_BUFFERS] == NULL) {
247 brw->wm.surf_bo[unit + MAX_DRAW_BUFFERS] = brw_create_texture_surface(brw, &key);
248 }
249
250 ret |= dri_bufmgr_check_aperture_space(brw->wm.surf_bo[unit + MAX_DRAW_BUFFERS]);
251 return ret;
252 }
253
254 /**
255 * Sets up a surface state structure to point at the given region.
256 * While it is only used for the front/back buffer currently, it should be
257 * usable for further buffers when doing ARB_draw_buffer support.
258 */
259 static int
260 brw_update_region_surface(struct brw_context *brw, struct intel_region *region,
261 unsigned int unit, GLboolean cached)
262 {
263 dri_bo *region_bo = NULL;
264 int ret = 0;
265 struct {
266 unsigned int surface_type;
267 unsigned int surface_format;
268 unsigned int width, height, cpp;
269 GLubyte color_mask[4];
270 GLboolean tiled, color_blend;
271 } key;
272
273 memset(&key, 0, sizeof(key));
274
275 if (region != NULL) {
276 region_bo = region->buffer;
277
278 key.surface_type = BRW_SURFACE_2D;
279 if (region->cpp == 4)
280 key.surface_format = BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
281 else
282 key.surface_format = BRW_SURFACEFORMAT_B5G6R5_UNORM;
283 key.tiled = region->tiled;
284 key.width = region->pitch; /* XXX: not really! */
285 key.height = region->height;
286 key.cpp = region->cpp;
287
288 ret |= dri_bufmgr_check_aperture_space(region->buffer);
289 } else {
290 key.surface_type = BRW_SURFACE_NULL;
291 key.surface_format = BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
292 key.tiled = 0;
293 key.width = 1;
294 key.height = 1;
295 key.cpp = 4;
296 }
297 memcpy(key.color_mask, brw->attribs.Color->ColorMask,
298 sizeof(key.color_mask));
299 key.color_blend = (!brw->attribs.Color->_LogicOpEnabled &&
300 brw->attribs.Color->BlendEnabled);
301
302 dri_bo_unreference(brw->wm.surf_bo[unit]);
303 brw->wm.surf_bo[unit] = NULL;
304 if (cached)
305 brw->wm.surf_bo[unit] = brw_search_cache(&brw->cache, BRW_SS_SURFACE,
306 &key, sizeof(key),
307 &region_bo, 1,
308 NULL);
309
310 if (brw->wm.surf_bo[unit] == NULL) {
311 struct brw_surface_state surf;
312
313 memset(&surf, 0, sizeof(surf));
314
315 surf.ss0.surface_format = key.surface_format;
316 surf.ss0.surface_type = key.surface_type;
317 if (region_bo != NULL)
318 surf.ss1.base_addr = region_bo->offset; /* reloc */
319
320 surf.ss2.width = key.width - 1;
321 surf.ss2.height = key.height - 1;
322 surf.ss3.tile_walk = BRW_TILEWALK_XMAJOR;
323 surf.ss3.tiled_surface = key.tiled;
324 surf.ss3.pitch = (key.width * key.cpp) - 1;
325
326 /* _NEW_COLOR */
327 surf.ss0.color_blend = key.color_blend;
328 surf.ss0.writedisable_red = !key.color_mask[0];
329 surf.ss0.writedisable_green = !key.color_mask[1];
330 surf.ss0.writedisable_blue = !key.color_mask[2];
331 surf.ss0.writedisable_alpha = !key.color_mask[3];
332
333 /* Key size will never match key size for textures, so we're safe. */
334 brw->wm.surf_bo[unit] = brw_upload_cache(&brw->cache, BRW_SS_SURFACE,
335 &key, sizeof(key),
336 &region_bo, 1,
337 &surf, sizeof(surf),
338 NULL, NULL);
339 if (region_bo != NULL) {
340 dri_emit_reloc(brw->wm.surf_bo[unit],
341 DRM_BO_FLAG_MEM_TT |
342 DRM_BO_FLAG_READ |
343 DRM_BO_FLAG_WRITE,
344 0,
345 offsetof(struct brw_surface_state, ss1),
346 region_bo);
347 }
348 }
349
350 ret |= dri_bufmgr_check_aperture_space(brw->wm.surf_bo[unit]);
351
352 return ret;
353 }
354
355
356 /**
357 * Constructs the binding table for the WM surface state, which maps unit
358 * numbers to surface state objects.
359 */
360 static dri_bo *
361 brw_wm_get_binding_table(struct brw_context *brw)
362 {
363 dri_bo *bind_bo;
364
365 bind_bo = brw_search_cache(&brw->cache, BRW_SS_SURF_BIND,
366 NULL, 0,
367 brw->wm.surf_bo, brw->wm.nr_surfaces,
368 NULL);
369
370 if (bind_bo == NULL) {
371 GLuint data_size = brw->wm.nr_surfaces * sizeof(GLuint);
372 uint32_t *data = malloc(data_size);
373 int i;
374
375 for (i = 0; i < brw->wm.nr_surfaces; i++)
376 if (brw->wm.surf_bo[i])
377 data[i] = brw->wm.surf_bo[i]->offset;
378 else
379 data[i] = 0;
380
381 bind_bo = brw_upload_cache( &brw->cache, BRW_SS_SURF_BIND,
382 NULL, 0,
383 brw->wm.surf_bo, brw->wm.nr_surfaces,
384 data, data_size,
385 NULL, NULL);
386
387 /* Emit binding table relocations to surface state */
388 for (i = 0; i < BRW_WM_MAX_SURF; i++) {
389 if (brw->wm.surf_bo[i] != NULL) {
390 dri_emit_reloc(bind_bo,
391 DRM_BO_FLAG_MEM_TT |
392 DRM_BO_FLAG_READ |
393 DRM_BO_FLAG_WRITE,
394 0,
395 i * sizeof(GLuint),
396 brw->wm.surf_bo[i]);
397 }
398 }
399
400 free(data);
401 }
402
403 return bind_bo;
404 }
405
406 static int prepare_wm_surfaces(struct brw_context *brw )
407 {
408 GLcontext *ctx = &brw->intel.ctx;
409 struct intel_context *intel = &brw->intel;
410 GLuint i, ret;
411
412 if (brw->state.nr_draw_regions > 1) {
413 for (i = 0; i < brw->state.nr_draw_regions; i++) {
414 ret = brw_update_region_surface(brw, brw->state.draw_regions[i], i,
415 GL_FALSE);
416 if (ret)
417 return ret;
418 }
419 }else {
420 ret = brw_update_region_surface(brw, brw->state.draw_regions[0], 0, GL_TRUE);
421 if (ret)
422 return ret;
423 }
424
425 brw->wm.nr_surfaces = MAX_DRAW_BUFFERS;
426
427 for (i = 0; i < BRW_MAX_TEX_UNIT; i++) {
428 struct gl_texture_unit *texUnit = &brw->attribs.Texture->Unit[i];
429
430 /* _NEW_TEXTURE, BRW_NEW_TEXDATA */
431 if(texUnit->_ReallyEnabled) {
432 if (texUnit->_Current == intel->frame_buffer_texobj) {
433 dri_bo_unreference(brw->wm.surf_bo[i+MAX_DRAW_BUFFERS]);
434 brw->wm.surf_bo[i+MAX_DRAW_BUFFERS] = brw->wm.surf_bo[0];
435 dri_bo_reference(brw->wm.surf_bo[i+MAX_DRAW_BUFFERS]);
436 brw->wm.nr_surfaces = i + MAX_DRAW_BUFFERS + 1;
437 } else {
438 ret = brw_update_texture_surface(ctx, i);
439 brw->wm.nr_surfaces = i + MAX_DRAW_BUFFERS + 1;
440
441 if (ret)
442 return ret;
443 }
444 } else {
445 dri_bo_unreference(brw->wm.surf_bo[i+MAX_DRAW_BUFFERS]);
446 brw->wm.surf_bo[i+MAX_DRAW_BUFFERS] = NULL;
447 }
448
449 }
450
451 dri_bo_unreference(brw->wm.bind_bo);
452 brw->wm.bind_bo = brw_wm_get_binding_table(brw);
453
454 return dri_bufmgr_check_aperture_space(brw->wm.bind_bo);
455 }
456
457
458 const struct brw_tracked_state brw_wm_surfaces = {
459 .dirty = {
460 .mesa = _NEW_COLOR | _NEW_TEXTURE | _NEW_BUFFERS,
461 .brw = BRW_NEW_CONTEXT,
462 .cache = 0
463 },
464 .prepare = prepare_wm_surfaces,
465 };
466
467
468