10a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath/*
20a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath * Copyright (C) 2009-2010 Francisco Jerez.
30a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath * All Rights Reserved.
40a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath *
50a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath * Permission is hereby granted, free of charge, to any person obtaining
60a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath * a copy of this software and associated documentation files (the
70a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath * "Software"), to deal in the Software without restriction, including
80a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath * without limitation the rights to use, copy, modify, merge, publish,
90a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath * distribute, sublicense, and/or sell copies of the Software, and to
100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath * permit persons to whom the Software is furnished to do so, subject to
110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath * the following conditions:
120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath *
130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath * The above copyright notice and this permission notice (including the
140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath * next paragraph) shall be included in all copies or substantial
150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath * portions of the Software.
160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath *
170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath *
250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath */
260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include <stdbool.h>
280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include "nouveau_driver.h"
290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include "nouveau_context.h"
300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include "nouveau_fbo.h"
310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include "nouveau_util.h"
320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include "nv_object.xml.h"
330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include "nv20_3d.xml.h"
340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include "nv04_driver.h"
350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include "nv10_driver.h"
360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include "nv20_driver.h"
370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathstatic void
390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathnv20_clear(struct gl_context *ctx, GLbitfield buffers)
400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath{
410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	struct nouveau_context *nctx = to_nouveau_context(ctx);
420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	struct nouveau_pushbuf *push = context_push(ctx);
430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	struct gl_framebuffer *fb = ctx->DrawBuffer;
440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	uint32_t clear = 0;
450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	nouveau_validate_framebuffer(ctx);
470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	nouveau_pushbuf_bufctx(push, nctx->hw.bufctx);
490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	if (nouveau_pushbuf_validate(push)) {
500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		nouveau_pushbuf_bufctx(push, NULL);
510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		return;
520a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	}
530a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
540a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	if (buffers & BUFFER_BITS_COLOR) {
550a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		struct nouveau_surface *s = &to_nouveau_renderbuffer(
560a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath			fb->_ColorDrawBuffers[0])->surface;
570a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
580a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		if (ctx->Color.ColorMask[0][RCOMP])
590a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath			clear |= NV20_3D_CLEAR_BUFFERS_COLOR_R;
600a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		if (ctx->Color.ColorMask[0][GCOMP])
610a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath			clear |= NV20_3D_CLEAR_BUFFERS_COLOR_G;
620a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		if (ctx->Color.ColorMask[0][BCOMP])
630a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath			clear |= NV20_3D_CLEAR_BUFFERS_COLOR_B;
640a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		if (ctx->Color.ColorMask[0][ACOMP])
650a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath			clear |= NV20_3D_CLEAR_BUFFERS_COLOR_A;
660a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
670a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		BEGIN_NV04(push, NV20_3D(CLEAR_VALUE), 1);
680a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		PUSH_DATA (push, pack_rgba_clamp_f(s->format, ctx->Color.ClearColor.f));
690a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
700a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		buffers &= ~BUFFER_BITS_COLOR;
710a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	}
720a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
730a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	if (buffers & (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL)) {
740a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		struct nouveau_surface *s = &to_nouveau_renderbuffer(
750a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath			fb->Attachment[BUFFER_DEPTH].Renderbuffer)->surface;
760a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
770a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		if (buffers & BUFFER_BIT_DEPTH && ctx->Depth.Mask)
780a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath			clear |= NV20_3D_CLEAR_BUFFERS_DEPTH;
790a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		if (buffers & BUFFER_BIT_STENCIL && ctx->Stencil.WriteMask[0])
800a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath			clear |= NV20_3D_CLEAR_BUFFERS_STENCIL;
810a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
820a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		BEGIN_NV04(push, NV20_3D(CLEAR_DEPTH_VALUE), 1);
830a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		PUSH_DATA (push, pack_zs_f(s->format, ctx->Depth.Clear,
840a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath					 ctx->Stencil.Clear));
850a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
860a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		buffers &= ~(BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
870a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	}
880a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
890a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	BEGIN_NV04(push, NV20_3D(CLEAR_BUFFERS), 1);
900a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, clear);
910a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
920a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	nouveau_pushbuf_bufctx(push, NULL);
930a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	nouveau_clear(ctx, buffers);
940a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath}
950a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
960a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathstatic void
970a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathnv20_hwctx_init(struct gl_context *ctx)
980a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath{
990a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	struct nouveau_pushbuf *push = context_push(ctx);
1000a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	struct nouveau_hw_state *hw = &to_nouveau_context(ctx)->hw;
1010a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	struct nv04_fifo *fifo = hw->chan->data;
1020a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	int i;
1030a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
1040a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	BEGIN_NV04(push, NV01_SUBC(3D, OBJECT), 1);
1050a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, hw->eng3d->handle);
1060a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	BEGIN_NV04(push, NV20_3D(DMA_NOTIFY), 1);
1070a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, hw->ntfy->handle);
1080a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	BEGIN_NV04(push, NV20_3D(DMA_TEXTURE0), 2);
1090a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, fifo->vram);
1100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, fifo->gart);
1110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	BEGIN_NV04(push, NV20_3D(DMA_COLOR), 2);
1120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, fifo->vram);
1130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, fifo->vram);
1140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	BEGIN_NV04(push, NV20_3D(DMA_VTXBUF0), 2);
1150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, fifo->vram);
1160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, fifo->gart);
1170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
1180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	BEGIN_NV04(push, NV20_3D(DMA_QUERY), 1);
1190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, 0);
1200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
1210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	BEGIN_NV04(push, NV20_3D(RT_HORIZ), 2);
1220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, 0);
1230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, 0);
1240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
1250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	BEGIN_NV04(push, NV20_3D(VIEWPORT_CLIP_HORIZ(0)), 1);
1260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, 0xfff << 16 | 0x0);
1270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	BEGIN_NV04(push, NV20_3D(VIEWPORT_CLIP_VERT(0)), 1);
1280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, 0xfff << 16 | 0x0);
1290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
1300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	for (i = 1; i < NV20_3D_VIEWPORT_CLIP_HORIZ__LEN; i++) {
1310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		BEGIN_NV04(push, NV20_3D(VIEWPORT_CLIP_HORIZ(i)), 1);
1320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		PUSH_DATA (push, 0);
1330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		BEGIN_NV04(push, NV20_3D(VIEWPORT_CLIP_VERT(i)), 1);
1340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		PUSH_DATA (push, 0);
1350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	}
1360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
1370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	BEGIN_NV04(push, NV20_3D(VIEWPORT_CLIP_MODE), 1);
1380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, 0);
1390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
1400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	BEGIN_NV04(push, SUBC_3D(0x17e0), 3);
1410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATAf(push, 0.0);
1420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATAf(push, 0.0);
1430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATAf(push, 1.0);
1440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
1450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	if (context_chipset(ctx) >= 0x25) {
1460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		BEGIN_NV04(push, NV20_3D(TEX_RCOMP), 1);
1470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		PUSH_DATA (push, NV20_3D_TEX_RCOMP_LEQUAL | 0xdb0);
1480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	} else {
1490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		BEGIN_NV04(push, SUBC_3D(0x1e68), 1);
1500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		PUSH_DATA (push, 0x4b800000); /* 16777216.000000 */
1510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		BEGIN_NV04(push, NV20_3D(TEX_RCOMP), 1);
1520a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		PUSH_DATA (push, NV20_3D_TEX_RCOMP_LEQUAL);
1530a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	}
1540a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
1550a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	BEGIN_NV04(push, SUBC_3D(0x290), 1);
1560a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, 0x10 << 16 | 1);
1570a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	BEGIN_NV04(push, SUBC_3D(0x9fc), 1);
1580a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, 0);
1590a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	BEGIN_NV04(push, SUBC_3D(0x1d80), 1);
1600a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, 1);
1610a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	BEGIN_NV04(push, SUBC_3D(0x9f8), 1);
1620a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, 4);
1630a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	BEGIN_NV04(push, SUBC_3D(0x17ec), 3);
1640a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATAf(push, 0.0);
1650a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATAf(push, 1.0);
1660a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATAf(push, 0.0);
1670a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
1680a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	if (context_chipset(ctx) >= 0x25) {
1690a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		BEGIN_NV04(push, SUBC_3D(0x1d88), 1);
1700a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		PUSH_DATA (push, 3);
1710a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
1720a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		BEGIN_NV04(push, NV25_3D(DMA_HIERZ), 1);
1730a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		PUSH_DATA (push, fifo->vram);
1740a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		BEGIN_NV04(push, NV25_3D(UNK01AC), 1);
1750a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath		PUSH_DATA (push, fifo->vram);
1760a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	}
1770a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
1780a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	BEGIN_NV04(push, NV20_3D(DMA_FENCE), 1);
1790a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, 0);
1800a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
1810a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	BEGIN_NV04(push, SUBC_3D(0x1e98), 1);
1820a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, 0);
1830a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
1840a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	BEGIN_NV04(push, NV04_GRAPH(3D, NOTIFY), 1);
1850a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, 0);
1860a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
1870a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	BEGIN_NV04(push, SUBC_3D(0x120), 3);
1880a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, 0);
1890a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath	PUSH_DATA (push, 1);
190	PUSH_DATA (push, 2);
191
192	if (context_chipset(ctx) >= 0x25) {
193		BEGIN_NV04(push, SUBC_3D(0x1da4), 1);
194		PUSH_DATA (push, 0);
195	}
196
197	BEGIN_NV04(push, NV20_3D(RT_HORIZ), 2);
198	PUSH_DATA (push, 0 << 16 | 0);
199	PUSH_DATA (push, 0 << 16 | 0);
200
201	BEGIN_NV04(push, NV20_3D(ALPHA_FUNC_ENABLE), 1);
202	PUSH_DATA (push, 0);
203	BEGIN_NV04(push, NV20_3D(ALPHA_FUNC_FUNC), 2);
204	PUSH_DATA (push, NV20_3D_ALPHA_FUNC_FUNC_ALWAYS);
205	PUSH_DATA (push, 0);
206
207	for (i = 0; i < NV20_3D_TEX__LEN; i++) {
208		BEGIN_NV04(push, NV20_3D(TEX_ENABLE(i)), 1);
209		PUSH_DATA (push, 0);
210	}
211
212	BEGIN_NV04(push, NV20_3D(TEX_SHADER_OP), 1);
213	PUSH_DATA (push, 0);
214	BEGIN_NV04(push, NV20_3D(TEX_SHADER_CULL_MODE), 1);
215	PUSH_DATA (push, 0);
216
217	BEGIN_NV04(push, NV20_3D(RC_IN_ALPHA(0)), 4);
218	PUSH_DATA (push, 0x30d410d0);
219	PUSH_DATA (push, 0);
220	PUSH_DATA (push, 0);
221	PUSH_DATA (push, 0);
222	BEGIN_NV04(push, NV20_3D(RC_OUT_RGB(0)), 4);
223	PUSH_DATA (push, 0x00000c00);
224	PUSH_DATA (push, 0);
225	PUSH_DATA (push, 0);
226	PUSH_DATA (push, 0);
227	BEGIN_NV04(push, NV20_3D(RC_ENABLE), 1);
228	PUSH_DATA (push, 0x00011101);
229	BEGIN_NV04(push, NV20_3D(RC_FINAL0), 2);
230	PUSH_DATA (push, 0x130e0300);
231	PUSH_DATA (push, 0x0c091c80);
232	BEGIN_NV04(push, NV20_3D(RC_OUT_ALPHA(0)), 4);
233	PUSH_DATA (push, 0x00000c00);
234	PUSH_DATA (push, 0);
235	PUSH_DATA (push, 0);
236	PUSH_DATA (push, 0);
237	BEGIN_NV04(push, NV20_3D(RC_IN_RGB(0)), 4);
238	PUSH_DATA (push, 0x20c400c0);
239	PUSH_DATA (push, 0);
240	PUSH_DATA (push, 0);
241	PUSH_DATA (push, 0);
242	BEGIN_NV04(push, NV20_3D(RC_COLOR0), 2);
243	PUSH_DATA (push, 0);
244	PUSH_DATA (push, 0);
245	BEGIN_NV04(push, NV20_3D(RC_CONSTANT_COLOR0(0)), 4);
246	PUSH_DATA (push, 0x035125a0);
247	PUSH_DATA (push, 0);
248	PUSH_DATA (push, 0x40002000);
249	PUSH_DATA (push, 0);
250
251	BEGIN_NV04(push, NV20_3D(MULTISAMPLE_CONTROL), 1);
252	PUSH_DATA (push, 0xffff0000);
253	BEGIN_NV04(push, NV20_3D(BLEND_FUNC_ENABLE), 1);
254	PUSH_DATA (push, 0);
255	BEGIN_NV04(push, NV20_3D(DITHER_ENABLE), 1);
256	PUSH_DATA (push, 0);
257	BEGIN_NV04(push, NV20_3D(STENCIL_ENABLE), 1);
258	PUSH_DATA (push, 0);
259	BEGIN_NV04(push, NV20_3D(BLEND_FUNC_SRC), 4);
260	PUSH_DATA (push, NV20_3D_BLEND_FUNC_SRC_ONE);
261	PUSH_DATA (push, NV20_3D_BLEND_FUNC_DST_ZERO);
262	PUSH_DATA (push, 0);
263	PUSH_DATA (push, NV20_3D_BLEND_EQUATION_FUNC_ADD);
264	BEGIN_NV04(push, NV20_3D(STENCIL_MASK), 7);
265	PUSH_DATA (push, 0xff);
266	PUSH_DATA (push, NV20_3D_STENCIL_FUNC_FUNC_ALWAYS);
267	PUSH_DATA (push, 0);
268	PUSH_DATA (push, 0xff);
269	PUSH_DATA (push, NV20_3D_STENCIL_OP_FAIL_KEEP);
270	PUSH_DATA (push, NV20_3D_STENCIL_OP_ZFAIL_KEEP);
271	PUSH_DATA (push, NV20_3D_STENCIL_OP_ZPASS_KEEP);
272
273	BEGIN_NV04(push, NV20_3D(COLOR_LOGIC_OP_ENABLE), 2);
274	PUSH_DATA (push, 0);
275	PUSH_DATA (push, NV20_3D_COLOR_LOGIC_OP_OP_COPY);
276	BEGIN_NV04(push, SUBC_3D(0x17cc), 1);
277	PUSH_DATA (push, 0);
278	if (context_chipset(ctx) >= 0x25) {
279		BEGIN_NV04(push, SUBC_3D(0x1d84), 1);
280		PUSH_DATA (push, 1);
281	}
282	BEGIN_NV04(push, NV20_3D(LIGHTING_ENABLE), 1);
283	PUSH_DATA (push, 0);
284	BEGIN_NV04(push, NV20_3D(LIGHT_MODEL), 1);
285	PUSH_DATA (push, NV20_3D_LIGHT_MODEL_VIEWER_NONLOCAL);
286	BEGIN_NV04(push, NV20_3D(SEPARATE_SPECULAR_ENABLE), 1);
287	PUSH_DATA (push, 0);
288	BEGIN_NV04(push, NV20_3D(LIGHT_MODEL_TWO_SIDE_ENABLE), 1);
289	PUSH_DATA (push, 0);
290	BEGIN_NV04(push, NV20_3D(ENABLED_LIGHTS), 1);
291	PUSH_DATA (push, 0);
292	BEGIN_NV04(push, NV20_3D(NORMALIZE_ENABLE), 1);
293	PUSH_DATA (push, 0);
294	BEGIN_NV04(push, NV20_3D(POLYGON_STIPPLE_PATTERN(0)),
295		   NV20_3D_POLYGON_STIPPLE_PATTERN__LEN);
296	for (i = 0; i < NV20_3D_POLYGON_STIPPLE_PATTERN__LEN; i++) {
297		PUSH_DATA (push, 0xffffffff);
298	}
299
300	BEGIN_NV04(push, NV20_3D(POLYGON_OFFSET_POINT_ENABLE), 3);
301	PUSH_DATA (push, 0);
302	PUSH_DATA (push, 0);
303	PUSH_DATA (push, 0);
304	BEGIN_NV04(push, NV20_3D(DEPTH_FUNC), 1);
305	PUSH_DATA (push, NV20_3D_DEPTH_FUNC_LESS);
306	BEGIN_NV04(push, NV20_3D(DEPTH_WRITE_ENABLE), 1);
307	PUSH_DATA (push, 0);
308	BEGIN_NV04(push, NV20_3D(DEPTH_TEST_ENABLE), 1);
309	PUSH_DATA (push, 0);
310	BEGIN_NV04(push, NV20_3D(POLYGON_OFFSET_FACTOR), 2);
311	PUSH_DATAf(push, 0.0);
312	PUSH_DATAf(push, 0.0);
313	BEGIN_NV04(push, NV20_3D(DEPTH_CLAMP), 1);
314	PUSH_DATA (push, 1);
315	if (context_chipset(ctx) < 0x25) {
316		BEGIN_NV04(push, SUBC_3D(0x1d84), 1);
317		PUSH_DATA (push, 3);
318	}
319	BEGIN_NV04(push, NV20_3D(POINT_SIZE), 1);
320	if (context_chipset(ctx) >= 0x25)
321		PUSH_DATAf(push, 1.0);
322	else
323		PUSH_DATA (push, 8);
324
325	if (context_chipset(ctx) >= 0x25) {
326		BEGIN_NV04(push, NV20_3D(POINT_PARAMETERS_ENABLE), 1);
327		PUSH_DATA (push, 0);
328		BEGIN_NV04(push, SUBC_3D(0x0a1c), 1);
329		PUSH_DATA (push, 0x800);
330	} else {
331		BEGIN_NV04(push, NV20_3D(POINT_PARAMETERS_ENABLE), 2);
332		PUSH_DATA (push, 0);
333		PUSH_DATA (push, 0);
334	}
335
336	BEGIN_NV04(push, NV20_3D(LINE_WIDTH), 1);
337	PUSH_DATA (push, 8);
338	BEGIN_NV04(push, NV20_3D(LINE_SMOOTH_ENABLE), 1);
339	PUSH_DATA (push, 0);
340	BEGIN_NV04(push, NV20_3D(POLYGON_MODE_FRONT), 2);
341	PUSH_DATA (push, NV20_3D_POLYGON_MODE_FRONT_FILL);
342	PUSH_DATA (push, NV20_3D_POLYGON_MODE_BACK_FILL);
343	BEGIN_NV04(push, NV20_3D(CULL_FACE), 2);
344	PUSH_DATA (push, NV20_3D_CULL_FACE_BACK);
345	PUSH_DATA (push, NV20_3D_FRONT_FACE_CCW);
346	BEGIN_NV04(push, NV20_3D(POLYGON_SMOOTH_ENABLE), 1);
347	PUSH_DATA (push, 0);
348	BEGIN_NV04(push, NV20_3D(CULL_FACE_ENABLE), 1);
349	PUSH_DATA (push, 0);
350	BEGIN_NV04(push, NV20_3D(SHADE_MODEL), 1);
351	PUSH_DATA (push, NV20_3D_SHADE_MODEL_SMOOTH);
352	BEGIN_NV04(push, NV20_3D(POLYGON_STIPPLE_ENABLE), 1);
353	PUSH_DATA (push, 0);
354
355	BEGIN_NV04(push, NV20_3D(TEX_GEN_MODE(0,0)),
356		   4 * NV20_3D_TEX_GEN_MODE__ESIZE);
357	for (i=0; i < 4 * NV20_3D_TEX_GEN_MODE__LEN; i++)
358		PUSH_DATA (push, 0);
359
360	BEGIN_NV04(push, NV20_3D(FOG_COEFF(0)), 3);
361	PUSH_DATAf(push, 1.5);
362	PUSH_DATAf(push, -0.090168);
363	PUSH_DATAf(push, 0.0);
364	BEGIN_NV04(push, NV20_3D(FOG_MODE), 2);
365	PUSH_DATA (push, NV20_3D_FOG_MODE_EXP_SIGNED);
366	PUSH_DATA (push, NV20_3D_FOG_COORD_FOG);
367	BEGIN_NV04(push, NV20_3D(FOG_ENABLE), 2);
368	PUSH_DATA (push, 0);
369	PUSH_DATA (push, 0);
370
371	BEGIN_NV04(push, NV20_3D(ENGINE), 1);
372	PUSH_DATA (push, NV20_3D_ENGINE_FIXED);
373
374	for (i = 0; i < NV20_3D_TEX_MATRIX_ENABLE__LEN; i++) {
375		BEGIN_NV04(push, NV20_3D(TEX_MATRIX_ENABLE(i)), 1);
376		PUSH_DATA (push, 0);
377	}
378
379	BEGIN_NV04(push, NV20_3D(VERTEX_ATTR_4F_X(1)), 4 * 15);
380	PUSH_DATAf(push, 1.0);
381	PUSH_DATAf(push, 0.0);
382	PUSH_DATAf(push, 0.0);
383	PUSH_DATAf(push, 1.0);
384	PUSH_DATAf(push, 0.0);
385	PUSH_DATAf(push, 0.0);
386	PUSH_DATAf(push, 1.0);
387	PUSH_DATAf(push, 1.0);
388	PUSH_DATAf(push, 1.0);
389	PUSH_DATAf(push, 1.0);
390	PUSH_DATAf(push, 1.0);
391	PUSH_DATAf(push, 1.0);
392	for (i = 0; i < 12; i++) {
393		PUSH_DATAf(push, 0.0);
394		PUSH_DATAf(push, 0.0);
395		PUSH_DATAf(push, 0.0);
396		PUSH_DATAf(push, 1.0);
397	}
398
399	BEGIN_NV04(push, NV20_3D(EDGEFLAG_ENABLE), 1);
400	PUSH_DATA (push, 1);
401	BEGIN_NV04(push, NV20_3D(COLOR_MASK), 1);
402	PUSH_DATA (push, 0x00010101);
403	BEGIN_NV04(push, NV20_3D(CLEAR_VALUE), 1);
404	PUSH_DATA (push, 0);
405
406	BEGIN_NV04(push, NV20_3D(DEPTH_RANGE_NEAR), 2);
407	PUSH_DATAf(push, 0.0);
408	PUSH_DATAf(push, 16777216.0);
409
410	BEGIN_NV04(push, NV20_3D(VIEWPORT_TRANSLATE_X), 4);
411	PUSH_DATAf(push, 0.0);
412	PUSH_DATAf(push, 0.0);
413	PUSH_DATAf(push, 0.0);
414	PUSH_DATAf(push, 16777215.0);
415
416	BEGIN_NV04(push, NV20_3D(VIEWPORT_SCALE_X), 4);
417	PUSH_DATAf(push, 0.0);
418	PUSH_DATAf(push, 0.0);
419	PUSH_DATAf(push, 16777215.0 * 0.5);
420	PUSH_DATAf(push, 65535.0);
421
422	PUSH_KICK (push);
423}
424
425static void
426nv20_context_destroy(struct gl_context *ctx)
427{
428	struct nouveau_context *nctx = to_nouveau_context(ctx);
429
430	nv04_surface_takedown(ctx);
431	nv20_swtnl_destroy(ctx);
432	nv20_vbo_destroy(ctx);
433
434	nouveau_object_del(&nctx->hw.eng3d);
435
436	nouveau_context_deinit(ctx);
437	FREE(ctx);
438}
439
440static struct gl_context *
441nv20_context_create(struct nouveau_screen *screen, const struct gl_config *visual,
442		    struct gl_context *share_ctx)
443{
444	struct nouveau_context *nctx;
445	struct gl_context *ctx;
446	unsigned kelvin_class;
447	int ret;
448
449	nctx = CALLOC_STRUCT(nouveau_context);
450	if (!nctx)
451		return NULL;
452
453	ctx = &nctx->base;
454
455	if (!nouveau_context_init(ctx, screen, visual, share_ctx))
456		goto fail;
457
458	ctx->Extensions.ARB_texture_env_crossbar = true;
459	ctx->Extensions.ARB_texture_env_combine = true;
460	ctx->Extensions.ARB_texture_env_dot3 = true;
461	ctx->Extensions.NV_fog_distance = true;
462	ctx->Extensions.NV_texture_rectangle = true;
463	if (ctx->Mesa_DXTn) {
464		ctx->Extensions.EXT_texture_compression_s3tc = true;
465		ctx->Extensions.S3_s3tc = true;
466	}
467
468	/* GL constants. */
469	ctx->Const.MaxTextureCoordUnits = NV20_TEXTURE_UNITS;
470	ctx->Const.MaxTextureImageUnits = NV20_TEXTURE_UNITS;
471	ctx->Const.MaxTextureUnits = NV20_TEXTURE_UNITS;
472	ctx->Const.MaxTextureMaxAnisotropy = 8;
473	ctx->Const.MaxTextureLodBias = 15;
474	ctx->Driver.Clear = nv20_clear;
475
476	/* 2D engine. */
477	ret = nv04_surface_init(ctx);
478	if (!ret)
479		goto fail;
480
481	/* 3D engine. */
482	if (context_chipset(ctx) >= 0x25)
483		kelvin_class = NV25_3D_CLASS;
484	else
485		kelvin_class = NV20_3D_CLASS;
486
487	ret = nouveau_object_new(context_chan(ctx), 0xbeef0001, kelvin_class,
488				 NULL, 0, &nctx->hw.eng3d);
489	if (ret)
490		goto fail;
491
492	nv20_hwctx_init(ctx);
493	nv20_vbo_init(ctx);
494	nv20_swtnl_init(ctx);
495
496	return ctx;
497
498fail:
499	nv20_context_destroy(ctx);
500	return NULL;
501}
502
503const struct nouveau_driver nv20_driver = {
504	.context_create = nv20_context_create,
505	.context_destroy = nv20_context_destroy,
506	.surface_copy = nv04_surface_copy,
507	.surface_fill = nv04_surface_fill,
508	.emit = (nouveau_state_func[]) {
509		nv10_emit_alpha_func,
510		nv10_emit_blend_color,
511		nv10_emit_blend_equation,
512		nv10_emit_blend_func,
513		nv20_emit_clip_plane,
514		nv20_emit_clip_plane,
515		nv20_emit_clip_plane,
516		nv20_emit_clip_plane,
517		nv20_emit_clip_plane,
518		nv20_emit_clip_plane,
519		nv10_emit_color_mask,
520		nv20_emit_color_material,
521		nv10_emit_cull_face,
522		nv10_emit_front_face,
523		nv10_emit_depth,
524		nv10_emit_dither,
525		nv20_emit_frag,
526		nv20_emit_framebuffer,
527		nv20_emit_fog,
528		nv10_emit_light_enable,
529		nv20_emit_light_model,
530		nv20_emit_light_source,
531		nv20_emit_light_source,
532		nv20_emit_light_source,
533		nv20_emit_light_source,
534		nv20_emit_light_source,
535		nv20_emit_light_source,
536		nv20_emit_light_source,
537		nv20_emit_light_source,
538		nv10_emit_line_stipple,
539		nv10_emit_line_mode,
540		nv20_emit_logic_opcode,
541		nv20_emit_material_ambient,
542		nv20_emit_material_ambient,
543		nv20_emit_material_diffuse,
544		nv20_emit_material_diffuse,
545		nv20_emit_material_specular,
546		nv20_emit_material_specular,
547		nv20_emit_material_shininess,
548		nv20_emit_material_shininess,
549		nv20_emit_modelview,
550		nv20_emit_point_mode,
551		nv10_emit_point_parameter,
552		nv10_emit_polygon_mode,
553		nv10_emit_polygon_offset,
554		nv10_emit_polygon_stipple,
555		nv20_emit_projection,
556		nv10_emit_render_mode,
557		nv10_emit_scissor,
558		nv10_emit_shade_model,
559		nv10_emit_stencil_func,
560		nv10_emit_stencil_mask,
561		nv10_emit_stencil_op,
562		nv20_emit_tex_env,
563		nv20_emit_tex_env,
564		nv20_emit_tex_env,
565		nv20_emit_tex_env,
566		nv20_emit_tex_gen,
567		nv20_emit_tex_gen,
568		nv20_emit_tex_gen,
569		nv20_emit_tex_gen,
570		nv20_emit_tex_mat,
571		nv20_emit_tex_mat,
572		nv20_emit_tex_mat,
573		nv20_emit_tex_mat,
574		nv20_emit_tex_obj,
575		nv20_emit_tex_obj,
576		nv20_emit_tex_obj,
577		nv20_emit_tex_obj,
578		nv20_emit_viewport,
579		nv20_emit_tex_shader
580	},
581	.num_emit = NUM_NV20_STATE,
582};
583