Merge commit 'origin/7.8'
[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 WAIT_RING(chan, 9);
75 BEGIN_RING(chan, celsius, NV17TCL_LMA_DEPTH_WINDOW_X, 4);
76 OUT_RINGf(chan, - 1792);
77 OUT_RINGf(chan, - 2304 + fb->Height);
78 OUT_RINGf(chan, fb->_DepthMaxF / 2);
79 OUT_RINGf(chan, 0);
80
81 BEGIN_RING(chan, celsius, NV17TCL_LMA_DEPTH_BUFFER_PITCH, 1);
82 OUT_RING(chan, pitch);
83
84 BEGIN_RING(chan, celsius, NV17TCL_LMA_DEPTH_ENABLE, 1);
85 OUT_RING(chan, 1);
86 }
87
88 void
89 nv10_emit_framebuffer(GLcontext *ctx, int emit)
90 {
91 struct nouveau_channel *chan = context_chan(ctx);
92 struct nouveau_grobj *celsius = context_eng3d(ctx);
93 struct nouveau_bo_context *bctx = context_bctx(ctx, FRAMEBUFFER);
94 struct gl_framebuffer *fb = ctx->DrawBuffer;
95 struct nouveau_surface *s;
96 unsigned rt_format = NV10TCL_RT_FORMAT_TYPE_LINEAR;
97 unsigned rt_pitch = 0, zeta_pitch = 0;
98 unsigned bo_flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR;
99
100 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT)
101 return;
102
103 /* At least nv11 seems to get sad if we don't do this before
104 * swapping RTs.*/
105 if (context_chipset(ctx) < 0x17) {
106 int i;
107
108 for (i = 0; i < 6; i++) {
109 BEGIN_RING(chan, celsius, NV10TCL_NOP, 1);
110 OUT_RING(chan, 0);
111 }
112 }
113
114 /* Render target */
115 if (fb->_ColorDrawBuffers[0]) {
116 s = &to_nouveau_renderbuffer(
117 fb->_ColorDrawBuffers[0])->surface;
118
119 rt_format |= get_rt_format(s->format);
120 zeta_pitch = rt_pitch = s->pitch;
121
122 nouveau_bo_markl(bctx, celsius, NV10TCL_COLOR_OFFSET,
123 s->bo, 0, bo_flags);
124 }
125
126 /* depth/stencil */
127 if (fb->_DepthBuffer) {
128 s = &to_nouveau_renderbuffer(
129 fb->_DepthBuffer->Wrapped)->surface;
130
131 rt_format |= get_rt_format(s->format);
132 zeta_pitch = s->pitch;
133
134 nouveau_bo_markl(bctx, celsius, NV10TCL_ZETA_OFFSET,
135 s->bo, 0, bo_flags);
136
137 if (context_chipset(ctx) >= 0x17)
138 setup_lma_buffer(ctx);
139 }
140
141 BEGIN_RING(chan, celsius, NV10TCL_RT_FORMAT, 2);
142 OUT_RING(chan, rt_format);
143 OUT_RING(chan, zeta_pitch << 16 | rt_pitch);
144
145 context_dirty(ctx, VIEWPORT);
146 context_dirty(ctx, SCISSOR);
147 }
148
149 void
150 nv10_emit_render_mode(GLcontext *ctx, int emit)
151 {
152 }
153
154 void
155 nv10_emit_scissor(GLcontext *ctx, int emit)
156 {
157 struct nouveau_channel *chan = context_chan(ctx);
158 struct nouveau_grobj *celsius = context_eng3d(ctx);
159 int x, y, w, h;
160
161 get_scissors(ctx->DrawBuffer, &x, &y, &w, &h);
162
163 BEGIN_RING(chan, celsius, NV10TCL_RT_HORIZ, 2);
164 OUT_RING(chan, w << 16 | x);
165 OUT_RING(chan, h << 16 | y);
166 }
167
168 void
169 nv10_emit_viewport(GLcontext *ctx, int emit)
170 {
171 struct nouveau_channel *chan = context_chan(ctx);
172 struct nouveau_grobj *celsius = context_eng3d(ctx);
173 struct gl_framebuffer *fb = ctx->DrawBuffer;
174 float a[4] = {};
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 OUT_RINGp(chan, a, 4);
182
183 BEGIN_RING(chan, celsius, NV10TCL_VIEWPORT_CLIP_HORIZ(0), 1);
184 OUT_RING(chan, (fb->Width - 1) << 16 | 0x08000800);
185 BEGIN_RING(chan, celsius, NV10TCL_VIEWPORT_CLIP_VERT(0), 1);
186 OUT_RING(chan, (fb->Height - 1) << 16 | 0x08000800);
187
188 context_dirty(ctx, PROJECTION);
189 }