Merge branch '7.8'
[mesa.git] / src / gallium / drivers / r300 / r300_blit.c
1 /*
2 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
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 "r300_blit.h"
24 #include "r300_context.h"
25 #include "r300_texture.h"
26
27 #include "util/u_format.h"
28
29 static void r300_blitter_save_states(struct r300_context* r300)
30 {
31 util_blitter_save_blend(r300->blitter, r300->blend_state.state);
32 util_blitter_save_depth_stencil_alpha(r300->blitter, r300->dsa_state.state);
33 util_blitter_save_stencil_ref(r300->blitter, &(r300->stencil_ref));
34 util_blitter_save_rasterizer(r300->blitter, r300->rs_state.state);
35 util_blitter_save_fragment_shader(r300->blitter, r300->fs.state);
36 util_blitter_save_vertex_shader(r300->blitter, r300->vs_state.state);
37 util_blitter_save_viewport(r300->blitter, &r300->viewport);
38 util_blitter_save_clip(r300->blitter, &r300->clip);
39 util_blitter_save_vertex_elements(r300->blitter, r300->velems);
40 /* XXX this crashes the driver
41 util_blitter_save_vertex_buffers(r300->blitter, r300->vertex_buffer_count,
42 r300->vertex_buffer); */
43 }
44
45 /* Clear currently bound buffers. */
46 void r300_clear(struct pipe_context* pipe,
47 unsigned buffers,
48 const float* rgba,
49 double depth,
50 unsigned stencil)
51 {
52 /* XXX Implement fastfill.
53 *
54 * If fastfill is enabled, a few facts should be considered:
55 *
56 * 1) Zbuffer must be micro-tiled and whole microtiles must be
57 * written.
58 *
59 * 2) ZB_DEPTHCLEARVALUE is used to clear a zbuffer and Z Mask must be
60 * equal to 0.
61 *
62 * 3) For 16-bit integer buffering, compression causes a hung with one or
63 * two samples and should not be used.
64 *
65 * 4) Fastfill must not be used if reading of compressed Z data is disabled
66 * and writing of compressed Z data is enabled (RD/WR_COMP_ENABLE),
67 * i.e. it cannot be used to compress the zbuffer.
68 * (what the hell does that mean and how does it fit in clearing
69 * the buffers?)
70 *
71 * - Marek
72 */
73
74 struct r300_context* r300 = r300_context(pipe);
75 struct pipe_framebuffer_state* fb =
76 (struct pipe_framebuffer_state*)r300->fb_state.state;
77
78 r300_blitter_save_states(r300);
79
80 util_blitter_clear(r300->blitter,
81 fb->width,
82 fb->height,
83 fb->nr_cbufs,
84 buffers, rgba, depth, stencil);
85 }
86
87 /* Copy a block of pixels from one surface to another using HW. */
88 static void r300_hw_copy(struct pipe_context* pipe,
89 struct pipe_surface* dst,
90 unsigned dstx, unsigned dsty,
91 struct pipe_surface* src,
92 unsigned srcx, unsigned srcy,
93 unsigned width, unsigned height)
94 {
95 struct r300_context* r300 = r300_context(pipe);
96 struct r300_textures_state* state =
97 (struct r300_textures_state*)r300->textures_state.state;
98
99 /* Yeah we have to save all those states to ensure this blitter operation
100 * is really transparent. The states will be restored by the blitter once
101 * copying is done. */
102 r300_blitter_save_states(r300);
103 util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);
104
105 util_blitter_save_fragment_sampler_states(
106 r300->blitter, state->sampler_state_count,
107 (void**)state->sampler_states);
108
109 util_blitter_save_fragment_sampler_views(
110 r300->blitter, state->sampler_view_count,
111 (struct pipe_sampler_view**)state->sampler_views);
112
113 /* Do a copy */
114 util_blitter_copy(r300->blitter,
115 dst, dstx, dsty, src, srcx, srcy, width, height, TRUE);
116 }
117
118 /* Copy a block of pixels from one surface to another. */
119 void r300_surface_copy(struct pipe_context* pipe,
120 struct pipe_surface* dst,
121 unsigned dstx, unsigned dsty,
122 struct pipe_surface* src,
123 unsigned srcx, unsigned srcy,
124 unsigned width, unsigned height)
125 {
126 enum pipe_format old_format = dst->texture->format;
127 enum pipe_format new_format = old_format;
128
129 if (dst->texture->format != src->texture->format) {
130 debug_printf("r300: Implementation error: Format mismatch in %s\n"
131 " : src: %s dst: %s\n", __FUNCTION__,
132 util_format_name(src->texture->format),
133 util_format_name(dst->texture->format));
134 debug_assert(0);
135 }
136
137 if (!pipe->screen->is_format_supported(pipe->screen,
138 old_format, src->texture->target,
139 PIPE_BIND_RENDER_TARGET |
140 PIPE_BIND_SAMPLER_VIEW, 0)) {
141 switch (util_format_get_blocksize(old_format)) {
142 case 1:
143 new_format = PIPE_FORMAT_I8_UNORM;
144 break;
145 case 2:
146 new_format = PIPE_FORMAT_B4G4R4A4_UNORM;
147 break;
148 case 4:
149 new_format = PIPE_FORMAT_B8G8R8A8_UNORM;
150 break;
151 case 8:
152 new_format = PIPE_FORMAT_R16G16B16A16_UNORM;
153 break;
154 default:
155 debug_printf("r300: surface_copy: Unhandled format: %s. Falling back to software.\n"
156 "r300: surface_copy: Software fallback doesn't work for tiled textures.\n",
157 util_format_name(old_format));
158 }
159 }
160
161 if (old_format != new_format) {
162 dst->format = new_format;
163 src->format = new_format;
164
165 r300_texture_reinterpret_format(pipe->screen,
166 dst->texture, new_format);
167 r300_texture_reinterpret_format(pipe->screen,
168 src->texture, new_format);
169 }
170
171 r300_hw_copy(pipe, dst, dstx, dsty, src, srcx, srcy, width, height);
172
173 if (old_format != new_format) {
174 dst->format = old_format;
175 src->format = old_format;
176
177 r300_texture_reinterpret_format(pipe->screen,
178 dst->texture, old_format);
179 r300_texture_reinterpret_format(pipe->screen,
180 src->texture, old_format);
181 }
182 }
183
184 /* Fill a region of a surface with a constant value. */
185 void r300_surface_fill(struct pipe_context* pipe,
186 struct pipe_surface* dst,
187 unsigned dstx, unsigned dsty,
188 unsigned width, unsigned height,
189 unsigned value)
190 {
191 struct r300_context* r300 = r300_context(pipe);
192
193 r300_blitter_save_states(r300);
194 util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);
195
196 util_blitter_fill(r300->blitter,
197 dst, dstx, dsty, width, height, value);
198 }