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_blit.h"
24 #include "r300_context.h"
25 #include "r300_texture.h"
27 #include "util/u_format.h"
29 static void r300_blitter_save_states(struct r300_context
* r300
)
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); */
45 /* Clear currently bound buffers. */
46 void r300_clear(struct pipe_context
* pipe
,
52 /* XXX Implement fastfill.
54 * If fastfill is enabled, a few facts should be considered:
56 * 1) Zbuffer must be micro-tiled and whole microtiles must be
59 * 2) ZB_DEPTHCLEARVALUE is used to clear a zbuffer and Z Mask must be
62 * 3) For 16-bit integer buffering, compression causes a hung with one or
63 * two samples and should not be used.
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
74 struct r300_context
* r300
= r300_context(pipe
);
75 struct pipe_framebuffer_state
* fb
=
76 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
78 r300_blitter_save_states(r300
);
80 util_blitter_clear(r300
->blitter
,
84 buffers
, rgba
, depth
, stencil
);
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
)
95 struct r300_context
* r300
= r300_context(pipe
);
96 struct r300_textures_state
* state
=
97 (struct r300_textures_state
*)r300
->textures_state
.state
;
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
);
105 util_blitter_save_fragment_sampler_states(
106 r300
->blitter
, state
->sampler_state_count
,
107 (void**)state
->sampler_states
);
109 util_blitter_save_fragment_sampler_views(
110 r300
->blitter
, state
->sampler_view_count
,
111 (struct pipe_sampler_view
**)state
->sampler_views
);
114 util_blitter_copy(r300
->blitter
,
115 dst
, dstx
, dsty
, src
, srcx
, srcy
, width
, height
, TRUE
);
118 /* Copy a block of pixels from one surface to another. */
119 void r300_surface_copy(struct pipe_context
* pipe
,
120 struct pipe_resource
* dst
,
121 struct pipe_subresource subdst
,
122 unsigned dstx
, unsigned dsty
, unsigned dstz
,
123 struct pipe_resource
* src
,
124 struct pipe_subresource subsrc
,
125 unsigned srcx
, unsigned srcy
, unsigned srcz
,
126 unsigned width
, unsigned height
)
128 struct pipe_screen
*screen
= pipe
->screen
;
129 enum pipe_format old_format
= dst
->format
;
130 enum pipe_format new_format
= old_format
;
131 struct pipe_surface
*srcsurf
, *dstsurf
;
134 if (util_format_is_depth_or_stencil(dst
->format
))
135 bind
= PIPE_BIND_DEPTH_STENCIL
;
137 bind
= PIPE_BIND_RENDER_TARGET
;
139 if (dst
->format
!= src
->format
) {
140 debug_printf("r300: Implementation error: Format mismatch in %s\n"
141 " : src: %s dst: %s\n", __FUNCTION__
,
142 util_format_short_name(src
->format
),
143 util_format_short_name(dst
->format
));
147 if (!pipe
->screen
->is_format_supported(pipe
->screen
,
148 old_format
, src
->target
,
150 PIPE_BIND_RENDER_TARGET
|
151 PIPE_BIND_SAMPLER_VIEW
, 0) &&
152 util_format_is_plain(old_format
)) {
153 switch (util_format_get_blocksize(old_format
)) {
155 new_format
= PIPE_FORMAT_I8_UNORM
;
158 new_format
= PIPE_FORMAT_B4G4R4A4_UNORM
;
161 new_format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
164 new_format
= PIPE_FORMAT_R16G16B16A16_UNORM
;
167 debug_printf("r300: surface_copy: Unhandled format: %s. Falling back to software.\n"
168 "r300: surface_copy: Software fallback doesn't work for tiled textures.\n",
169 util_format_short_name(old_format
));
173 if (old_format
!= new_format
) {
174 dst
->format
= new_format
;
175 src
->format
= new_format
;
177 r300_texture_reinterpret_format(pipe
->screen
,
179 r300_texture_reinterpret_format(pipe
->screen
,
183 srcsurf
= screen
->get_tex_surface(screen
, src
,
184 subsrc
.face
, subsrc
.level
, srcz
,
185 PIPE_BIND_SAMPLER_VIEW
);
187 dstsurf
= screen
->get_tex_surface(screen
, dst
,
188 subdst
.face
, subdst
.level
, dstz
,
191 r300_hw_copy(pipe
, dstsurf
, dstx
, dsty
, srcsurf
, srcx
, srcy
, width
, height
);
193 pipe_surface_reference(&srcsurf
, NULL
);
194 pipe_surface_reference(&dstsurf
, NULL
);
196 if (old_format
!= new_format
) {
197 dst
->format
= old_format
;
198 src
->format
= old_format
;
200 r300_texture_reinterpret_format(pipe
->screen
,
202 r300_texture_reinterpret_format(pipe
->screen
,
207 /* Fill a region of a surface with a constant value. */
208 void r300_surface_fill(struct pipe_context
* pipe
,
209 struct pipe_resource
* dst
,
210 struct pipe_subresource subdst
,
211 unsigned dstx
, unsigned dsty
, unsigned dstz
,
212 unsigned width
, unsigned height
,
215 struct pipe_screen
*screen
= pipe
->screen
;
216 struct r300_context
* r300
= r300_context(pipe
);
217 struct pipe_surface
*dstsurf
;
220 if (util_format_is_depth_or_stencil(dst
->format
))
221 bind
= PIPE_BIND_DEPTH_STENCIL
;
223 bind
= PIPE_BIND_RENDER_TARGET
;
225 dstsurf
= screen
->get_tex_surface(screen
, dst
,
231 r300_blitter_save_states(r300
);
232 util_blitter_save_framebuffer(r300
->blitter
, r300
->fb_state
.state
);
234 util_blitter_fill(r300
->blitter
,
235 dstsurf
, dstx
, dsty
, width
, height
, value
);
237 pipe_surface_reference(&dstsurf
, NULL
);