winsys/drm: Handle circular dependencies in Makefile.egl.
[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);
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 }
40
41 /* Clear currently bound buffers. */
42 void r300_clear(struct pipe_context* pipe,
43 unsigned buffers,
44 const float* rgba,
45 double depth,
46 unsigned stencil)
47 {
48 /* XXX Implement fastfill.
49 *
50 * If fastfill is enabled, a few facts should be considered:
51 *
52 * 1) Zbuffer must be micro-tiled and whole microtiles must be
53 * written.
54 *
55 * 2) ZB_DEPTHCLEARVALUE is used to clear a zbuffer and Z Mask must be
56 * equal to 0.
57 *
58 * 3) RB3D_COLOR_CLEAR_VALUE is used to clear a colorbuffer and
59 * RB3D_COLOR_CHANNEL_MASK must be equal to 0.
60 *
61 * 4) ZB_CB_CLEAR can be used to make the ZB units help in clearing
62 * the colorbuffer. The color clear value is supplied through both
63 * RB3D_COLOR_CLEAR_VALUE and ZB_DEPTHCLEARVALUE, and the colorbuffer
64 * must be set in ZB_DEPTHOFFSET and ZB_DEPTHPITCH in addition to
65 * RB3D_COLOROFFSET and RB3D_COLORPITCH. It's obvious that the zbuffer
66 * will not be cleared and multiple render targets cannot be cleared
67 * this way either.
68 *
69 * 5) For 16-bit integer buffering, compression causes a hung with one or
70 * two samples and should not be used.
71 *
72 * 6) Fastfill must not be used if reading of compressed Z data is disabled
73 * and writing of compressed Z data is enabled (RD/WR_COMP_ENABLE),
74 * i.e. it cannot be used to compress the zbuffer.
75 * (what the hell does that mean and how does it fit in clearing
76 * the buffers?)
77 *
78 * - Marek
79 */
80
81 struct r300_context* r300 = r300_context(pipe);
82 struct pipe_framebuffer_state* fb =
83 (struct pipe_framebuffer_state*)r300->fb_state.state;
84
85 r300_blitter_save_states(r300);
86
87 util_blitter_clear(r300->blitter,
88 fb->width,
89 fb->height,
90 fb->nr_cbufs,
91 buffers, rgba, depth, stencil);
92 }
93
94 /* Copy a block of pixels from one surface to another using HW. */
95 static void r300_hw_copy(struct pipe_context* pipe,
96 struct pipe_surface* dst,
97 unsigned dstx, unsigned dsty,
98 struct pipe_surface* src,
99 unsigned srcx, unsigned srcy,
100 unsigned width, unsigned height)
101 {
102 struct r300_context* r300 = r300_context(pipe);
103 struct r300_textures_state* state =
104 (struct r300_textures_state*)r300->textures_state.state;
105
106 /* Yeah we have to save all those states to ensure this blitter operation
107 * is really transparent. The states will be restored by the blitter once
108 * copying is done. */
109 r300_blitter_save_states(r300);
110 util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);
111
112 util_blitter_save_fragment_sampler_states(
113 r300->blitter, state->sampler_count, (void**)state->sampler_states);
114
115 util_blitter_save_fragment_sampler_textures(
116 r300->blitter, state->texture_count,
117 (struct pipe_texture**)state->textures);
118
119 /* Do a copy */
120 util_blitter_copy(r300->blitter,
121 dst, dstx, dsty, src, srcx, srcy, width, height, TRUE);
122 }
123
124 /* Copy a block of pixels from one surface to another. */
125 void r300_surface_copy(struct pipe_context* pipe,
126 struct pipe_surface* dst,
127 unsigned dstx, unsigned dsty,
128 struct pipe_surface* src,
129 unsigned srcx, unsigned srcy,
130 unsigned width, unsigned height)
131 {
132 enum pipe_format old_format = dst->texture->format;
133 enum pipe_format new_format = old_format;
134
135 assert(dst->texture->format == src->texture->format);
136
137 if (!pipe->screen->is_format_supported(pipe->screen,
138 old_format, src->texture->target,
139 PIPE_TEXTURE_USAGE_RENDER_TARGET |
140 PIPE_TEXTURE_USAGE_SAMPLER, 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 default:
152 debug_printf("r300: surface_copy: Unhandled format: %s. Falling back to software.\n"
153 "r300: surface_copy: Software fallback doesn't work for tiled textures.\n",
154 util_format_name(old_format));
155 }
156 }
157
158 if (old_format != new_format) {
159 dst->format = new_format;
160 src->format = new_format;
161
162 r300_texture_reinterpret_format(pipe->screen,
163 dst->texture, new_format);
164 r300_texture_reinterpret_format(pipe->screen,
165 src->texture, new_format);
166 }
167
168 r300_hw_copy(pipe, dst, dstx, dsty, src, srcx, srcy, width, height);
169
170 if (old_format != new_format) {
171 dst->format = old_format;
172 src->format = old_format;
173
174 r300_texture_reinterpret_format(pipe->screen,
175 dst->texture, old_format);
176 r300_texture_reinterpret_format(pipe->screen,
177 src->texture, old_format);
178 }
179 }
180
181 /* Fill a region of a surface with a constant value. */
182 void r300_surface_fill(struct pipe_context* pipe,
183 struct pipe_surface* dst,
184 unsigned dstx, unsigned dsty,
185 unsigned width, unsigned height,
186 unsigned value)
187 {
188 struct r300_context* r300 = r300_context(pipe);
189
190 r300_blitter_save_states(r300);
191 util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);
192
193 util_blitter_fill(r300->blitter,
194 dst, dstx, dsty, width, height, value);
195 }