i915g: Remove heuristic flushing.
[mesa.git] / src / gallium / drivers / i915 / i915_surface.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 /*
43 * surface functions using the render engine
44 */
45
46 static void
47 i915_surface_copy_render(struct pipe_context *pipe,
48 struct pipe_resource *dst, unsigned dst_level,
49 unsigned dstx, unsigned dsty, unsigned dstz,
50 struct pipe_resource *src, unsigned src_level,
51 const struct pipe_box *src_box)
52 {
53 struct i915_context *i915 = i915_context(pipe);
54
55 /* Fallback for buffers. */
56 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
57 util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
58 src, src_level, src_box);
59 return;
60 }
61
62 util_blitter_save_blend(i915->blitter, (void *)i915->blend);
63 util_blitter_save_depth_stencil_alpha(i915->blitter, (void *)i915->depth_stencil);
64 util_blitter_save_stencil_ref(i915->blitter, &i915->stencil_ref);
65 util_blitter_save_rasterizer(i915->blitter, (void *)i915->rasterizer);
66 util_blitter_save_fragment_shader(i915->blitter, i915->saved_fs);
67 util_blitter_save_vertex_shader(i915->blitter, i915->saved_vs);
68 util_blitter_save_viewport(i915->blitter, &i915->viewport);
69 util_blitter_save_clip(i915->blitter, &i915->saved_clip);
70 util_blitter_save_vertex_elements(i915->blitter, i915->saved_velems);
71 util_blitter_save_vertex_buffers(i915->blitter, i915->saved_nr_vertex_buffers,
72 i915->saved_vertex_buffers);
73
74 util_blitter_save_framebuffer(i915->blitter, &i915->framebuffer);
75
76 util_blitter_save_fragment_sampler_states(i915->blitter,
77 i915->saved_nr_samplers,
78 i915->saved_samplers);
79 util_blitter_save_fragment_sampler_views(i915->blitter,
80 i915->saved_nr_sampler_views,
81 i915->saved_sampler_views);
82
83 util_blitter_copy_region(i915->blitter, dst, dst_level, dstx, dsty, dstz,
84 src, src_level, src_box, TRUE);
85 }
86
87 static void
88 i915_clear_render_target_render(struct pipe_context *pipe,
89 struct pipe_surface *dst,
90 const float *rgba,
91 unsigned dstx, unsigned dsty,
92 unsigned width, unsigned height)
93 {
94 struct i915_context *i915 = i915_context(pipe);
95 struct pipe_framebuffer_state fb_state;
96
97 util_blitter_save_framebuffer(i915->blitter, &i915->framebuffer);
98
99 fb_state.width = dst->width;
100 fb_state.height = dst->height;
101 fb_state.nr_cbufs = 1;
102 fb_state.cbufs[0] = dst;
103 fb_state.zsbuf = NULL;
104 pipe->set_framebuffer_state(pipe, &fb_state);
105
106 if (i915->dirty)
107 i915_update_derived(i915);
108
109 i915_clear_emit(pipe, PIPE_CLEAR_COLOR, rgba, 0.0, 0x0,
110 dstx, dsty, width, height);
111
112 pipe->set_framebuffer_state(pipe, &i915->blitter->saved_fb_state);
113 util_unreference_framebuffer_state(&i915->blitter->saved_fb_state);
114 i915->blitter->saved_fb_state.nr_cbufs = ~0;
115 }
116
117 static void
118 i915_clear_depth_stencil_render(struct pipe_context *pipe,
119 struct pipe_surface *dst,
120 unsigned clear_flags,
121 double depth,
122 unsigned stencil,
123 unsigned dstx, unsigned dsty,
124 unsigned width, unsigned height)
125 {
126 struct i915_context *i915 = i915_context(pipe);
127 struct pipe_framebuffer_state fb_state;
128
129 util_blitter_save_framebuffer(i915->blitter, &i915->framebuffer);
130
131 fb_state.width = dst->width;
132 fb_state.height = dst->height;
133 fb_state.nr_cbufs = 0;
134 fb_state.zsbuf = dst;
135 pipe->set_framebuffer_state(pipe, &fb_state);
136
137 if (i915->dirty)
138 i915_update_derived(i915);
139
140 i915_clear_emit(pipe, clear_flags & PIPE_CLEAR_DEPTHSTENCIL,
141 NULL, depth, stencil,
142 dstx, dsty, width, height);
143
144 pipe->set_framebuffer_state(pipe, &i915->blitter->saved_fb_state);
145 util_unreference_framebuffer_state(&i915->blitter->saved_fb_state);
146 i915->blitter->saved_fb_state.nr_cbufs = ~0;
147 }
148
149 /*
150 * surface functions using the blitter
151 */
152
153 /* Assumes all values are within bounds -- no checking at this level -
154 * do it higher up if required.
155 */
156 static void
157 i915_surface_copy_blitter(struct pipe_context *pipe,
158 struct pipe_resource *dst, unsigned dst_level,
159 unsigned dstx, unsigned dsty, unsigned dstz,
160 struct pipe_resource *src, unsigned src_level,
161 const struct pipe_box *src_box)
162 {
163 struct i915_texture *dst_tex = i915_texture(dst);
164 struct i915_texture *src_tex = i915_texture(src);
165 struct pipe_resource *dpt = &dst_tex->b.b;
166 struct pipe_resource *spt = &src_tex->b.b;
167 unsigned dst_offset, src_offset; /* in bytes */
168
169 /* Fallback for buffers. */
170 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
171 util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
172 src, src_level, src_box);
173 return;
174 }
175
176 /* XXX cannot copy 3d regions at this time */
177 assert(src_box->depth == 1);
178 if (dst->target != PIPE_TEXTURE_CUBE &&
179 dst->target != PIPE_TEXTURE_3D)
180 assert(dstz == 0);
181 dst_offset = i915_texture_offset(dst_tex, dst_level, dstz);
182
183 if (src->target != PIPE_TEXTURE_CUBE &&
184 src->target != PIPE_TEXTURE_3D)
185 assert(src_box->z == 0);
186 src_offset = i915_texture_offset(src_tex, src_level, src_box->z);
187
188 assert( util_format_get_blocksize(dpt->format) == util_format_get_blocksize(spt->format) );
189 assert( util_format_get_blockwidth(dpt->format) == util_format_get_blockwidth(spt->format) );
190 assert( util_format_get_blockheight(dpt->format) == util_format_get_blockheight(spt->format) );
191 assert( util_format_get_blockwidth(dpt->format) == 1 );
192 assert( util_format_get_blockheight(dpt->format) == 1 );
193
194 i915_copy_blit( i915_context(pipe),
195 util_format_get_blocksize(dpt->format),
196 (unsigned short) src_tex->stride, src_tex->buffer, src_offset,
197 (unsigned short) dst_tex->stride, dst_tex->buffer, dst_offset,
198 (short) src_box->x, (short) src_box->y, (short) dstx, (short) dsty,
199 (short) src_box->width, (short) src_box->height );
200 }
201
202 static void
203 i915_clear_render_target_blitter(struct pipe_context *pipe,
204 struct pipe_surface *dst,
205 const float *rgba,
206 unsigned dstx, unsigned dsty,
207 unsigned width, unsigned height)
208 {
209 struct i915_texture *tex = i915_texture(dst->texture);
210 struct pipe_resource *pt = &tex->b.b;
211 union util_color uc;
212 unsigned offset = i915_texture_offset(tex, dst->u.tex.level, dst->u.tex.first_layer);
213
214 assert(util_format_get_blockwidth(pt->format) == 1);
215 assert(util_format_get_blockheight(pt->format) == 1);
216
217 util_pack_color(rgba, dst->format, &uc);
218 i915_fill_blit( i915_context(pipe),
219 util_format_get_blocksize(pt->format),
220 XY_COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB,
221 (unsigned short) tex->stride,
222 tex->buffer, offset,
223 (short) dstx, (short) dsty,
224 (short) width, (short) height,
225 uc.ui );
226 }
227
228 static void
229 i915_clear_depth_stencil_blitter(struct pipe_context *pipe,
230 struct pipe_surface *dst,
231 unsigned clear_flags,
232 double depth,
233 unsigned stencil,
234 unsigned dstx, unsigned dsty,
235 unsigned width, unsigned height)
236 {
237 struct i915_texture *tex = i915_texture(dst->texture);
238 struct pipe_resource *pt = &tex->b.b;
239 unsigned packedds;
240 unsigned mask = 0;
241 unsigned offset = i915_texture_offset(tex, dst->u.tex.level, dst->u.tex.first_layer);
242
243 assert(util_format_get_blockwidth(pt->format) == 1);
244 assert(util_format_get_blockheight(pt->format) == 1);
245
246 packedds = util_pack_z_stencil(dst->format, depth, stencil);
247
248 if (clear_flags & PIPE_CLEAR_DEPTH)
249 mask |= XY_COLOR_BLT_WRITE_RGB;
250 /* XXX presumably this does read-modify-write
251 (otherwise this won't work anyway). Hence will only want to
252 do it if really have stencil and it isn't cleared */
253 if ((clear_flags & PIPE_CLEAR_STENCIL) ||
254 (dst->format != PIPE_FORMAT_Z24_UNORM_S8_USCALED))
255 mask |= XY_COLOR_BLT_WRITE_ALPHA;
256
257 i915_fill_blit( i915_context(pipe),
258 util_format_get_blocksize(pt->format),
259 mask,
260 (unsigned short) tex->stride,
261 tex->buffer, offset,
262 (short) dstx, (short) dsty,
263 (short) width, (short) height,
264 packedds );
265 }
266
267 /*
268 * Screen surface functions
269 */
270
271
272 static struct pipe_surface *
273 i915_create_surface(struct pipe_context *ctx,
274 struct pipe_resource *pt,
275 const struct pipe_surface *surf_tmpl)
276 {
277 struct pipe_surface *ps;
278
279 assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
280 if (pt->target != PIPE_TEXTURE_CUBE &&
281 pt->target != PIPE_TEXTURE_3D)
282 assert(surf_tmpl->u.tex.first_layer == 0);
283
284 ps = CALLOC_STRUCT(pipe_surface);
285 if (ps) {
286 /* could subclass pipe_surface and store offset as it used to do */
287 pipe_reference_init(&ps->reference, 1);
288 pipe_resource_reference(&ps->texture, pt);
289 ps->format = surf_tmpl->format;
290 ps->width = u_minify(pt->width0, surf_tmpl->u.tex.level);
291 ps->height = u_minify(pt->height0, surf_tmpl->u.tex.level);
292 ps->u.tex.level = surf_tmpl->u.tex.level;
293 ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
294 ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
295 ps->usage = surf_tmpl->usage;
296 ps->context = ctx;
297 }
298 return ps;
299 }
300
301 static void
302 i915_surface_destroy(struct pipe_context *ctx,
303 struct pipe_surface *surf)
304 {
305 pipe_resource_reference(&surf->texture, NULL);
306 FREE(surf);
307 }
308
309
310 void
311 i915_init_surface_functions(struct i915_context *i915)
312 {
313 if (i915_screen(i915->base.screen)->debug.use_blitter) {
314 i915->base.resource_copy_region = i915_surface_copy_blitter;
315 i915->base.clear_render_target = i915_clear_render_target_blitter;
316 i915->base.clear_depth_stencil = i915_clear_depth_stencil_blitter;
317 } else {
318 i915->base.resource_copy_region = i915_surface_copy_render;
319 i915->base.clear_render_target = i915_clear_render_target_render;
320 i915->base.clear_depth_stencil = i915_clear_depth_stencil_render;
321 }
322 i915->base.create_surface = i915_create_surface;
323 i915->base.surface_destroy = i915_surface_destroy;
324 }