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 "nv04_3d.xml.h"
32#include "nv04_driver.h"
33
34static inline unsigned
35get_rt_format(gl_format format)
36{
37	switch (format) {
38	case MESA_FORMAT_XRGB8888:
39		return NV04_CONTEXT_SURFACES_3D_FORMAT_COLOR_X8R8G8B8_X8R8G8B8;
40	case MESA_FORMAT_ARGB8888:
41		return NV04_CONTEXT_SURFACES_3D_FORMAT_COLOR_A8R8G8B8;
42	case MESA_FORMAT_RGB565:
43		return NV04_CONTEXT_SURFACES_3D_FORMAT_COLOR_R5G6B5;
44	default:
45		assert(0);
46	}
47}
48
49void
50nv04_emit_framebuffer(struct gl_context *ctx, int emit)
51{
52	struct nouveau_pushbuf *push = context_push(ctx);
53	struct gl_framebuffer *fb = ctx->DrawBuffer;
54	struct nouveau_surface *s;
55	uint32_t rt_format = NV04_CONTEXT_SURFACES_3D_FORMAT_TYPE_PITCH;
56	uint32_t rt_pitch = 0, zeta_pitch = 0;
57	unsigned bo_flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR;
58
59	if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT)
60		return;
61
62	PUSH_RESET(push, BUFCTX_FB);
63
64	/* Render target */
65	if (fb->_ColorDrawBuffers[0]) {
66		s = &to_nouveau_renderbuffer(
67			fb->_ColorDrawBuffers[0])->surface;
68
69		rt_format |= get_rt_format(s->format);
70		zeta_pitch = rt_pitch = s->pitch;
71
72		BEGIN_NV04(push, NV04_SF3D(OFFSET_COLOR), 1);
73		PUSH_MTHDl(push, NV04_SF3D(OFFSET_COLOR), BUFCTX_FB,
74				 s->bo, 0, bo_flags);
75	}
76
77	/* depth/stencil */
78	if (fb->Attachment[BUFFER_DEPTH].Renderbuffer) {
79		s = &to_nouveau_renderbuffer(
80			fb->Attachment[BUFFER_DEPTH].Renderbuffer)->surface;
81
82		zeta_pitch = s->pitch;
83
84		BEGIN_NV04(push, NV04_SF3D(OFFSET_ZETA), 1);
85		PUSH_MTHDl(push, NV04_SF3D(OFFSET_ZETA), BUFCTX_FB,
86				 s->bo, 0, bo_flags);
87	}
88
89	BEGIN_NV04(push, NV04_SF3D(FORMAT), 1);
90	PUSH_DATA (push, rt_format);
91	BEGIN_NV04(push, NV04_SF3D(PITCH), 1);
92	PUSH_DATA (push, zeta_pitch << 16 | rt_pitch);
93
94	/* Recompute the scissor state. */
95	context_dirty(ctx, SCISSOR);
96}
97
98void
99nv04_emit_scissor(struct gl_context *ctx, int emit)
100{
101	struct nouveau_pushbuf *push = context_push(ctx);
102	int x, y, w, h;
103
104	get_scissors(ctx->DrawBuffer, &x, &y, &w, &h);
105
106	BEGIN_NV04(push, NV04_SF3D(CLIP_HORIZONTAL), 2);
107	PUSH_DATA (push, w << 16 | x);
108	PUSH_DATA (push, h << 16 | y);
109}
110