Merge branch 'lp-offset-twoside'
[mesa.git] / src / gallium / drivers / r600 / r600_blit.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 *
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
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
13 * Software.
14 *
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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #include <util/u_surface.h>
24 #include <util/u_blitter.h>
25 #include <util/u_format.h>
26 #include "r600_pipe.h"
27
28 enum r600_blitter_op /* bitmask */
29 {
30 R600_CLEAR = 1,
31 R600_CLEAR_SURFACE = 2,
32 R600_COPY = 4
33 };
34
35 static void r600_blitter_begin(struct pipe_context *ctx, enum r600_blitter_op op)
36 {
37 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
38
39 r600_context_queries_suspend(&rctx->ctx);
40
41 util_blitter_save_blend(rctx->blitter, rctx->states[R600_PIPE_STATE_BLEND]);
42 util_blitter_save_depth_stencil_alpha(rctx->blitter, rctx->states[R600_PIPE_STATE_DSA]);
43 if (rctx->states[R600_PIPE_STATE_STENCIL_REF]) {
44 util_blitter_save_stencil_ref(rctx->blitter, &rctx->stencil_ref);
45 }
46 util_blitter_save_rasterizer(rctx->blitter, rctx->states[R600_PIPE_STATE_RASTERIZER]);
47 util_blitter_save_fragment_shader(rctx->blitter, rctx->ps_shader);
48 util_blitter_save_vertex_shader(rctx->blitter, rctx->vs_shader);
49 util_blitter_save_vertex_elements(rctx->blitter, rctx->vertex_elements);
50 if (rctx->states[R600_PIPE_STATE_VIEWPORT]) {
51 util_blitter_save_viewport(rctx->blitter, &rctx->viewport);
52 }
53 if (rctx->states[R600_PIPE_STATE_CLIP]) {
54 util_blitter_save_clip(rctx->blitter, &rctx->clip);
55 }
56 util_blitter_save_vertex_buffers(rctx->blitter, rctx->nvertex_buffer, rctx->vertex_buffer);
57
58 rctx->vertex_elements = NULL;
59
60 if (op & (R600_CLEAR_SURFACE | R600_COPY))
61 util_blitter_save_framebuffer(rctx->blitter, &rctx->framebuffer);
62
63 if (op & R600_COPY) {
64 util_blitter_save_fragment_sampler_states(
65 rctx->blitter, rctx->ps_samplers.n_samplers,
66 (void**)rctx->ps_samplers.samplers);
67
68 util_blitter_save_fragment_sampler_views(
69 rctx->blitter, rctx->ps_samplers.n_views,
70 (struct pipe_sampler_view**)rctx->ps_samplers.views);
71 }
72
73 }
74
75 static void r600_blitter_end(struct pipe_context *ctx)
76 {
77 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
78 r600_context_queries_resume(&rctx->ctx);
79 }
80
81 int r600_blit_uncompress_depth(struct pipe_context *ctx, struct r600_resource_texture *texture)
82 {
83 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
84 struct pipe_surface *zsurf, *cbsurf;
85 int level = 0;
86 float depth = 1.0f;
87
88 zsurf = ctx->screen->get_tex_surface(ctx->screen, &texture->resource.base.b, 0, level, 0,
89 PIPE_BIND_DEPTH_STENCIL);
90
91 cbsurf = ctx->screen->get_tex_surface(ctx->screen,
92 (struct pipe_resource*)texture->flushed_depth_texture,
93 0, level, 0, PIPE_BIND_RENDER_TARGET);
94
95 if (rctx->family == CHIP_RV610 || rctx->family == CHIP_RV630 ||
96 rctx->family == CHIP_RV620 || rctx->family == CHIP_RV635)
97 depth = 0.0f;
98
99 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
100 util_blitter_custom_depth_stencil(rctx->blitter, zsurf, cbsurf, rctx->custom_dsa_flush, depth);
101 r600_blitter_end(ctx);
102
103 pipe_surface_reference(&zsurf, NULL);
104 pipe_surface_reference(&cbsurf, NULL);
105
106
107 return 0;
108 }
109
110 static void r600_clear(struct pipe_context *ctx, unsigned buffers,
111 const float *rgba, double depth, unsigned stencil)
112 {
113 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
114 struct pipe_framebuffer_state *fb = &rctx->framebuffer;
115
116 r600_blitter_begin(ctx, R600_CLEAR);
117 util_blitter_clear(rctx->blitter, fb->width, fb->height,
118 fb->nr_cbufs, buffers, rgba, depth,
119 stencil);
120 r600_blitter_end(ctx);
121 }
122
123 static void r600_clear_render_target(struct pipe_context *ctx,
124 struct pipe_surface *dst,
125 const float *rgba,
126 unsigned dstx, unsigned dsty,
127 unsigned width, unsigned height)
128 {
129 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
130
131 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
132 util_blitter_clear_render_target(rctx->blitter, dst, rgba,
133 dstx, dsty, width, height);
134 r600_blitter_end(ctx);
135 }
136
137 static void r600_clear_depth_stencil(struct pipe_context *ctx,
138 struct pipe_surface *dst,
139 unsigned clear_flags,
140 double depth,
141 unsigned stencil,
142 unsigned dstx, unsigned dsty,
143 unsigned width, unsigned height)
144 {
145 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
146
147 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
148 util_blitter_clear_depth_stencil(rctx->blitter, dst, clear_flags, depth, stencil,
149 dstx, dsty, width, height);
150 r600_blitter_end(ctx);
151 }
152
153
154
155 /* Copy a block of pixels from one surface to another using HW. */
156 static void r600_hw_copy_region(struct pipe_context *ctx,
157 struct pipe_resource *dst,
158 struct pipe_subresource subdst,
159 unsigned dstx, unsigned dsty, unsigned dstz,
160 struct pipe_resource *src,
161 struct pipe_subresource subsrc,
162 unsigned srcx, unsigned srcy, unsigned srcz,
163 unsigned width, unsigned height)
164 {
165 struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
166
167 r600_blitter_begin(ctx, R600_COPY);
168 util_blitter_copy_region(rctx->blitter, dst, subdst, dstx, dsty, dstz,
169 src, subsrc, srcx, srcy, srcz, width, height,
170 TRUE);
171 r600_blitter_end(ctx);
172 }
173
174 static void r600_resource_copy_region(struct pipe_context *ctx,
175 struct pipe_resource *dst,
176 struct pipe_subresource subdst,
177 unsigned dstx, unsigned dsty, unsigned dstz,
178 struct pipe_resource *src,
179 struct pipe_subresource subsrc,
180 unsigned srcx, unsigned srcy, unsigned srcz,
181 unsigned width, unsigned height)
182 {
183 boolean is_depth;
184 /* there is something wrong with depth resource copies at the moment so avoid them for now */
185 is_depth = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0;
186 if (is_depth)
187 util_resource_copy_region(ctx, dst, subdst, dstx, dsty, dstz,
188 src, subsrc, srcx, srcy, srcz, width, height);
189 else
190 r600_hw_copy_region(ctx, dst, subdst, dstx, dsty, dstz,
191 src, subsrc, srcx, srcy, srcz, width, height);
192 }
193
194 void r600_init_blit_functions(struct r600_pipe_context *rctx)
195 {
196 rctx->context.clear = r600_clear;
197 rctx->context.clear_render_target = r600_clear_render_target;
198 rctx->context.clear_depth_stencil = r600_clear_depth_stencil;
199 rctx->context.resource_copy_region = r600_resource_copy_region;
200 }