Merge branch '7.8' into master
[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_class.h"
31 #include "nouveau_util.h"
32 #include "nv10_driver.h"
33
34 static inline unsigned
35 get_rt_format(gl_format format)
36 {
37 switch (format) {
38 case MESA_FORMAT_XRGB8888:
39 return 0x05;
40 case MESA_FORMAT_ARGB8888:
41 return 0x08;
42 case MESA_FORMAT_RGB565:
43 return 0x03;
44 case MESA_FORMAT_Z16:
45 return 0x10;
46 case MESA_FORMAT_Z24_S8:
47 return 0x0;
48 default:
49 assert(0);
50 }
51 }
52
53 static void
54 setup_lma_buffer(GLcontext *ctx)
55 {
56 struct nouveau_channel *chan = context_chan(ctx);
57 struct nouveau_grobj *celsius = context_eng3d(ctx);
58 struct nouveau_bo_context *bctx = context_bctx(ctx, LMA_DEPTH);
59 struct gl_framebuffer *fb = ctx->DrawBuffer;
60 struct nouveau_framebuffer *nfb = to_nouveau_framebuffer(fb);
61 unsigned pitch = align(fb->Width, 128),
62 height = align(fb->Height, 2),
63 size = pitch * height;
64
65 if (!nfb->lma_bo || nfb->lma_bo->size != size) {
66 nouveau_bo_ref(NULL, &nfb->lma_bo);
67 nouveau_bo_new(context_dev(ctx), NOUVEAU_BO_VRAM, 0, size,
68 &nfb->lma_bo);
69 }
70
71 nouveau_bo_markl(bctx, celsius, NV17TCL_LMA_DEPTH_BUFFER_OFFSET,
72 nfb->lma_bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR);
73
74 BEGIN_RING(chan, celsius, NV17TCL_LMA_DEPTH_WINDOW_X, 4);
75 OUT_RINGf(chan, - 1792);
76 OUT_RINGf(chan, - 2304 + fb->Height);
77 OUT_RINGf(chan, fb->_DepthMaxF / 2);
78 OUT_RINGf(chan, 0);
79
80 BEGIN_RING(chan, celsius, NV17TCL_LMA_DEPTH_BUFFER_PITCH, 1);
81 OUT_RING(chan, pitch);
82
83 BEGIN_RING(chan, celsius, NV17TCL_LMA_DEPTH_ENABLE, 1);
84 OUT_RING(chan, 1);
85 }
86
87 void
88 nv10_emit_framebuffer(GLcontext *ctx, int emit)
89 {
90 struct nouveau_channel *chan = context_chan(ctx);
91 struct nouveau_grobj *celsius = context_eng3d(ctx);
92 struct nouveau_bo_context *bctx = context_bctx(ctx, FRAMEBUFFER);
93 struct gl_framebuffer *fb = ctx->DrawBuffer;
94 struct nouveau_surface *s;
95 unsigned rt_format = NV10TCL_RT_FORMAT_TYPE_LINEAR;
96 unsigned rt_pitch = 0, zeta_pitch = 0;
97 unsigned bo_flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR;
98
99 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT)
100 return;
101
102 /* At least nv11 seems to get sad if we don't do this before
103 * swapping RTs.*/
104 if (context_chipset(ctx) < 0x17) {
105 int i;
106
107 for (i = 0; i < 6; i++) {
108 BEGIN_RING(chan, celsius, NV10TCL_NOP, 1);
109 OUT_RING(chan, 0);
110 }
111 }
112
113 /* Render target */
114 if (fb->_ColorDrawBuffers[0]) {
115 s = &to_nouveau_renderbuffer(
116 fb->_ColorDrawBuffers[0])->surface;
117
118 rt_format |= get_rt_format(s->format);
119 zeta_pitch = rt_pitch = s->pitch;
120
121 nouveau_bo_markl(bctx, celsius, NV10TCL_COLOR_OFFSET,
122 s->bo, 0, bo_flags);
123 }
124
125 /* depth/stencil */
126 if (fb->_DepthBuffer) {
127 s = &to_nouveau_renderbuffer(
128 fb->_DepthBuffer->Wrapped)->surface;
129
130 rt_format |= get_rt_format(s->format);
131 zeta_pitch = s->pitch;
132
133 nouveau_bo_markl(bctx, celsius, NV10TCL_ZETA_OFFSET,
134 s->bo, 0, bo_flags);
135
136 if (context_chipset(ctx) >= 0x17)
137 setup_lma_buffer(ctx);
138 }
139
140 BEGIN_RING(chan, celsius, NV10TCL_RT_FORMAT, 2);
141 OUT_RING(chan, rt_format);
142 OUT_RING(chan, zeta_pitch << 16 | rt_pitch);
143
144 context_dirty(ctx, VIEWPORT);
145 context_dirty(ctx, SCISSOR);
146 }
147
148 void
149 nv10_emit_render_mode(GLcontext *ctx, int emit)
150 {
151 }
152
153 void
154 nv10_emit_scissor(GLcontext *ctx, int emit)
155 {
156 struct nouveau_channel *chan = context_chan(ctx);
157 struct nouveau_grobj *celsius = context_eng3d(ctx);
158 int x, y, w, h;
159
160 get_scissors(ctx->DrawBuffer, &x, &y, &w, &h);
161
162 BEGIN_RING(chan, celsius, NV10TCL_RT_HORIZ, 2);
163 OUT_RING(chan, w << 16 | x);
164 OUT_RING(chan, h << 16 | y);
165 }
166
167 void
168 nv10_emit_viewport(GLcontext *ctx, int emit)
169 {
170 struct nouveau_channel *chan = context_chan(ctx);
171 struct nouveau_grobj *celsius = context_eng3d(ctx);
172 struct gl_framebuffer *fb = ctx->DrawBuffer;
173 float a[4] = {};
174 int i;
175
176 get_viewport_translate(ctx, a);
177 a[0] -= 2048;
178 a[1] -= 2048;
179
180 BEGIN_RING(chan, celsius, NV10TCL_VIEWPORT_TRANSLATE_X, 4);
181 for (i = 0; i < 4; i++)
182 OUT_RINGf(chan, a[i]);
183
184 BEGIN_RING(chan, celsius, NV10TCL_VIEWPORT_CLIP_HORIZ(0), 1);
185 OUT_RING(chan, (fb->Width - 1) << 16 | 0x08000800);
186 BEGIN_RING(chan, celsius, NV10TCL_VIEWPORT_CLIP_VERT(0), 1);
187 OUT_RING(chan, (fb->Height - 1) << 16 | 0x08000800);
188
189 context_dirty(ctx, PROJECTION);
190 }