Merge branch 'lp-offset-twoside'
[mesa.git] / src / mesa / drivers / dri / nouveau / nv10_state_fb.c
1 /*
2 * Copyright (C) 2009 Francisco Jerez.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26
27 #include "nouveau_driver.h"
28 #include "nouveau_context.h"
29 #include "nouveau_fbo.h"
30 #include "nouveau_util.h"
31 #include "nv_object.xml.h"
32 #include "nv10_3d.xml.h"
33 #include "nv10_driver.h"
34
35 static inline unsigned
36 get_rt_format(gl_format format)
37 {
38 switch (format) {
39 case MESA_FORMAT_XRGB8888:
40 return NV10_3D_RT_FORMAT_COLOR_X8R8G8B8;
41 case MESA_FORMAT_ARGB8888:
42 return NV10_3D_RT_FORMAT_COLOR_A8R8G8B8;
43 case MESA_FORMAT_RGB565:
44 return NV10_3D_RT_FORMAT_COLOR_R5G6B5;
45 case MESA_FORMAT_Z16:
46 return NV10_3D_RT_FORMAT_DEPTH_Z16;
47 case MESA_FORMAT_Z24_S8:
48 return NV10_3D_RT_FORMAT_DEPTH_Z24S8;
49 default:
50 assert(0);
51 }
52 }
53
54 static void
55 setup_hierz_buffer(struct gl_context *ctx)
56 {
57 struct nouveau_channel *chan = context_chan(ctx);
58 struct nouveau_grobj *celsius = context_eng3d(ctx);
59 struct nouveau_bo_context *bctx = context_bctx(ctx, HIERZ);
60 struct gl_framebuffer *fb = ctx->DrawBuffer;
61 struct nouveau_framebuffer *nfb = to_nouveau_framebuffer(fb);
62 unsigned pitch = align(fb->Width, 128),
63 height = align(fb->Height, 2),
64 size = pitch * height;
65
66 if (!nfb->hierz.bo || nfb->hierz.bo->size != size) {
67 nouveau_bo_ref(NULL, &nfb->hierz.bo);
68 nouveau_bo_new_tile(context_dev(ctx), NOUVEAU_BO_VRAM, 0, size,
69 0, NOUVEAU_BO_TILE_ZETA, &nfb->hierz.bo);
70 }
71
72 nouveau_bo_markl(bctx, celsius, NV17_3D_HIERZ_OFFSET,
73 nfb->hierz.bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR);
74
75 WAIT_RING(chan, 9);
76 BEGIN_RING(chan, celsius, NV17_3D_HIERZ_WINDOW_X, 4);
77 OUT_RINGf(chan, - 1792);
78 OUT_RINGf(chan, - 2304 + fb->Height);
79 OUT_RINGf(chan, fb->_DepthMaxF / 2);
80 OUT_RINGf(chan, 0);
81
82 BEGIN_RING(chan, celsius, NV17_3D_HIERZ_PITCH, 1);
83 OUT_RING(chan, pitch);
84
85 BEGIN_RING(chan, celsius, NV17_3D_HIERZ_ENABLE, 1);
86 OUT_RING(chan, 1);
87 }
88
89 void
90 nv10_emit_framebuffer(struct gl_context *ctx, int emit)
91 {
92 struct nouveau_channel *chan = context_chan(ctx);
93 struct nouveau_grobj *celsius = context_eng3d(ctx);
94 struct nouveau_bo_context *bctx = context_bctx(ctx, FRAMEBUFFER);
95 struct gl_framebuffer *fb = ctx->DrawBuffer;
96 struct nouveau_surface *s;
97 unsigned rt_format = NV10_3D_RT_FORMAT_TYPE_LINEAR;
98 unsigned rt_pitch = 0, zeta_pitch = 0;
99 unsigned bo_flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR;
100
101 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT)
102 return;
103
104 /* At least nv11 seems to get sad if we don't do this before
105 * swapping RTs.*/
106 if (context_chipset(ctx) < 0x17) {
107 int i;
108
109 for (i = 0; i < 6; i++) {
110 BEGIN_RING(chan, celsius, NV04_GRAPH_NOP, 1);
111 OUT_RING(chan, 0);
112 }
113 }
114
115 /* Render target */
116 if (fb->_ColorDrawBuffers[0]) {
117 s = &to_nouveau_renderbuffer(
118 fb->_ColorDrawBuffers[0])->surface;
119
120 rt_format |= get_rt_format(s->format);
121 zeta_pitch = rt_pitch = s->pitch;
122
123 nouveau_bo_markl(bctx, celsius, NV10_3D_COLOR_OFFSET,
124 s->bo, 0, bo_flags);
125 }
126
127 /* depth/stencil */
128 if (fb->_DepthBuffer) {
129 s = &to_nouveau_renderbuffer(
130 fb->_DepthBuffer->Wrapped)->surface;
131
132 rt_format |= get_rt_format(s->format);
133 zeta_pitch = s->pitch;
134
135 nouveau_bo_markl(bctx, celsius, NV10_3D_ZETA_OFFSET,
136 s->bo, 0, bo_flags);
137
138 if (context_chipset(ctx) >= 0x17) {
139 setup_hierz_buffer(ctx);
140 context_dirty(ctx, ZCLEAR);
141 }
142 }
143
144 BEGIN_RING(chan, celsius, NV10_3D_RT_FORMAT, 2);
145 OUT_RING(chan, rt_format);
146 OUT_RING(chan, zeta_pitch << 16 | rt_pitch);
147
148 context_dirty(ctx, VIEWPORT);
149 context_dirty(ctx, SCISSOR);
150 }
151
152 void
153 nv10_emit_render_mode(struct gl_context *ctx, int emit)
154 {
155 }
156
157 void
158 nv10_emit_scissor(struct gl_context *ctx, int emit)
159 {
160 struct nouveau_channel *chan = context_chan(ctx);
161 struct nouveau_grobj *celsius = context_eng3d(ctx);
162 int x, y, w, h;
163
164 get_scissors(ctx->DrawBuffer, &x, &y, &w, &h);
165
166 BEGIN_RING(chan, celsius, NV10_3D_RT_HORIZ, 2);
167 OUT_RING(chan, w << 16 | x);
168 OUT_RING(chan, h << 16 | y);
169 }
170
171 void
172 nv10_emit_viewport(struct gl_context *ctx, int emit)
173 {
174 struct nouveau_channel *chan = context_chan(ctx);
175 struct nouveau_grobj *celsius = context_eng3d(ctx);
176 struct gl_viewport_attrib *vp = &ctx->Viewport;
177 struct gl_framebuffer *fb = ctx->DrawBuffer;
178 float a[4] = {};
179
180 get_viewport_translate(ctx, a);
181 a[0] -= 2048;
182 a[1] -= 2048;
183 if (nv10_use_viewport_zclear(ctx))
184 a[2] = nv10_transform_depth(ctx, (vp->Far + vp->Near) / 2);
185
186 BEGIN_RING(chan, celsius, NV10_3D_VIEWPORT_TRANSLATE_X, 4);
187 OUT_RINGp(chan, a, 4);
188
189 BEGIN_RING(chan, celsius, NV10_3D_VIEWPORT_CLIP_HORIZ(0), 1);
190 OUT_RING(chan, (fb->Width - 1) << 16 | 0x08000800);
191 BEGIN_RING(chan, celsius, NV10_3D_VIEWPORT_CLIP_VERT(0), 1);
192 OUT_RING(chan, (fb->Height - 1) << 16 | 0x08000800);
193
194 context_dirty(ctx, PROJECTION);
195 }
196
197 void
198 nv10_emit_zclear(struct gl_context *ctx, int emit)
199 {
200 struct nouveau_context *nctx = to_nouveau_context(ctx);
201 struct nouveau_channel *chan = context_chan(ctx);
202 struct nouveau_grobj *celsius = context_eng3d(ctx);
203 struct nouveau_framebuffer *nfb =
204 to_nouveau_framebuffer(ctx->DrawBuffer);
205
206 if (nfb->hierz.bo) {
207 BEGIN_RING(chan, celsius, NV17_3D_ZCLEAR_ENABLE, 2);
208 OUT_RINGb(chan, !nctx->hierz.clear_blocked);
209 OUT_RING(chan, nfb->hierz.clear_value |
210 (nctx->hierz.clear_seq & 0xff));
211 } else {
212 BEGIN_RING(chan, celsius, NV10_3D_DEPTH_RANGE_NEAR, 2);
213 OUT_RINGf(chan, nv10_transform_depth(ctx, 0));
214 OUT_RINGf(chan, nv10_transform_depth(ctx, 1));
215 context_dirty(ctx, VIEWPORT);
216 }
217 }