Debian Codegraph @

src/gallium/drivers/nouveau/nv30/nv30_clear.c

raw
1 /*
2 * Copyright 2012 Red Hat Inc.
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Ben Skeggs
23 *
24 */
25
26 #include "pipe/p_defines.h"
27 #include "util/u_pack_color.h"
28
29 #include "nouveau_gldefs.h"
30 #include "nv_object.xml.h"
31 #include "nv30/nv30-40_3d.xml.h"
32 #include "nv30/nv30_context.h"
33 #include "nv30/nv30_winsys.h"
34 #include "nv30/nv30_format.h"
35
36 static inline uint32_t
37 pack_rgba(enum pipe_format format, const float *rgba)
38 {
39 union util_color uc;
40 util_pack_color(rgba, format, &uc);
41 return uc.ui[0];
42 }
43
44 static inline uint32_t
45 pack_zeta(enum pipe_format format, double depth, unsigned stencil)
46 {
47 uint32_t zuint = (uint32_t)(depth * 4294967295.0);
48 if (format != PIPE_FORMAT_Z16_UNORM)
49 return (zuint & 0xffffff00) | (stencil & 0xff);
50 return zuint >> 16;
51 }
52
53 static void
54 nv30_clear(struct pipe_context *pipe, unsigned buffers,
55 uint32_t color_clear_mask, uint8_t stencil_clear_mask,
56 const struct pipe_scissor_state *scissor_state,
57 const union pipe_color_union *color, double depth, unsigned stencil)
58 {
59 struct nv30_context *nv30 = nv30_context(pipe);
60 struct nouveau_pushbuf *push = nv30->base.pushbuf;
61 struct pipe_framebuffer_state *fb = &nv30->framebuffer;
62 uint32_t colr = 0, zeta = 0, mode = 0;
63
64 if (!nv30_state_validate(nv30, NV30_NEW_FRAMEBUFFER, true))
65 return;
66
67 if (scissor_state) {
68 uint32_t minx = scissor_state->minx;
69 uint32_t maxx = MIN2(fb->width, scissor_state->maxx);
70 uint32_t miny = scissor_state->miny;
71 uint32_t maxy = MIN2(fb->height, scissor_state->maxy);
72
73 BEGIN_NV04(push, NV30_3D(SCISSOR_HORIZ), 2);
74 PUSH_DATA (push, minx | (maxx - minx) << 16);
75 PUSH_DATA (push, miny | (maxy - miny) << 16);
76 }
77 else {
78 BEGIN_NV04(push, NV30_3D(SCISSOR_HORIZ), 2);
79 PUSH_DATA (push, 0x10000000);
80 PUSH_DATA (push, 0x10000000);
81 }
82
83 if (buffers & PIPE_CLEAR_COLOR && fb->nr_cbufs) {
84 colr = pack_rgba(fb->cbufs[0].format, color->f);
85 mode |= NV30_3D_CLEAR_BUFFERS_COLOR_R |
86 NV30_3D_CLEAR_BUFFERS_COLOR_G |
87 NV30_3D_CLEAR_BUFFERS_COLOR_B |
88 NV30_3D_CLEAR_BUFFERS_COLOR_A;
89 }
90
91 if (fb->zsbuf.texture) {
92 zeta = pack_zeta(fb->zsbuf.format, depth, stencil);
93 if (buffers & PIPE_CLEAR_DEPTH)
94 mode |= NV30_3D_CLEAR_BUFFERS_DEPTH;
95 if (buffers & PIPE_CLEAR_STENCIL) {
96 mode |= NV30_3D_CLEAR_BUFFERS_STENCIL;
97 BEGIN_NV04(push, NV30_3D(STENCIL_ENABLE(0)), 2);
98 PUSH_DATA (push, 0);
99 PUSH_DATA (push, 0x000000ff);
100 nv30->dirty |= NV30_NEW_ZSA;
101 }
102 }
103
104 /*XXX: wtf? fixes clears sometimes not clearing on nv3x... */
105 if (nv30->screen->eng3d->oclass < NV40_3D_CLASS) {
106 BEGIN_NV04(push, NV30_3D(CLEAR_DEPTH_VALUE), 3);
107 PUSH_DATA (push, zeta);
108 PUSH_DATA (push, colr);
109 PUSH_DATA (push, mode);
110 }
111
112 BEGIN_NV04(push, NV30_3D(CLEAR_DEPTH_VALUE), 3);
113 PUSH_DATA (push, zeta);
114 PUSH_DATA (push, colr);
115 PUSH_DATA (push, mode);
116
117 nv30_state_release(nv30);
118
119 /* Make sure regular draw commands will get their scissor state set */
120 nv30->dirty |= NV30_NEW_SCISSOR;
121 nv30->state.scissor_off = 0;
122 }
123
124 static void
125 nv30_clear_render_target(struct pipe_context *pipe, struct pipe_surface *ps,
126 const union pipe_color_union *color,
127 unsigned x, unsigned y, unsigned w, unsigned h,
128 bool render_condition_enabled)
129 {
130 struct nv30_context *nv30 = nv30_context(pipe);
131 struct nv30_surface *sf = nv30_surface(ps);
132 struct nv30_miptree *mt = nv30_miptree(ps->texture);
133 struct nouveau_pushbuf *push = nv30->base.pushbuf;
134 struct nouveau_object *eng3d = nv30->screen->eng3d;
135 uint32_t rt_format;
136
137 rt_format = nv30_format(pipe->screen, ps->format)->hw;
138 if (util_format_get_blocksize(ps->format) == 4)
139 rt_format |= NV30_3D_RT_FORMAT_ZETA_Z24S8;
140 else
141 rt_format |= NV30_3D_RT_FORMAT_ZETA_Z16;
142
143 if (nv30_miptree(ps->texture)->swizzled) {
144 rt_format |= NV30_3D_RT_FORMAT_TYPE_SWIZZLED;
145 rt_format |= util_logbase2(sf->width) << 16;
146 rt_format |= util_logbase2(sf->height) << 24;
147 } else {
148 rt_format |= NV30_3D_RT_FORMAT_TYPE_LINEAR;
149 }
150
151 if (!PUSH_SPACE_EX(push, 32, 1, 0) ||
152 PUSH_REF1(push, mt->base.bo, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR))
153 return;
154
155 BEGIN_NV04(push, NV30_3D(RT_ENABLE), 1);
156 PUSH_DATA (push, NV30_3D_RT_ENABLE_COLOR0);
157 BEGIN_NV04(push, NV30_3D(RT_HORIZ), 3);
158 PUSH_DATA (push, sf->width << 16);
159 PUSH_DATA (push, sf->height << 16);
160 PUSH_DATA (push, rt_format);
161 BEGIN_NV04(push, NV30_3D(COLOR0_PITCH), 2);
162 if (eng3d->oclass < NV40_3D_CLASS)
163 PUSH_DATA (push, (sf->pitch << 16) | sf->pitch);
164 else
165 PUSH_DATA (push, sf->pitch);
166 PUSH_RELOC(push, mt->base.bo, sf->offset, NOUVEAU_BO_LOW, 0, 0);
167 BEGIN_NV04(push, NV30_3D(SCISSOR_HORIZ), 2);
168 PUSH_DATA (push, (w << 16) | x);
169 PUSH_DATA (push, (h << 16) | y);
170
171 BEGIN_NV04(push, NV30_3D(CLEAR_COLOR_VALUE), 2);
172 PUSH_DATA (push, pack_rgba(ps->format, color->f));
173 PUSH_DATA (push, NV30_3D_CLEAR_BUFFERS_COLOR_R |
174 NV30_3D_CLEAR_BUFFERS_COLOR_G |
175 NV30_3D_CLEAR_BUFFERS_COLOR_B |
176 NV30_3D_CLEAR_BUFFERS_COLOR_A);
177
178 nv30->dirty |= NV30_NEW_FRAMEBUFFER | NV30_NEW_SCISSOR;
179 nv30->state.scissor_off = 0;
180 }
181
182 static void
183 nv30_clear_depth_stencil(struct pipe_context *pipe, struct pipe_surface *ps,
184 unsigned buffers, double depth, unsigned stencil,
185 unsigned x, unsigned y, unsigned w, unsigned h,
186 bool render_condition_enabled)
187 {
188 struct nv30_context *nv30 = nv30_context(pipe);
189 struct nv30_surface *sf = nv30_surface(ps);
190 struct nv30_miptree *mt = nv30_miptree(ps->texture);
191 struct nouveau_pushbuf *push = nv30->base.pushbuf;
192 struct nouveau_object *eng3d = nv30->screen->eng3d;
193 uint32_t rt_format, mode = 0;
194
195 rt_format = nv30_format(pipe->screen, ps->format)->hw;
196 if (util_format_get_blocksize(ps->format) == 4)
197 rt_format |= NV30_3D_RT_FORMAT_COLOR_A8R8G8B8;
198 else
199 rt_format |= NV30_3D_RT_FORMAT_COLOR_R5G6B5;
200
201 if (nv30_miptree(ps->texture)->swizzled) {
202 rt_format |= NV30_3D_RT_FORMAT_TYPE_SWIZZLED;
203 rt_format |= util_logbase2(sf->width) << 16;
204 rt_format |= util_logbase2(sf->height) << 24;
205 } else {
206 rt_format |= NV30_3D_RT_FORMAT_TYPE_LINEAR;
207 }
208
209 if (buffers & PIPE_CLEAR_DEPTH)
210 mode |= NV30_3D_CLEAR_BUFFERS_DEPTH;
211 if (buffers & PIPE_CLEAR_STENCIL)
212 mode |= NV30_3D_CLEAR_BUFFERS_STENCIL;
213
214 if (!PUSH_SPACE_EX(push, 32, 1, 0) ||
215 PUSH_REF1(push, mt->base.bo, NOUVEAU_BO_VRAM | NOUVEAU_BO_WR))
216 return;
217
218 BEGIN_NV04(push, NV30_3D(RT_ENABLE), 1);
219 PUSH_DATA (push, 0);
220 BEGIN_NV04(push, NV30_3D(RT_HORIZ), 3);
221 PUSH_DATA (push, sf->width << 16);
222 PUSH_DATA (push, sf->height << 16);
223 PUSH_DATA (push, rt_format);
224 if (eng3d->oclass < NV40_3D_CLASS) {
225 BEGIN_NV04(push, NV30_3D(COLOR0_PITCH), 1);
226 PUSH_DATA (push, (sf->pitch << 16) | sf->pitch);
227 } else {
228 BEGIN_NV04(push, NV40_3D(ZETA_PITCH), 1);
229 PUSH_DATA (push, sf->pitch);
230 }
231 BEGIN_NV04(push, NV30_3D(ZETA_OFFSET), 1);
232 PUSH_RELOC(push, mt->base.bo, sf->offset, NOUVEAU_BO_LOW, 0, 0);
233 BEGIN_NV04(push, NV30_3D(SCISSOR_HORIZ), 2);
234 PUSH_DATA (push, (w << 16) | x);
235 PUSH_DATA (push, (h << 16) | y);
236
237 BEGIN_NV04(push, NV30_3D(CLEAR_DEPTH_VALUE), 1);
238 PUSH_DATA (push, pack_zeta(ps->format, depth, stencil));
239 BEGIN_NV04(push, NV30_3D(CLEAR_BUFFERS), 1);
240 PUSH_DATA (push, mode);
241
242 nv30->dirty |= NV30_NEW_FRAMEBUFFER | NV30_NEW_SCISSOR;
243 nv30->state.scissor_off = 0;
244 }
245
246 void
247 nv30_clear_init(struct pipe_context *pipe)
248 {
249 pipe->clear = nv30_clear;
250 pipe->clear_render_target = nv30_clear_render_target;
251 pipe->clear_depth_stencil = nv30_clear_depth_stencil;
252 }