gallium: Add a cap for offset_units_unscaled
[mesa.git] / src / gallium / drivers / i915 / i915_surface.c
1 /**************************************************************************
2 *
3 * Copyright 2003 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * 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, sub license, 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 portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "i915_surface.h"
29 #include "i915_resource.h"
30 #include "i915_state.h"
31 #include "i915_blit.h"
32 #include "i915_reg.h"
33 #include "i915_screen.h"
34 #include "pipe/p_defines.h"
35 #include "util/u_inlines.h"
36 #include "util/u_math.h"
37 #include "util/u_format.h"
38 #include "util/u_memory.h"
39 #include "util/u_pack_color.h"
40 #include "util/u_surface.h"
41
42 static struct pipe_surface *
43 i915_create_surface_custom(struct pipe_context *ctx,
44 struct pipe_resource *pt,
45 const struct pipe_surface *surf_tmpl,
46 unsigned width0,
47 unsigned height0);
48 /*
49 * surface functions using the render engine
50 */
51
52 static void
53 i915_util_blitter_save_states(struct i915_context *i915)
54 {
55 util_blitter_save_blend(i915->blitter, (void *)i915->blend);
56 util_blitter_save_depth_stencil_alpha(i915->blitter, (void *)i915->depth_stencil);
57 util_blitter_save_stencil_ref(i915->blitter, &i915->stencil_ref);
58 util_blitter_save_rasterizer(i915->blitter, (void *)i915->rasterizer);
59 util_blitter_save_fragment_shader(i915->blitter, i915->fs);
60 util_blitter_save_vertex_shader(i915->blitter, i915->vs);
61 util_blitter_save_viewport(i915->blitter, &i915->viewport);
62 util_blitter_save_scissor(i915->blitter, &i915->scissor);
63 util_blitter_save_vertex_elements(i915->blitter, i915->velems);
64 util_blitter_save_vertex_buffer_slot(i915->blitter,
65 i915->vertex_buffers);
66
67 util_blitter_save_framebuffer(i915->blitter, &i915->framebuffer);
68
69 util_blitter_save_fragment_sampler_states(i915->blitter,
70 i915->num_samplers,
71 (void**)i915->fragment_sampler);
72 util_blitter_save_fragment_sampler_views(i915->blitter,
73 i915->num_fragment_sampler_views,
74 i915->fragment_sampler_views);
75 }
76
77 static void
78 i915_surface_copy_render(struct pipe_context *pipe,
79 struct pipe_resource *dst, unsigned dst_level,
80 unsigned dstx, unsigned dsty, unsigned dstz,
81 struct pipe_resource *src, unsigned src_level,
82 const struct pipe_box *src_box)
83 {
84 struct i915_context *i915 = i915_context(pipe);
85 unsigned src_width0 = src->width0;
86 unsigned src_height0 = src->height0;
87 unsigned dst_width0 = dst->width0;
88 unsigned dst_height0 = dst->height0;
89 struct pipe_box dstbox;
90 struct pipe_sampler_view src_templ, *src_view;
91 struct pipe_surface dst_templ, *dst_view;
92 const struct util_format_description *desc;
93
94 /* Fallback for buffers. */
95 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER)
96 goto fallback;
97
98 /* Fallback for depth&stencil. XXX: see if we can use a proxy format */
99 desc = util_format_description(src->format);
100 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS)
101 goto fallback;
102
103 desc = util_format_description(dst->format);
104 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS)
105 goto fallback;
106
107 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
108 util_blitter_default_src_texture(&src_templ, src, src_level);
109
110 if (!util_blitter_is_copy_supported(i915->blitter, dst, src))
111 goto fallback;
112
113 i915_util_blitter_save_states(i915);
114
115 dst_view = i915_create_surface_custom(pipe, dst, &dst_templ, dst_width0, dst_height0);
116 src_view = i915_create_sampler_view_custom(pipe, src, &src_templ, src_width0, src_height0);
117
118 u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),
119 abs(src_box->depth), &dstbox);
120
121 util_blitter_blit_generic(i915->blitter, dst_view, &dstbox,
122 src_view, src_box, src_width0, src_height0,
123 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
124 FALSE);
125 return;
126
127 fallback:
128 util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
129 src, src_level, src_box);
130 }
131
132 static void
133 i915_clear_render_target_render(struct pipe_context *pipe,
134 struct pipe_surface *dst,
135 const union pipe_color_union *color,
136 unsigned dstx, unsigned dsty,
137 unsigned width, unsigned height)
138 {
139 struct i915_context *i915 = i915_context(pipe);
140 struct pipe_framebuffer_state fb_state;
141
142 util_blitter_save_framebuffer(i915->blitter, &i915->framebuffer);
143
144 fb_state.width = dst->width;
145 fb_state.height = dst->height;
146 fb_state.nr_cbufs = 1;
147 fb_state.cbufs[0] = dst;
148 fb_state.zsbuf = NULL;
149 pipe->set_framebuffer_state(pipe, &fb_state);
150
151 if (i915->dirty)
152 i915_update_derived(i915);
153
154 i915_clear_emit(pipe, PIPE_CLEAR_COLOR, color, 0.0, 0x0,
155 dstx, dsty, width, height);
156
157 pipe->set_framebuffer_state(pipe, &i915->blitter->saved_fb_state);
158 util_unreference_framebuffer_state(&i915->blitter->saved_fb_state);
159 i915->blitter->saved_fb_state.nr_cbufs = ~0;
160 }
161
162 static void
163 i915_clear_depth_stencil_render(struct pipe_context *pipe,
164 struct pipe_surface *dst,
165 unsigned clear_flags,
166 double depth,
167 unsigned stencil,
168 unsigned dstx, unsigned dsty,
169 unsigned width, unsigned height)
170 {
171 struct i915_context *i915 = i915_context(pipe);
172 struct pipe_framebuffer_state fb_state;
173
174 util_blitter_save_framebuffer(i915->blitter, &i915->framebuffer);
175
176 fb_state.width = dst->width;
177 fb_state.height = dst->height;
178 fb_state.nr_cbufs = 0;
179 fb_state.zsbuf = dst;
180 pipe->set_framebuffer_state(pipe, &fb_state);
181
182 if (i915->dirty)
183 i915_update_derived(i915);
184
185 i915_clear_emit(pipe, clear_flags & PIPE_CLEAR_DEPTHSTENCIL,
186 NULL, depth, stencil,
187 dstx, dsty, width, height);
188
189 pipe->set_framebuffer_state(pipe, &i915->blitter->saved_fb_state);
190 util_unreference_framebuffer_state(&i915->blitter->saved_fb_state);
191 i915->blitter->saved_fb_state.nr_cbufs = ~0;
192 }
193
194 /*
195 * surface functions using the blitter
196 */
197
198 /* Assumes all values are within bounds -- no checking at this level -
199 * do it higher up if required.
200 */
201 static void
202 i915_surface_copy_blitter(struct pipe_context *pipe,
203 struct pipe_resource *dst, unsigned dst_level,
204 unsigned dstx, unsigned dsty, unsigned dstz,
205 struct pipe_resource *src, unsigned src_level,
206 const struct pipe_box *src_box)
207 {
208 struct i915_texture *dst_tex = i915_texture(dst);
209 struct i915_texture *src_tex = i915_texture(src);
210 struct pipe_resource *dpt = &dst_tex->b.b;
211 struct pipe_resource *spt = &src_tex->b.b;
212 unsigned dst_offset, src_offset; /* in bytes */
213
214 /* Fallback for buffers. */
215 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
216 util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
217 src, src_level, src_box);
218 return;
219 }
220
221 /* XXX cannot copy 3d regions at this time */
222 assert(src_box->depth == 1);
223 if (dst->target != PIPE_TEXTURE_CUBE &&
224 dst->target != PIPE_TEXTURE_3D)
225 assert(dstz == 0);
226 dst_offset = i915_texture_offset(dst_tex, dst_level, dstz);
227
228 if (src->target != PIPE_TEXTURE_CUBE &&
229 src->target != PIPE_TEXTURE_3D)
230 assert(src_box->z == 0);
231 src_offset = i915_texture_offset(src_tex, src_level, src_box->z);
232
233 assert( util_format_get_blocksize(dpt->format) == util_format_get_blocksize(spt->format) );
234 assert( util_format_get_blockwidth(dpt->format) == util_format_get_blockwidth(spt->format) );
235 assert( util_format_get_blockheight(dpt->format) == util_format_get_blockheight(spt->format) );
236 assert( util_format_get_blockwidth(dpt->format) == 1 );
237 assert( util_format_get_blockheight(dpt->format) == 1 );
238
239 i915_copy_blit( i915_context(pipe),
240 util_format_get_blocksize(dpt->format),
241 (unsigned short) src_tex->stride, src_tex->buffer, src_offset,
242 (unsigned short) dst_tex->stride, dst_tex->buffer, dst_offset,
243 (short) src_box->x, (short) src_box->y, (short) dstx, (short) dsty,
244 (short) src_box->width, (short) src_box->height );
245 }
246
247 static void
248 i915_blit(struct pipe_context *pipe, const struct pipe_blit_info *blit_info)
249 {
250 struct i915_context *i915 = i915_context(pipe);
251 struct pipe_blit_info info = *blit_info;
252
253 if (util_try_blit_via_copy_region(pipe, &info)) {
254 return; /* done */
255 }
256
257 if (info.mask & PIPE_MASK_S) {
258 debug_printf("i915: cannot blit stencil, skipping\n");
259 info.mask &= ~PIPE_MASK_S;
260 }
261
262 if (!util_blitter_is_blit_supported(i915->blitter, &info)) {
263 debug_printf("i915: blit unsupported %s -> %s\n",
264 util_format_short_name(info.src.resource->format),
265 util_format_short_name(info.dst.resource->format));
266 return;
267 }
268
269 i915_util_blitter_save_states(i915);
270
271 util_blitter_blit(i915->blitter, &info);
272 }
273
274 static void
275 i915_flush_resource(struct pipe_context *ctx, struct pipe_resource *resource)
276 {
277 }
278
279 static void
280 i915_clear_render_target_blitter(struct pipe_context *pipe,
281 struct pipe_surface *dst,
282 const union pipe_color_union *color,
283 unsigned dstx, unsigned dsty,
284 unsigned width, unsigned height)
285 {
286 struct i915_texture *tex = i915_texture(dst->texture);
287 struct pipe_resource *pt = &tex->b.b;
288 union util_color uc;
289 unsigned offset = i915_texture_offset(tex, dst->u.tex.level, dst->u.tex.first_layer);
290
291 assert(util_format_get_blockwidth(pt->format) == 1);
292 assert(util_format_get_blockheight(pt->format) == 1);
293
294 util_pack_color(color->f, dst->format, &uc);
295 i915_fill_blit( i915_context(pipe),
296 util_format_get_blocksize(pt->format),
297 XY_COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB,
298 (unsigned short) tex->stride,
299 tex->buffer, offset,
300 (short) dstx, (short) dsty,
301 (short) width, (short) height,
302 uc.ui[0] );
303 }
304
305 static void
306 i915_clear_depth_stencil_blitter(struct pipe_context *pipe,
307 struct pipe_surface *dst,
308 unsigned clear_flags,
309 double depth,
310 unsigned stencil,
311 unsigned dstx, unsigned dsty,
312 unsigned width, unsigned height)
313 {
314 struct i915_texture *tex = i915_texture(dst->texture);
315 struct pipe_resource *pt = &tex->b.b;
316 unsigned packedds;
317 unsigned mask = 0;
318 unsigned offset = i915_texture_offset(tex, dst->u.tex.level, dst->u.tex.first_layer);
319
320 assert(util_format_get_blockwidth(pt->format) == 1);
321 assert(util_format_get_blockheight(pt->format) == 1);
322
323 packedds = util_pack_z_stencil(dst->format, depth, stencil);
324
325 if (clear_flags & PIPE_CLEAR_DEPTH)
326 mask |= XY_COLOR_BLT_WRITE_RGB;
327 /* XXX presumably this does read-modify-write
328 (otherwise this won't work anyway). Hence will only want to
329 do it if really have stencil and it isn't cleared */
330 if ((clear_flags & PIPE_CLEAR_STENCIL) ||
331 (dst->format != PIPE_FORMAT_Z24_UNORM_S8_UINT))
332 mask |= XY_COLOR_BLT_WRITE_ALPHA;
333
334 i915_fill_blit( i915_context(pipe),
335 util_format_get_blocksize(pt->format),
336 mask,
337 (unsigned short) tex->stride,
338 tex->buffer, offset,
339 (short) dstx, (short) dsty,
340 (short) width, (short) height,
341 packedds );
342 }
343
344 /*
345 * Screen surface functions
346 */
347
348
349 static struct pipe_surface *
350 i915_create_surface_custom(struct pipe_context *ctx,
351 struct pipe_resource *pt,
352 const struct pipe_surface *surf_tmpl,
353 unsigned width0,
354 unsigned height0)
355 {
356 struct pipe_surface *ps;
357
358 assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
359 if (pt->target != PIPE_TEXTURE_CUBE &&
360 pt->target != PIPE_TEXTURE_3D)
361 assert(surf_tmpl->u.tex.first_layer == 0);
362
363 ps = CALLOC_STRUCT(pipe_surface);
364 if (ps) {
365 /* could subclass pipe_surface and store offset as it used to do */
366 pipe_reference_init(&ps->reference, 1);
367 pipe_resource_reference(&ps->texture, pt);
368 ps->format = surf_tmpl->format;
369 ps->width = u_minify(width0, surf_tmpl->u.tex.level);
370 ps->height = u_minify(height0, surf_tmpl->u.tex.level);
371 ps->u.tex.level = surf_tmpl->u.tex.level;
372 ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
373 ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
374 ps->context = ctx;
375 }
376 return ps;
377 }
378
379 static struct pipe_surface *
380 i915_create_surface(struct pipe_context *ctx,
381 struct pipe_resource *pt,
382 const struct pipe_surface *surf_tmpl)
383 {
384 return i915_create_surface_custom(ctx, pt, surf_tmpl,
385 pt->width0, pt->height0);
386 }
387
388 static void
389 i915_surface_destroy(struct pipe_context *ctx,
390 struct pipe_surface *surf)
391 {
392 pipe_resource_reference(&surf->texture, NULL);
393 FREE(surf);
394 }
395
396
397 void
398 i915_init_surface_functions(struct i915_context *i915)
399 {
400 if (i915_screen(i915->base.screen)->debug.use_blitter) {
401 i915->base.resource_copy_region = i915_surface_copy_blitter;
402 i915->base.clear_render_target = i915_clear_render_target_blitter;
403 i915->base.clear_depth_stencil = i915_clear_depth_stencil_blitter;
404 } else {
405 i915->base.resource_copy_region = i915_surface_copy_render;
406 i915->base.clear_render_target = i915_clear_render_target_render;
407 i915->base.clear_depth_stencil = i915_clear_depth_stencil_render;
408 }
409 i915->base.blit = i915_blit;
410 i915->base.flush_resource = i915_flush_resource;
411 i915->base.create_surface = i915_create_surface;
412 i915->base.surface_destroy = i915_surface_destroy;
413 }