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
35static inline unsigned
36get_rt_format(mesa_format format)
37{
38	switch (format) {
39	case MESA_FORMAT_B8G8R8X8_UNORM:
40		return NV10_3D_RT_FORMAT_COLOR_X8R8G8B8;
41	case MESA_FORMAT_B8G8R8A8_UNORM:
42		return NV10_3D_RT_FORMAT_COLOR_A8R8G8B8;
43	case MESA_FORMAT_B5G6R5_UNORM:
44		return NV10_3D_RT_FORMAT_COLOR_R5G6B5;
45	case MESA_FORMAT_Z_UNORM16:
46		return NV10_3D_RT_FORMAT_DEPTH_Z16;
47	case MESA_FORMAT_S8_UINT_Z24_UNORM:
48		return NV10_3D_RT_FORMAT_DEPTH_Z24S8;
49	default:
50		assert(0);
51	}
52}
53
54static void
55setup_hierz_buffer(struct gl_context *ctx)
56{
57	struct nouveau_pushbuf *push = context_push(ctx);
58	struct gl_framebuffer *fb = ctx->DrawBuffer;
59	struct nouveau_framebuffer *nfb = to_nouveau_framebuffer(fb);
60	unsigned pitch = align(fb->Width, 128),
61		height = align(fb->Height, 2),
62		size = pitch * height;
63
64	if (!nfb->hierz.bo || nfb->hierz.bo->size != size) {
65		union nouveau_bo_config config = {
66			.nv04.surf_flags = NV04_BO_ZETA,
67			.nv04.surf_pitch = 0
68		};
69
70		nouveau_bo_ref(NULL, &nfb->hierz.bo);
71		nouveau_bo_new(context_dev(ctx), NOUVEAU_BO_VRAM, 0, size,
72			       &config, &nfb->hierz.bo);
73	}
74
75	PUSH_SPACE(push, 11);
76	BEGIN_NV04(push, NV17_3D(HIERZ_OFFSET), 1);
77	PUSH_MTHDl(push, NV17_3D(HIERZ_OFFSET), BUFCTX_FB,
78			 nfb->hierz.bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR);
79	BEGIN_NV04(push, NV17_3D(HIERZ_WINDOW_X), 4);
80	PUSH_DATAf(push, - 1792);
81	PUSH_DATAf(push, - 2304 + fb->Height);
82	PUSH_DATAf(push, fb->_DepthMaxF / 2);
83	PUSH_DATAf(push, 0);
84
85	BEGIN_NV04(push, NV17_3D(HIERZ_PITCH), 1);
86	PUSH_DATA (push, pitch);
87
88	BEGIN_NV04(push, NV17_3D(HIERZ_ENABLE), 1);
89	PUSH_DATA (push, 1);
90}
91
92void
93nv10_emit_framebuffer(struct gl_context *ctx, int emit)
94{
95	struct nouveau_pushbuf *push = context_push(ctx);
96	struct gl_framebuffer *fb = ctx->DrawBuffer;
97	struct nouveau_surface *s;
98	unsigned rt_format = NV10_3D_RT_FORMAT_TYPE_LINEAR;
99	unsigned rt_pitch = 0, zeta_pitch = 0;
100	unsigned bo_flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR;
101
102	if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT)
103		return;
104
105	PUSH_RESET(push, BUFCTX_FB);
106
107	/* At least nv11 seems to get sad if we don't do this before
108	 * swapping RTs.*/
109	if (context_eng3d(ctx)->oclass < NV17_3D_CLASS) {
110		int i;
111
112		for (i = 0; i < 6; i++) {
113			BEGIN_NV04(push, NV04_GRAPH(3D, NOP), 1);
114			PUSH_DATA (push, 0);
115		}
116	}
117
118	/* Render target */
119	if (fb->_ColorDrawBuffers[0]) {
120		s = &to_nouveau_renderbuffer(
121			fb->_ColorDrawBuffers[0])->surface;
122
123		rt_format |= get_rt_format(s->format);
124		zeta_pitch = rt_pitch = s->pitch;
125
126		BEGIN_NV04(push, NV10_3D(COLOR_OFFSET), 1);
127		PUSH_MTHDl(push, NV10_3D(COLOR_OFFSET), BUFCTX_FB,
128				 s->bo, 0, bo_flags);
129	}
130
131	/* depth/stencil */
132	if (fb->Attachment[BUFFER_DEPTH].Renderbuffer) {
133		s = &to_nouveau_renderbuffer(
134			fb->Attachment[BUFFER_DEPTH].Renderbuffer)->surface;
135
136		rt_format |= get_rt_format(s->format);
137		zeta_pitch = s->pitch;
138
139		BEGIN_NV04(push, NV10_3D(ZETA_OFFSET), 1);
140		PUSH_MTHDl(push, NV10_3D(ZETA_OFFSET), BUFCTX_FB,
141				 s->bo, 0, bo_flags);
142
143		if (context_eng3d(ctx)->oclass >= NV17_3D_CLASS) {
144			setup_hierz_buffer(ctx);
145			context_dirty(ctx, ZCLEAR);
146		}
147	}
148
149	BEGIN_NV04(push, NV10_3D(RT_FORMAT), 2);
150	PUSH_DATA (push, rt_format);
151	PUSH_DATA (push, zeta_pitch << 16 | rt_pitch);
152
153	context_dirty(ctx, VIEWPORT);
154	context_dirty(ctx, SCISSOR);
155	context_dirty(ctx, DEPTH);
156}
157
158void
159nv10_emit_render_mode(struct gl_context *ctx, int emit)
160{
161}
162
163void
164nv10_emit_scissor(struct gl_context *ctx, int emit)
165{
166	struct nouveau_pushbuf *push = context_push(ctx);
167	int x, y, w, h;
168
169	get_scissors(ctx->DrawBuffer, &x, &y, &w, &h);
170
171	BEGIN_NV04(push, NV10_3D(RT_HORIZ), 2);
172	PUSH_DATA (push, w << 16 | x);
173	PUSH_DATA (push, h << 16 | y);
174}
175
176void
177nv10_emit_viewport(struct gl_context *ctx, int emit)
178{
179	struct nouveau_pushbuf *push = context_push(ctx);
180	struct gl_viewport_attrib *vp = &ctx->ViewportArray[0];
181	struct gl_framebuffer *fb = ctx->DrawBuffer;
182	float a[4] = {};
183
184	get_viewport_translate(ctx, a);
185	a[0] -= 2048;
186	a[1] -= 2048;
187	if (nv10_use_viewport_zclear(ctx))
188		a[2] = nv10_transform_depth(ctx, (vp->Far + vp->Near) / 2);
189
190	BEGIN_NV04(push, NV10_3D(VIEWPORT_TRANSLATE_X), 4);
191	PUSH_DATAp(push, a, 4);
192
193	BEGIN_NV04(push, NV10_3D(VIEWPORT_CLIP_HORIZ(0)), 1);
194	PUSH_DATA (push, (fb->Width - 1) << 16 | 0x08000800);
195	BEGIN_NV04(push, NV10_3D(VIEWPORT_CLIP_VERT(0)), 1);
196	PUSH_DATA (push, (fb->Height - 1) << 16 | 0x08000800);
197
198	context_dirty(ctx, PROJECTION);
199}
200
201void
202nv10_emit_zclear(struct gl_context *ctx, int emit)
203{
204	struct nouveau_context *nctx = to_nouveau_context(ctx);
205	struct nouveau_pushbuf *push = context_push(ctx);
206	struct nouveau_framebuffer *nfb =
207		to_nouveau_framebuffer(ctx->DrawBuffer);
208
209	if (nfb->hierz.bo) {
210		BEGIN_NV04(push, NV17_3D(ZCLEAR_ENABLE), 2);
211		PUSH_DATAb(push, !nctx->hierz.clear_blocked);
212		PUSH_DATA (push, nfb->hierz.clear_value |
213			 (nctx->hierz.clear_seq & 0xff));
214	} else {
215		BEGIN_NV04(push, NV10_3D(DEPTH_RANGE_NEAR), 2);
216		PUSH_DATAf(push, nv10_transform_depth(ctx, 0));
217		PUSH_DATAf(push, nv10_transform_depth(ctx, 1));
218		context_dirty(ctx, VIEWPORT);
219	}
220}
221