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_gldefs.h"
31 #include "nouveau_util.h"
32 #include "nv20_3d.xml.h"
33 #include "nv20_driver.h"
34 
35 static inline unsigned
get_rt_format(mesa_format format)36 get_rt_format(mesa_format format)
37 {
38 	switch (format) {
39 	case MESA_FORMAT_B8G8R8X8_UNORM:
40 		return NV20_3D_RT_FORMAT_COLOR_X8R8G8B8;
41 	case MESA_FORMAT_B8G8R8A8_UNORM:
42 		return NV20_3D_RT_FORMAT_COLOR_A8R8G8B8;
43 	case MESA_FORMAT_B5G6R5_UNORM:
44 		return NV20_3D_RT_FORMAT_COLOR_R5G6B5;
45 	case MESA_FORMAT_Z_UNORM16:
46 		return NV20_3D_RT_FORMAT_DEPTH_Z16;
47 	case MESA_FORMAT_S8_UINT_Z24_UNORM:
48 		return NV20_3D_RT_FORMAT_DEPTH_Z24S8;
49 	default:
50 		assert(0);
51 	}
52 }
53 
54 static void
setup_hierz_buffer(struct gl_context * ctx)55 setup_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 		nouveau_bo_ref(NULL, &nfb->hierz.bo);
66 		nouveau_bo_new(context_dev(ctx), NOUVEAU_BO_VRAM, 0, size,
67 			       NULL, &nfb->hierz.bo);
68 	}
69 
70 	BEGIN_NV04(push, NV25_3D(HIERZ_PITCH), 1);
71 	PUSH_DATA (push, pitch);
72 	BEGIN_NV04(push, NV25_3D(HIERZ_OFFSET), 1);
73 	PUSH_MTHDl(push, NV25_3D(HIERZ_OFFSET), BUFCTX_FB,
74 			 nfb->hierz.bo, 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR);
75 }
76 
77 void
nv20_emit_framebuffer(struct gl_context * ctx,int emit)78 nv20_emit_framebuffer(struct gl_context *ctx, int emit)
79 {
80 	struct nouveau_pushbuf *push = context_push(ctx);
81 	struct gl_framebuffer *fb = ctx->DrawBuffer;
82 	struct nouveau_surface *s;
83 	unsigned rt_format = NV20_3D_RT_FORMAT_TYPE_LINEAR;
84 	unsigned rt_pitch = 0, zeta_pitch = 0;
85 	unsigned bo_flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR;
86 
87 	if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT)
88 		return;
89 
90 	PUSH_RESET(push, BUFCTX_FB);
91 
92 	/* Render target */
93 	if (fb->_ColorDrawBuffers[0]) {
94 		s = &to_nouveau_renderbuffer(
95 			fb->_ColorDrawBuffers[0])->surface;
96 
97 		rt_format |= get_rt_format(s->format);
98 		rt_pitch = s->pitch;
99 
100 		BEGIN_NV04(push, NV20_3D(COLOR_OFFSET), 1);
101 		PUSH_MTHDl(push, NV20_3D(COLOR_OFFSET), BUFCTX_FB,
102 				 s->bo, 0, bo_flags);
103 	}
104 
105 	/* depth/stencil */
106 	if (fb->Attachment[BUFFER_DEPTH].Renderbuffer) {
107 		s = &to_nouveau_renderbuffer(
108 			fb->Attachment[BUFFER_DEPTH].Renderbuffer)->surface;
109 
110 		rt_format |= get_rt_format(s->format);
111 		zeta_pitch = s->pitch;
112 
113 		BEGIN_NV04(push, NV20_3D(ZETA_OFFSET), 1);
114 		PUSH_MTHDl(push, NV20_3D(ZETA_OFFSET), BUFCTX_FB,
115 				 s->bo, 0, bo_flags);
116 
117 		if (context_chipset(ctx) >= 0x25)
118 			setup_hierz_buffer(ctx);
119 	} else {
120 		rt_format |= get_rt_format(MESA_FORMAT_S8_UINT_Z24_UNORM);
121 		zeta_pitch = rt_pitch;
122 	}
123 
124 	BEGIN_NV04(push, NV20_3D(RT_FORMAT), 2);
125 	PUSH_DATA (push, rt_format);
126 	PUSH_DATA (push, zeta_pitch << 16 | rt_pitch);
127 
128 	/* Recompute the viewport/scissor state. */
129 	context_dirty(ctx, VIEWPORT);
130 	context_dirty(ctx, SCISSOR);
131 	context_dirty(ctx, DEPTH);
132 }
133 
134 void
nv20_emit_viewport(struct gl_context * ctx,int emit)135 nv20_emit_viewport(struct gl_context *ctx, int emit)
136 {
137 	struct nouveau_pushbuf *push = context_push(ctx);
138 	struct gl_framebuffer *fb = ctx->DrawBuffer;
139 	float a[4] = {};
140 
141 	get_viewport_translate(ctx, a);
142 
143 	BEGIN_NV04(push, NV20_3D(VIEWPORT_TRANSLATE_X), 4);
144 	PUSH_DATAp(push, a, 4);
145 
146 	BEGIN_NV04(push, NV20_3D(VIEWPORT_CLIP_HORIZ(0)), 1);
147 	PUSH_DATA (push, (fb->Width - 1) << 16);
148 	BEGIN_NV04(push, NV20_3D(VIEWPORT_CLIP_VERT(0)), 1);
149 	PUSH_DATA (push, (fb->Height - 1) << 16);
150 
151 	context_dirty(ctx, PROJECTION);
152 }
153