ilo: fix fence reference counting
[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
93 /* Fallback for buffers and npot. */
94 if ((dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) ||
95 !util_is_power_of_two(src_width0) || !util_is_power_of_two(src_height0))
96 goto fallback;
97
98 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
99 util_blitter_default_src_texture(&src_templ, src, src_level);
100
101 if (!util_blitter_is_copy_supported(i915->blitter, dst, src))
102 goto fallback;
103
104 i915_util_blitter_save_states(i915);
105
106 dst_view = i915_create_surface_custom(pipe, dst, &dst_templ, dst_width0, dst_height0);
107 src_view = i915_create_sampler_view_custom(pipe, src, &src_templ, src_width0, src_height0);
108
109 u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),
110 abs(src_box->depth), &dstbox);
111
112 util_blitter_blit_generic(i915->blitter, dst_view, &dstbox,
113 src_view, src_box, src_width0, src_height0,
114 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL);
115 return;
116
117 fallback:
118 util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
119 src, src_level, src_box);
120 }
121
122 static void
123 i915_clear_render_target_render(struct pipe_context *pipe,
124 struct pipe_surface *dst,
125 const union pipe_color_union *color,
126 unsigned dstx, unsigned dsty,
127 unsigned width, unsigned height)
128 {
129 struct i915_context *i915 = i915_context(pipe);
130 struct pipe_framebuffer_state fb_state;
131
132 util_blitter_save_framebuffer(i915->blitter, &i915->framebuffer);
133
134 fb_state.width = dst->width;
135 fb_state.height = dst->height;
136 fb_state.nr_cbufs = 1;
137 fb_state.cbufs[0] = dst;
138 fb_state.zsbuf = NULL;
139 pipe->set_framebuffer_state(pipe, &fb_state);
140
141 if (i915->dirty)
142 i915_update_derived(i915);
143
144 i915_clear_emit(pipe, PIPE_CLEAR_COLOR, color, 0.0, 0x0,
145 dstx, dsty, width, height);
146
147 pipe->set_framebuffer_state(pipe, &i915->blitter->saved_fb_state);
148 util_unreference_framebuffer_state(&i915->blitter->saved_fb_state);
149 i915->blitter->saved_fb_state.nr_cbufs = ~0;
150 }
151
152 static void
153 i915_clear_depth_stencil_render(struct pipe_context *pipe,
154 struct pipe_surface *dst,
155 unsigned clear_flags,
156 double depth,
157 unsigned stencil,
158 unsigned dstx, unsigned dsty,
159 unsigned width, unsigned height)
160 {
161 struct i915_context *i915 = i915_context(pipe);
162 struct pipe_framebuffer_state fb_state;
163
164 util_blitter_save_framebuffer(i915->blitter, &i915->framebuffer);
165
166 fb_state.width = dst->width;
167 fb_state.height = dst->height;
168 fb_state.nr_cbufs = 0;
169 fb_state.zsbuf = dst;
170 pipe->set_framebuffer_state(pipe, &fb_state);
171
172 if (i915->dirty)
173 i915_update_derived(i915);
174
175 i915_clear_emit(pipe, clear_flags & PIPE_CLEAR_DEPTHSTENCIL,
176 NULL, depth, stencil,
177 dstx, dsty, width, height);
178
179 pipe->set_framebuffer_state(pipe, &i915->blitter->saved_fb_state);
180 util_unreference_framebuffer_state(&i915->blitter->saved_fb_state);
181 i915->blitter->saved_fb_state.nr_cbufs = ~0;
182 }
183
184 /*
185 * surface functions using the blitter
186 */
187
188 /* Assumes all values are within bounds -- no checking at this level -
189 * do it higher up if required.
190 */
191 static void
192 i915_surface_copy_blitter(struct pipe_context *pipe,
193 struct pipe_resource *dst, unsigned dst_level,
194 unsigned dstx, unsigned dsty, unsigned dstz,
195 struct pipe_resource *src, unsigned src_level,
196 const struct pipe_box *src_box)
197 {
198 struct i915_texture *dst_tex = i915_texture(dst);
199 struct i915_texture *src_tex = i915_texture(src);
200 struct pipe_resource *dpt = &dst_tex->b.b;
201 struct pipe_resource *spt = &src_tex->b.b;
202 unsigned dst_offset, src_offset; /* in bytes */
203
204 /* Fallback for buffers. */
205 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
206 util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
207 src, src_level, src_box);
208 return;
209 }
210
211 /* XXX cannot copy 3d regions at this time */
212 assert(src_box->depth == 1);
213 if (dst->target != PIPE_TEXTURE_CUBE &&
214 dst->target != PIPE_TEXTURE_3D)
215 assert(dstz == 0);
216 dst_offset = i915_texture_offset(dst_tex, dst_level, dstz);
217
218 if (src->target != PIPE_TEXTURE_CUBE &&
219 src->target != PIPE_TEXTURE_3D)
220 assert(src_box->z == 0);
221 src_offset = i915_texture_offset(src_tex, src_level, src_box->z);
222
223 assert( util_format_get_blocksize(dpt->format) == util_format_get_blocksize(spt->format) );
224 assert( util_format_get_blockwidth(dpt->format) == util_format_get_blockwidth(spt->format) );
225 assert( util_format_get_blockheight(dpt->format) == util_format_get_blockheight(spt->format) );
226 assert( util_format_get_blockwidth(dpt->format) == 1 );
227 assert( util_format_get_blockheight(dpt->format) == 1 );
228
229 i915_copy_blit( i915_context(pipe),
230 util_format_get_blocksize(dpt->format),
231 (unsigned short) src_tex->stride, src_tex->buffer, src_offset,
232 (unsigned short) dst_tex->stride, dst_tex->buffer, dst_offset,
233 (short) src_box->x, (short) src_box->y, (short) dstx, (short) dsty,
234 (short) src_box->width, (short) src_box->height );
235 }
236
237 static void
238 i915_blit(struct pipe_context *pipe, const struct pipe_blit_info *blit_info)
239 {
240 struct i915_context *i915 = i915_context(pipe);
241 struct pipe_blit_info info = *blit_info;
242
243 if (util_try_blit_via_copy_region(pipe, &info)) {
244 return; /* done */
245 }
246
247 if (info.mask & PIPE_MASK_S) {
248 debug_printf("i915: cannot blit stencil, skipping\n");
249 info.mask &= ~PIPE_MASK_S;
250 }
251
252 if (!util_blitter_is_blit_supported(i915->blitter, &info)) {
253 debug_printf("i915: blit unsupported %s -> %s\n",
254 util_format_short_name(info.src.resource->format),
255 util_format_short_name(info.dst.resource->format));
256 return;
257 }
258
259 i915_util_blitter_save_states(i915);
260
261 util_blitter_blit(i915->blitter, &info);
262 }
263
264 static void
265 i915_flush_resource(struct pipe_context *ctx, struct pipe_resource *resource)
266 {
267 }
268
269 static void
270 i915_clear_render_target_blitter(struct pipe_context *pipe,
271 struct pipe_surface *dst,
272 const union pipe_color_union *color,
273 unsigned dstx, unsigned dsty,
274 unsigned width, unsigned height)
275 {
276 struct i915_texture *tex = i915_texture(dst->texture);
277 struct pipe_resource *pt = &tex->b.b;
278 union util_color uc;
279 unsigned offset = i915_texture_offset(tex, dst->u.tex.level, dst->u.tex.first_layer);
280
281 assert(util_format_get_blockwidth(pt->format) == 1);
282 assert(util_format_get_blockheight(pt->format) == 1);
283
284 util_pack_color(color->f, dst->format, &uc);
285 i915_fill_blit( i915_context(pipe),
286 util_format_get_blocksize(pt->format),
287 XY_COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB,
288 (unsigned short) tex->stride,
289 tex->buffer, offset,
290 (short) dstx, (short) dsty,
291 (short) width, (short) height,
292 uc.ui[0] );
293 }
294
295 static void
296 i915_clear_depth_stencil_blitter(struct pipe_context *pipe,
297 struct pipe_surface *dst,
298 unsigned clear_flags,
299 double depth,
300 unsigned stencil,
301 unsigned dstx, unsigned dsty,
302 unsigned width, unsigned height)
303 {
304 struct i915_texture *tex = i915_texture(dst->texture);
305 struct pipe_resource *pt = &tex->b.b;
306 unsigned packedds;
307 unsigned mask = 0;
308 unsigned offset = i915_texture_offset(tex, dst->u.tex.level, dst->u.tex.first_layer);
309
310 assert(util_format_get_blockwidth(pt->format) == 1);
311 assert(util_format_get_blockheight(pt->format) == 1);
312
313 packedds = util_pack_z_stencil(dst->format, depth, stencil);
314
315 if (clear_flags & PIPE_CLEAR_DEPTH)
316 mask |= XY_COLOR_BLT_WRITE_RGB;
317 /* XXX presumably this does read-modify-write
318 (otherwise this won't work anyway). Hence will only want to
319 do it if really have stencil and it isn't cleared */
320 if ((clear_flags & PIPE_CLEAR_STENCIL) ||
321 (dst->format != PIPE_FORMAT_Z24_UNORM_S8_UINT))
322 mask |= XY_COLOR_BLT_WRITE_ALPHA;
323
324 i915_fill_blit( i915_context(pipe),
325 util_format_get_blocksize(pt->format),
326 mask,
327 (unsigned short) tex->stride,
328 tex->buffer, offset,
329 (short) dstx, (short) dsty,
330 (short) width, (short) height,
331 packedds );
332 }
333
334 /*
335 * Screen surface functions
336 */
337
338
339 static struct pipe_surface *
340 i915_create_surface_custom(struct pipe_context *ctx,
341 struct pipe_resource *pt,
342 const struct pipe_surface *surf_tmpl,
343 unsigned width0,
344 unsigned height0)
345 {
346 struct pipe_surface *ps;
347
348 assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
349 if (pt->target != PIPE_TEXTURE_CUBE &&
350 pt->target != PIPE_TEXTURE_3D)
351 assert(surf_tmpl->u.tex.first_layer == 0);
352
353 ps = CALLOC_STRUCT(pipe_surface);
354 if (ps) {
355 /* could subclass pipe_surface and store offset as it used to do */
356 pipe_reference_init(&ps->reference, 1);
357 pipe_resource_reference(&ps->texture, pt);
358 ps->format = surf_tmpl->format;
359 ps->width = u_minify(width0, surf_tmpl->u.tex.level);
360 ps->height = u_minify(height0, surf_tmpl->u.tex.level);
361 ps->u.tex.level = surf_tmpl->u.tex.level;
362 ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
363 ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
364 ps->context = ctx;
365 }
366 return ps;
367 }
368
369 static struct pipe_surface *
370 i915_create_surface(struct pipe_context *ctx,
371 struct pipe_resource *pt,
372 const struct pipe_surface *surf_tmpl)
373 {
374 return i915_create_surface_custom(ctx, pt, surf_tmpl,
375 pt->width0, pt->height0);
376 }
377
378 static void
379 i915_surface_destroy(struct pipe_context *ctx,
380 struct pipe_surface *surf)
381 {
382 pipe_resource_reference(&surf->texture, NULL);
383 FREE(surf);
384 }
385
386
387 void
388 i915_init_surface_functions(struct i915_context *i915)
389 {
390 if (i915_screen(i915->base.screen)->debug.use_blitter) {
391 i915->base.resource_copy_region = i915_surface_copy_blitter;
392 i915->base.clear_render_target = i915_clear_render_target_blitter;
393 i915->base.clear_depth_stencil = i915_clear_depth_stencil_blitter;
394 } else {
395 i915->base.resource_copy_region = i915_surface_copy_render;
396 i915->base.clear_render_target = i915_clear_render_target_render;
397 i915->base.clear_depth_stencil = i915_clear_depth_stencil_render;
398 }
399 i915->base.blit = i915_blit;
400 i915->base.flush_resource = i915_flush_resource;
401 i915->base.create_surface = i915_create_surface;
402 i915->base.surface_destroy = i915_surface_destroy;
403 }