2 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
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:
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
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. */
23 #include "r300_context.h"
24 #include "r300_texture.h"
26 #include "util/u_format.h"
28 static void r300_blitter_save_states(struct r300_context* r300)
30 util_blitter_save_blend(r300->blitter, r300->blend_state.state);
31 util_blitter_save_depth_stencil_alpha(r300->blitter, r300->dsa_state.state);
32 util_blitter_save_stencil_ref(r300->blitter, &(r300->stencil_ref));
33 util_blitter_save_rasterizer(r300->blitter, r300->rs_state.state);
34 util_blitter_save_fragment_shader(r300->blitter, r300->fs.state);
35 util_blitter_save_vertex_shader(r300->blitter, r300->vs_state.state);
36 util_blitter_save_viewport(r300->blitter, &r300->viewport);
37 util_blitter_save_clip(r300->blitter, &r300->clip);
38 util_blitter_save_vertex_elements(r300->blitter, r300->velems);
39 /* XXX this crashes the driver
40 util_blitter_save_vertex_buffers(r300->blitter, r300->vertex_buffer_count,
41 r300->vertex_buffer); */
44 /* Clear currently bound buffers. */
45 static void r300_clear(struct pipe_context* pipe,
51 /* XXX Implement fastfill.
53 * If fastfill is enabled, a few facts should be considered:
55 * 1) Zbuffer must be micro-tiled and whole microtiles must be
58 * 2) ZB_DEPTHCLEARVALUE is used to clear a zbuffer and Z Mask must be
61 * 3) For 16-bit integer buffering, compression causes a hung with one or
62 * two samples and should not be used.
64 * 4) Fastfill must not be used if reading of compressed Z data is disabled
65 * and writing of compressed Z data is enabled (RD/WR_COMP_ENABLE),
66 * i.e. it cannot be used to compress the zbuffer.
67 * (what the hell does that mean and how does it fit in clearing
73 struct r300_context* r300 = r300_context(pipe);
74 struct pipe_framebuffer_state* fb =
75 (struct pipe_framebuffer_state*)r300->fb_state.state;
77 r300_blitter_save_states(r300);
79 util_blitter_clear(r300->blitter,
83 buffers, rgba, depth, stencil);
86 /* Copy a block of pixels from one surface to another using HW. */
87 static void r300_hw_copy_region(struct pipe_context* pipe,
88 struct pipe_resource *dst,
89 struct pipe_subresource subdst,
90 unsigned dstx, unsigned dsty, unsigned dstz,
91 struct pipe_resource *src,
92 struct pipe_subresource subsrc,
93 unsigned srcx, unsigned srcy, unsigned srcz,
94 unsigned width, unsigned height)
96 struct r300_context* r300 = r300_context(pipe);
97 struct r300_textures_state* state =
98 (struct r300_textures_state*)r300->textures_state.state;
100 /* Yeah we have to save all those states to ensure this blitter operation
101 * is really transparent. The states will be restored by the blitter once
102 * copying is done. */
103 r300_blitter_save_states(r300);
104 util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);
106 util_blitter_save_fragment_sampler_states(
107 r300->blitter, state->sampler_state_count,
108 (void**)state->sampler_states);
110 util_blitter_save_fragment_sampler_views(
111 r300->blitter, state->sampler_view_count,
112 (struct pipe_sampler_view**)state->sampler_views);
115 util_blitter_copy_region(r300->blitter, dst, subdst, dstx, dsty, dstz,
116 src, subsrc, srcx, srcy, srcz, width, height,
120 /* Copy a block of pixels from one surface to another. */
121 static void r300_resource_copy_region(struct pipe_context *pipe,
122 struct pipe_resource *dst,
123 struct pipe_subresource subdst,
124 unsigned dstx, unsigned dsty, unsigned dstz,
125 struct pipe_resource *src,
126 struct pipe_subresource subsrc,
127 unsigned srcx, unsigned srcy, unsigned srcz,
128 unsigned width, unsigned height)
130 enum pipe_format old_format = dst->format;
131 enum pipe_format new_format = old_format;
133 if (dst->format != src->format) {
134 debug_printf("r300: Implementation error: Format mismatch in %s\n"
135 " : src: %s dst: %s\n", __FUNCTION__,
136 util_format_short_name(src->format),
137 util_format_short_name(dst->format));
141 if (!pipe->screen->is_format_supported(pipe->screen,
142 old_format, src->target,
144 PIPE_BIND_RENDER_TARGET |
145 PIPE_BIND_SAMPLER_VIEW, 0) &&
146 util_format_is_plain(old_format)) {
147 switch (util_format_get_blocksize(old_format)) {
149 new_format = PIPE_FORMAT_I8_UNORM;
152 new_format = PIPE_FORMAT_B4G4R4A4_UNORM;
155 new_format = PIPE_FORMAT_B8G8R8A8_UNORM;
158 new_format = PIPE_FORMAT_R16G16B16A16_UNORM;
161 debug_printf("r300: surface_copy: Unhandled format: %s. Falling back to software.\n"
162 "r300: surface_copy: Software fallback doesn't work for tiled textures.\n",
163 util_format_short_name(old_format));
167 if (old_format != new_format) {
168 dst->format = new_format;
169 src->format = new_format;
171 r300_texture_reinterpret_format(pipe->screen,
173 r300_texture_reinterpret_format(pipe->screen,
177 r300_hw_copy_region(pipe, dst, subdst, dstx, dsty, dstz,
178 src, subsrc, srcx, srcy, srcz, width, height);
180 if (old_format != new_format) {
181 dst->format = old_format;
182 src->format = old_format;
184 r300_texture_reinterpret_format(pipe->screen,
186 r300_texture_reinterpret_format(pipe->screen,
191 /* Clear a region of a color surface to a constant value. */
192 static void r300_clear_render_target(struct pipe_context *pipe,
193 struct pipe_surface *dst,
195 unsigned dstx, unsigned dsty,
196 unsigned width, unsigned height)
198 struct r300_context *r300 = r300_context(pipe);
200 r300_blitter_save_states(r300);
201 util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);
203 util_blitter_clear_render_target(r300->blitter, dst, rgba,
204 dstx, dsty, width, height);
207 /* Clear a region of a depth stencil surface. */
208 static void r300_clear_depth_stencil(struct pipe_context *pipe,
209 struct pipe_surface *dst,
210 unsigned clear_flags,
213 unsigned dstx, unsigned dsty,
214 unsigned width, unsigned height)
216 struct r300_context *r300 = r300_context(pipe);
218 r300_blitter_save_states(r300);
219 util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);
221 util_blitter_clear_depth_stencil(r300->blitter, dst, clear_flags, depth, stencil,
222 dstx, dsty, width, height);
225 void r300_init_blit_functions(struct r300_context *r300)
227 r300->context.clear = r300_clear;
228 r300->context.clear_render_target = r300_clear_render_target;
229 r300->context.clear_depth_stencil = r300_clear_depth_stencil;
230 r300->context.resource_copy_region = r300_resource_copy_region;