Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / gallium / drivers / i965simple / 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 #include "brw_context.h"
33 #include "brw_state.h"
34 #include "brw_defines.h"
35
36 static unsigned translate_tex_target( enum pipe_texture_target target )
37 {
38 switch (target) {
39 case PIPE_TEXTURE_1D:
40 return BRW_SURFACE_1D;
41
42 case PIPE_TEXTURE_2D:
43 return BRW_SURFACE_2D;
44
45 case PIPE_TEXTURE_3D:
46 return BRW_SURFACE_3D;
47
48 case PIPE_TEXTURE_CUBE:
49 return BRW_SURFACE_CUBE;
50
51 default:
52 assert(0);
53 return 0;
54 }
55 }
56
57 static unsigned translate_tex_format( enum pipe_format pipe_format )
58 {
59 switch( pipe_format ) {
60 case PIPE_FORMAT_L8_UNORM:
61 return BRW_SURFACEFORMAT_L8_UNORM;
62
63 case PIPE_FORMAT_I8_UNORM:
64 return BRW_SURFACEFORMAT_I8_UNORM;
65
66 case PIPE_FORMAT_A8_UNORM:
67 return BRW_SURFACEFORMAT_A8_UNORM;
68
69 case PIPE_FORMAT_A8L8_UNORM:
70 return BRW_SURFACEFORMAT_L8A8_UNORM;
71
72 case PIPE_FORMAT_R8G8B8_UNORM:
73 assert(0); /* not supported for sampling */
74 return BRW_SURFACEFORMAT_R8G8B8_UNORM;
75
76 case PIPE_FORMAT_B8G8R8A8_UNORM:
77 return BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
78
79 case PIPE_FORMAT_R8G8B8A8_UNORM:
80 return BRW_SURFACEFORMAT_R8G8B8A8_UNORM;
81
82 case PIPE_FORMAT_R5G6B5_UNORM:
83 return BRW_SURFACEFORMAT_B5G6R5_UNORM;
84
85 case PIPE_FORMAT_A1R5G5B5_UNORM:
86 return BRW_SURFACEFORMAT_B5G5R5A1_UNORM;
87
88 case PIPE_FORMAT_A4R4G4B4_UNORM:
89 return BRW_SURFACEFORMAT_B4G4R4A4_UNORM;
90
91 case PIPE_FORMAT_YCBCR_REV:
92 return BRW_SURFACEFORMAT_YCRCB_NORMAL;
93
94 case PIPE_FORMAT_YCBCR:
95 return BRW_SURFACEFORMAT_YCRCB_SWAPUVY;
96 #if 0
97 case PIPE_FORMAT_RGB_FXT1:
98 case PIPE_FORMAT_RGBA_FXT1:
99 return BRW_SURFACEFORMAT_FXT1;
100 #endif
101
102 case PIPE_FORMAT_Z16_UNORM:
103 return BRW_SURFACEFORMAT_I16_UNORM;
104 #if 0
105 case PIPE_FORMAT_RGB_DXT1:
106 return BRW_SURFACEFORMAT_DXT1_RGB;
107
108 case PIPE_FORMAT_RGBA_DXT1:
109 return BRW_SURFACEFORMAT_BC1_UNORM;
110
111 case PIPE_FORMAT_RGBA_DXT3:
112 return BRW_SURFACEFORMAT_BC2_UNORM;
113
114 case PIPE_FORMAT_RGBA_DXT5:
115 return BRW_SURFACEFORMAT_BC3_UNORM;
116
117 case PIPE_FORMAT_SRGBA8:
118 return BRW_SURFACEFORMAT_R8G8B8A8_UNORM_SRGB;
119 case PIPE_FORMAT_SRGB_DXT1:
120 return BRW_SURFACEFORMAT_BC1_UNORM_SRGB;
121 #endif
122
123 default:
124 assert(0);
125 return 0;
126 }
127 }
128
129 static unsigned brw_buffer_offset(struct brw_context *brw,
130 struct pipe_buffer *buffer)
131 {
132 return brw->winsys->get_buffer_offset(brw->winsys,
133 buffer,
134 0);
135 }
136
137 static
138 void brw_update_texture_surface( struct brw_context *brw,
139 unsigned unit )
140 {
141 const struct brw_texture *tObj = brw->attribs.Texture[unit];
142 struct brw_surface_state surf;
143
144 memset(&surf, 0, sizeof(surf));
145
146 surf.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
147 surf.ss0.surface_type = translate_tex_target(tObj->base.target);
148 surf.ss0.surface_format = translate_tex_format(tObj->base.format);
149
150 /* This is ok for all textures with channel width 8bit or less:
151 */
152 /* surf.ss0.data_return_format = BRW_SURFACERETURNFORMAT_S1; */
153
154 /* Updated in emit_reloc */
155 surf.ss1.base_addr = brw_buffer_offset( brw, tObj->buffer );
156
157 surf.ss2.mip_count = tObj->base.last_level;
158 surf.ss2.width = tObj->base.width[0] - 1;
159 surf.ss2.height = tObj->base.height[0] - 1;
160
161 surf.ss3.tile_walk = BRW_TILEWALK_XMAJOR;
162 surf.ss3.tiled_surface = 0; /* always zero */
163 surf.ss3.pitch = tObj->stride - 1;
164 surf.ss3.depth = tObj->base.depth[0] - 1;
165
166 surf.ss4.min_lod = 0;
167
168 if (tObj->base.target == PIPE_TEXTURE_CUBE) {
169 surf.ss0.cube_pos_x = 1;
170 surf.ss0.cube_pos_y = 1;
171 surf.ss0.cube_pos_z = 1;
172 surf.ss0.cube_neg_x = 1;
173 surf.ss0.cube_neg_y = 1;
174 surf.ss0.cube_neg_z = 1;
175 }
176
177 brw->wm.bind.surf_ss_offset[unit + 1] =
178 brw_cache_data( &brw->cache[BRW_SS_SURFACE], &surf );
179 }
180
181
182
183 #define OFFSET(TYPE, FIELD) ( (unsigned)&(((TYPE *)0)->FIELD) )
184
185
186 static void upload_wm_surfaces(struct brw_context *brw )
187 {
188 unsigned i;
189
190 {
191 struct brw_surface_state surf;
192
193 /* BRW_NEW_FRAMEBUFFER
194 */
195 struct pipe_surface *pipe_surface = brw->attribs.FrameBuffer.cbufs[0];/*fixme*/
196
197 memset(&surf, 0, sizeof(surf));
198
199 if (pipe_surface != NULL) {
200 if (pipe_surface->block.size == 4)
201 surf.ss0.surface_format = BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
202 else
203 surf.ss0.surface_format = BRW_SURFACEFORMAT_B5G6R5_UNORM;
204
205 surf.ss0.surface_type = BRW_SURFACE_2D;
206
207 surf.ss1.base_addr = brw_buffer_offset( brw, pipe_surface->buffer );
208
209 surf.ss2.width = pipe_surface->width - 1;
210 surf.ss2.height = pipe_surface->height - 1;
211 surf.ss3.tile_walk = BRW_TILEWALK_XMAJOR;
212 surf.ss3.tiled_surface = 0;
213 surf.ss3.pitch = pipe_surface->stride - 1;
214 } else {
215 surf.ss0.surface_format = BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
216 surf.ss0.surface_type = BRW_SURFACE_NULL;
217 }
218
219 /* BRW_NEW_BLEND */
220 surf.ss0.color_blend = (!brw->attribs.Blend->logicop_enable &&
221 brw->attribs.Blend->blend_enable);
222
223
224 surf.ss0.writedisable_red = !(brw->attribs.Blend->colormask & PIPE_MASK_R);
225 surf.ss0.writedisable_green = !(brw->attribs.Blend->colormask & PIPE_MASK_G);
226 surf.ss0.writedisable_blue = !(brw->attribs.Blend->colormask & PIPE_MASK_B);
227 surf.ss0.writedisable_alpha = !(brw->attribs.Blend->colormask & PIPE_MASK_A);
228
229
230
231
232 brw->wm.bind.surf_ss_offset[0] = brw_cache_data( &brw->cache[BRW_SS_SURFACE], &surf );
233
234 brw->wm.nr_surfaces = 1;
235 }
236
237
238 /* BRW_NEW_TEXTURE
239 */
240 for (i = 0; i < brw->num_textures && i < brw->num_samplers; i++) {
241 const struct brw_texture *texUnit = brw->attribs.Texture[i];
242
243 if (texUnit &&
244 texUnit->base.refcount/*(texUnit->refcount > 0) == really used */) {
245
246 brw_update_texture_surface(brw, i);
247
248 brw->wm.nr_surfaces = i+2;
249 }
250 else {
251 brw->wm.bind.surf_ss_offset[i+1] = 0;
252 }
253 }
254
255 brw->wm.bind_ss_offset = brw_cache_data( &brw->cache[BRW_SS_SURF_BIND],
256 &brw->wm.bind );
257 }
258
259
260 /* KW: Will find a different way to acheive this, see for example the
261 * state caches with relocs in the i915 swz driver.
262 */
263 #if 0
264 static void emit_reloc_wm_surfaces(struct brw_context *brw)
265 {
266 int unit;
267
268 if (brw->state.draw_region != NULL) {
269 /* Emit framebuffer relocation */
270 dri_emit_reloc(brw_cache_buffer(brw, BRW_SS_SURFACE),
271 DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE,
272 0,
273 brw->wm.bind.surf_ss_offset[0] +
274 offsetof(struct brw_surface_state, ss1),
275 brw->state.draw_region->buffer);
276 }
277
278 /* Emit relocations for texture buffers */
279 for (unit = 0; unit < BRW_MAX_TEX_UNIT; unit++) {
280 struct gl_texture_unit *texUnit = &brw->attribs.Texture->Unit[unit];
281 struct gl_texture_object *tObj = texUnit->_Current;
282 struct intel_texture_object *intelObj = intel_texture_object(tObj);
283
284 if (texUnit->_ReallyEnabled && intelObj->mt != NULL) {
285 dri_emit_reloc(brw_cache_buffer(brw, BRW_SS_SURFACE),
286 DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_READ,
287 0,
288 brw->wm.bind.surf_ss_offset[unit + 1] +
289 offsetof(struct brw_surface_state, ss1),
290 intelObj->mt->region->buffer);
291 }
292 }
293 }
294 #endif
295
296 const struct brw_tracked_state brw_wm_surfaces = {
297 .dirty = {
298 .brw = (BRW_NEW_FRAMEBUFFER |
299 BRW_NEW_BLEND |
300 BRW_NEW_TEXTURE),
301 .cache = 0
302 },
303 .update = upload_wm_surfaces,
304 };