2 * Copyright © 2011 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 #include "main/mtypes.h"
24 #include "main/samplerobj.h"
25 #include "program/prog_parameter.h"
27 #include "intel_mipmap_tree.h"
28 #include "intel_batchbuffer.h"
29 #include "intel_tex.h"
30 #include "intel_fbo.h"
31 #include "intel_buffer_objects.h"
33 #include "brw_context.h"
34 #include "brw_state.h"
35 #include "brw_defines.h"
39 gen7_set_surface_tiling(struct gen7_surface_state
*surf
, uint32_t tiling
)
42 case I915_TILING_NONE
:
43 surf
->ss0
.tiled_surface
= 0;
44 surf
->ss0
.tile_walk
= 0;
47 surf
->ss0
.tiled_surface
= 1;
48 surf
->ss0
.tile_walk
= BRW_TILEWALK_XMAJOR
;
51 surf
->ss0
.tiled_surface
= 1;
52 surf
->ss0
.tile_walk
= BRW_TILEWALK_YMAJOR
;
58 gen7_update_buffer_texture_surface(struct gl_context
*ctx
, GLuint unit
)
60 struct brw_context
*brw
= brw_context(ctx
);
61 struct gl_texture_object
*tObj
= ctx
->Texture
.Unit
[unit
]._Current
;
62 const GLuint surf_index
= SURF_INDEX_TEXTURE(unit
);
63 struct gen7_surface_state
*surf
;
64 struct intel_buffer_object
*intel_obj
=
65 intel_buffer_object(tObj
->BufferObject
);
66 drm_intel_bo
*bo
= intel_obj
? intel_obj
->buffer
: NULL
;
67 gl_format format
= tObj
->_BufferObjectFormat
;
68 int texel_size
= _mesa_get_format_bytes(format
);
70 surf
= brw_state_batch(brw
, AUB_TRACE_SURFACE_STATE
,
71 sizeof(*surf
), 32, &brw
->wm
.surf_offset
[surf_index
]);
72 memset(surf
, 0, sizeof(*surf
));
74 surf
->ss0
.surface_type
= BRW_SURFACE_BUFFER
;
75 surf
->ss0
.surface_format
= brw_format_for_mesa_format(format
);
77 surf
->ss0
.render_cache_read_write
= 1;
79 if (surf
->ss0
.surface_format
== 0 && format
!= MESA_FORMAT_RGBA_FLOAT32
) {
80 _mesa_problem(NULL
, "bad format %s for texture buffer\n",
81 _mesa_get_format_name(format
));
85 surf
->ss1
.base_addr
= bo
->offset
; /* reloc */
87 /* Emit relocation to surface contents. Section 5.1.1 of the gen4
88 * bspec ("Data Cache") says that the data cache does not exist as
89 * a separate cache and is just the sampler cache.
91 drm_intel_bo_emit_reloc(brw
->intel
.batch
.bo
,
92 (brw
->wm
.surf_offset
[surf_index
] +
93 offsetof(struct gen7_surface_state
, ss1
)),
95 I915_GEM_DOMAIN_SAMPLER
, 0);
97 int w
= intel_obj
->Base
.Size
/ texel_size
;
98 surf
->ss2
.width
= w
& 0x7f; /* bits 6:0 of size or width */
99 surf
->ss2
.height
= (w
>> 7) & 0x1fff; /* bits 19:7 of size or width */
100 surf
->ss3
.depth
= (w
>> 20) & 0x7f; /* bits 26:20 of size or width */
101 surf
->ss3
.pitch
= texel_size
- 1;
103 surf
->ss1
.base_addr
= 0;
105 surf
->ss2
.height
= 0;
110 gen7_set_surface_tiling(surf
, I915_TILING_NONE
);
114 gen7_update_texture_surface(struct gl_context
*ctx
, GLuint unit
)
116 struct brw_context
*brw
= brw_context(ctx
);
117 struct gl_texture_object
*tObj
= ctx
->Texture
.Unit
[unit
]._Current
;
118 struct intel_texture_object
*intelObj
= intel_texture_object(tObj
);
119 struct intel_mipmap_tree
*mt
= intelObj
->mt
;
120 struct gl_texture_image
*firstImage
= tObj
->Image
[0][tObj
->BaseLevel
];
121 struct gl_sampler_object
*sampler
= _mesa_get_samplerobj(ctx
, unit
);
122 const GLuint surf_index
= SURF_INDEX_TEXTURE(unit
);
123 struct gen7_surface_state
*surf
;
124 int width
, height
, depth
;
126 if (tObj
->Target
== GL_TEXTURE_BUFFER
) {
127 gen7_update_buffer_texture_surface(ctx
, unit
);
131 intel_miptree_get_dimensions_for_image(firstImage
, &width
, &height
, &depth
);
133 surf
= brw_state_batch(brw
, AUB_TRACE_SURFACE_STATE
,
134 sizeof(*surf
), 32, &brw
->wm
.surf_offset
[surf_index
]);
135 memset(surf
, 0, sizeof(*surf
));
137 if (mt
->align_h
== 4)
138 surf
->ss0
.vertical_alignment
= 1;
139 if (mt
->align_w
== 8)
140 surf
->ss0
.horizontal_alignment
= 1;
142 surf
->ss0
.surface_type
= translate_tex_target(tObj
->Target
);
143 surf
->ss0
.surface_format
= translate_tex_format(mt
->format
,
144 firstImage
->InternalFormat
,
146 sampler
->sRGBDecode
);
147 if (tObj
->Target
== GL_TEXTURE_CUBE_MAP
) {
148 surf
->ss0
.cube_pos_x
= 1;
149 surf
->ss0
.cube_pos_y
= 1;
150 surf
->ss0
.cube_pos_z
= 1;
151 surf
->ss0
.cube_neg_x
= 1;
152 surf
->ss0
.cube_neg_y
= 1;
153 surf
->ss0
.cube_neg_z
= 1;
156 surf
->ss0
.is_array
= depth
> 1 && tObj
->Target
!= GL_TEXTURE_3D
;
158 gen7_set_surface_tiling(surf
, intelObj
->mt
->region
->tiling
);
160 /* ss0 remaining fields:
161 * - vert_line_stride (exists on gen6 but we ignore it)
162 * - vert_line_stride_ofs (exists on gen6 but we ignore it)
163 * - surface_array_spacing
164 * - render_cache_read_write (exists on gen6 but ignored here)
167 surf
->ss1
.base_addr
= intelObj
->mt
->region
->bo
->offset
; /* reloc */
169 surf
->ss2
.width
= width
- 1;
170 surf
->ss2
.height
= height
- 1;
172 surf
->ss3
.pitch
= (intelObj
->mt
->region
->pitch
* intelObj
->mt
->cpp
) - 1;
173 surf
->ss3
.depth
= depth
- 1;
177 surf
->ss5
.mip_count
= intelObj
->_MaxLevel
- tObj
->BaseLevel
;
178 surf
->ss5
.min_lod
= 0;
180 /* ss5 remaining fields:
181 * - x_offset (N/A for textures?)
186 if (brw
->intel
.is_haswell
) {
187 surf
->ss7
.shader_chanel_select_r
= HSW_SCS_RED
;
188 surf
->ss7
.shader_chanel_select_g
= HSW_SCS_GREEN
;
189 surf
->ss7
.shader_chanel_select_b
= HSW_SCS_BLUE
;
190 surf
->ss7
.shader_chanel_select_a
= HSW_SCS_ALPHA
;
193 /* Emit relocation to surface contents */
194 drm_intel_bo_emit_reloc(brw
->intel
.batch
.bo
,
195 brw
->wm
.surf_offset
[surf_index
] +
196 offsetof(struct gen7_surface_state
, ss1
),
197 intelObj
->mt
->region
->bo
, 0,
198 I915_GEM_DOMAIN_SAMPLER
, 0);
202 * Create the constant buffer surface. Vertex/fragment shader constants will
203 * be read from this buffer with Data Port Read instructions/messages.
206 gen7_create_constant_surface(struct brw_context
*brw
,
209 uint32_t *out_offset
)
211 const GLint w
= width
- 1;
212 struct gen7_surface_state
*surf
;
214 surf
= brw_state_batch(brw
, AUB_TRACE_SURFACE_STATE
,
215 sizeof(*surf
), 32, out_offset
);
216 memset(surf
, 0, sizeof(*surf
));
218 surf
->ss0
.surface_type
= BRW_SURFACE_BUFFER
;
219 surf
->ss0
.surface_format
= BRW_SURFACEFORMAT_R32G32B32A32_FLOAT
;
221 surf
->ss0
.render_cache_read_write
= 1;
224 surf
->ss1
.base_addr
= bo
->offset
; /* reloc */
226 surf
->ss2
.width
= w
& 0x7f; /* bits 6:0 of size or width */
227 surf
->ss2
.height
= (w
>> 7) & 0x1fff; /* bits 19:7 of size or width */
228 surf
->ss3
.depth
= (w
>> 20) & 0x7f; /* bits 26:20 of size or width */
229 surf
->ss3
.pitch
= (16 - 1); /* stride between samples */
230 gen7_set_surface_tiling(surf
, I915_TILING_NONE
); /* tiling now allowed */
232 if (brw
->intel
.is_haswell
) {
233 surf
->ss7
.shader_chanel_select_r
= HSW_SCS_RED
;
234 surf
->ss7
.shader_chanel_select_g
= HSW_SCS_GREEN
;
235 surf
->ss7
.shader_chanel_select_b
= HSW_SCS_BLUE
;
236 surf
->ss7
.shader_chanel_select_a
= HSW_SCS_ALPHA
;
239 /* Emit relocation to surface contents. Section 5.1.1 of the gen4
240 * bspec ("Data Cache") says that the data cache does not exist as
241 * a separate cache and is just the sampler cache.
243 drm_intel_bo_emit_reloc(brw
->intel
.batch
.bo
,
245 offsetof(struct gen7_surface_state
, ss1
)),
247 I915_GEM_DOMAIN_SAMPLER
, 0);
251 gen7_update_null_renderbuffer_surface(struct brw_context
*brw
, unsigned unit
)
253 struct gen7_surface_state
*surf
;
255 surf
= brw_state_batch(brw
, AUB_TRACE_SURFACE_STATE
,
256 sizeof(*surf
), 32, &brw
->wm
.surf_offset
[unit
]);
257 memset(surf
, 0, sizeof(*surf
));
259 surf
->ss0
.surface_type
= BRW_SURFACE_NULL
;
260 surf
->ss0
.surface_format
= BRW_SURFACEFORMAT_B8G8R8A8_UNORM
;
264 * Sets up a surface state structure to point at the given region.
265 * While it is only used for the front/back buffer currently, it should be
266 * usable for further buffers when doing ARB_draw_buffer support.
269 gen7_update_renderbuffer_surface(struct brw_context
*brw
,
270 struct gl_renderbuffer
*rb
,
273 struct intel_context
*intel
= &brw
->intel
;
274 struct gl_context
*ctx
= &intel
->ctx
;
275 struct intel_renderbuffer
*irb
= intel_renderbuffer(rb
);
276 struct intel_region
*region
= irb
->mt
->region
;
277 struct gen7_surface_state
*surf
;
278 uint32_t tile_x
, tile_y
;
279 gl_format rb_format
= intel_rb_format(irb
);
281 surf
= brw_state_batch(brw
, AUB_TRACE_SURFACE_STATE
,
282 sizeof(*surf
), 32, &brw
->wm
.surf_offset
[unit
]);
283 memset(surf
, 0, sizeof(*surf
));
285 if (irb
->mt
->align_h
== 4)
286 surf
->ss0
.vertical_alignment
= 1;
287 if (irb
->mt
->align_w
== 8)
288 surf
->ss0
.horizontal_alignment
= 1;
291 case MESA_FORMAT_SARGB8
:
294 * Without GL_EXT_framebuffer_sRGB we shouldn't bind sRGB surfaces to the
295 * blend/update as sRGB.
297 if (ctx
->Color
.sRGBEnabled
)
298 surf
->ss0
.surface_format
= brw_format_for_mesa_format(rb_format
);
300 surf
->ss0
.surface_format
= BRW_SURFACEFORMAT_B8G8R8A8_UNORM
;
303 assert(brw_render_target_supported(intel
, rb
));
304 surf
->ss0
.surface_format
= brw
->render_target_format
[rb_format
];
305 if (unlikely(!brw
->format_supported_as_render_target
[rb_format
])) {
306 _mesa_problem(ctx
, "%s: renderbuffer format %s unsupported\n",
307 __FUNCTION__
, _mesa_get_format_name(rb_format
));
312 surf
->ss0
.surface_type
= BRW_SURFACE_2D
;
314 surf
->ss1
.base_addr
= intel_renderbuffer_tile_offsets(irb
, &tile_x
, &tile_y
);
315 surf
->ss1
.base_addr
+= region
->bo
->offset
; /* reloc */
317 assert(brw
->has_surface_tile_offset
);
318 /* Note that the low bits of these fields are missing, so
319 * there's the possibility of getting in trouble.
321 assert(tile_x
% 4 == 0);
322 assert(tile_y
% 2 == 0);
323 surf
->ss5
.x_offset
= tile_x
/ 4;
324 surf
->ss5
.y_offset
= tile_y
/ 2;
326 surf
->ss2
.width
= rb
->Width
- 1;
327 surf
->ss2
.height
= rb
->Height
- 1;
328 gen7_set_surface_tiling(surf
, region
->tiling
);
329 surf
->ss3
.pitch
= (region
->pitch
* region
->cpp
) - 1;
331 if (intel
->is_haswell
) {
332 surf
->ss7
.shader_chanel_select_r
= HSW_SCS_RED
;
333 surf
->ss7
.shader_chanel_select_g
= HSW_SCS_GREEN
;
334 surf
->ss7
.shader_chanel_select_b
= HSW_SCS_BLUE
;
335 surf
->ss7
.shader_chanel_select_a
= HSW_SCS_ALPHA
;
338 drm_intel_bo_emit_reloc(brw
->intel
.batch
.bo
,
339 brw
->wm
.surf_offset
[unit
] +
340 offsetof(struct gen7_surface_state
, ss1
),
342 surf
->ss1
.base_addr
- region
->bo
->offset
,
343 I915_GEM_DOMAIN_RENDER
,
344 I915_GEM_DOMAIN_RENDER
);
348 gen7_init_vtable_surface_functions(struct brw_context
*brw
)
350 struct intel_context
*intel
= &brw
->intel
;
352 intel
->vtbl
.update_texture_surface
= gen7_update_texture_surface
;
353 intel
->vtbl
.update_renderbuffer_surface
= gen7_update_renderbuffer_surface
;
354 intel
->vtbl
.update_null_renderbuffer_surface
=
355 gen7_update_null_renderbuffer_surface
;
356 intel
->vtbl
.create_constant_surface
= gen7_create_constant_surface
;