Merge branch 'lp-offset-twoside'
[mesa.git] / src / mesa / drivers / dri / nouveau / nv20_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_gldefs.h"
31 #include "nouveau_util.h"
32 #include "nv20_3d.xml.h"
33 #include "nv20_driver.h"
34
35 static inline unsigned
36 get_rt_format(gl_format format)
37 {
38 switch (format) {
39 case MESA_FORMAT_XRGB8888:
40 return NV20_3D_RT_FORMAT_COLOR_X8R8G8B8;
41 case MESA_FORMAT_ARGB8888:
42 return NV20_3D_RT_FORMAT_COLOR_A8R8G8B8;
43 case MESA_FORMAT_RGB565:
44 return NV20_3D_RT_FORMAT_COLOR_R5G6B5;
45 case MESA_FORMAT_Z16:
46 return NV20_3D_RT_FORMAT_DEPTH_Z16;
47 case MESA_FORMAT_Z24_S8:
48 return NV20_3D_RT_FORMAT_DEPTH_Z24S8;
49 default:
50 assert(0);
51 }
52 }
53
54 static void
55 setup_hierz_buffer(struct gl_context *ctx)
56 {
57 struct nouveau_channel *chan = context_chan(ctx);
58 struct nouveau_grobj *kelvin = context_eng3d(ctx);
59 struct nouveau_bo_context *bctx = context_bctx(ctx, HIERZ);
60 struct gl_framebuffer *fb = ctx->DrawBuffer;
61 struct nouveau_framebuffer *nfb = to_nouveau_framebuffer(fb);
62 unsigned pitch = align(fb->Width, 128),
63 height = align(fb->Height, 2),
64 size = pitch * height;
65
66 if (!nfb->hierz.bo || nfb->hierz.bo->size != size) {
67 nouveau_bo_ref(NULL, &nfb->hierz.bo);
68 nouveau_bo_new(context_dev(ctx), NOUVEAU_BO_VRAM, 0, size,
69 &nfb->hierz.bo);
70 }
71
72 BEGIN_RING(chan, kelvin, NV25_3D_HIERZ_PITCH, 1);
73 OUT_RING(chan, pitch);
74
75 nouveau_bo_markl(bctx, kelvin, NV25_3D_HIERZ_OFFSET, nfb->hierz.bo,
76 0, NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR);
77 }
78
79 void
80 nv20_emit_framebuffer(struct gl_context *ctx, int emit)
81 {
82 struct nouveau_channel *chan = context_chan(ctx);
83 struct nouveau_grobj *kelvin = context_eng3d(ctx);
84 struct nouveau_bo_context *bctx = context_bctx(ctx, FRAMEBUFFER);
85 struct gl_framebuffer *fb = ctx->DrawBuffer;
86 struct nouveau_surface *s;
87 unsigned rt_format = NV20_3D_RT_FORMAT_TYPE_LINEAR;
88 unsigned rt_pitch = 0, zeta_pitch = 0;
89 unsigned bo_flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR;
90
91 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT)
92 return;
93
94 /* Render target */
95 if (fb->_ColorDrawBuffers[0]) {
96 s = &to_nouveau_renderbuffer(
97 fb->_ColorDrawBuffers[0])->surface;
98
99 rt_format |= get_rt_format(s->format);
100 rt_pitch = s->pitch;
101
102 nouveau_bo_markl(bctx, kelvin, NV20_3D_COLOR_OFFSET,
103 s->bo, 0, bo_flags);
104 }
105
106 /* depth/stencil */
107 if (fb->_DepthBuffer) {
108 s = &to_nouveau_renderbuffer(
109 fb->_DepthBuffer->Wrapped)->surface;
110
111 rt_format |= get_rt_format(s->format);
112 zeta_pitch = s->pitch;
113
114 nouveau_bo_markl(bctx, kelvin, NV20_3D_ZETA_OFFSET,
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_Z24_S8);
121 zeta_pitch = rt_pitch;
122 }
123
124 BEGIN_RING(chan, kelvin, NV20_3D_RT_FORMAT, 2);
125 OUT_RING(chan, rt_format);
126 OUT_RING(chan, zeta_pitch << 16 | rt_pitch);
127
128 /* Recompute the viewport/scissor state. */
129 context_dirty(ctx, VIEWPORT);
130 context_dirty(ctx, SCISSOR);
131 }
132
133 void
134 nv20_emit_viewport(struct gl_context *ctx, int emit)
135 {
136 struct nouveau_channel *chan = context_chan(ctx);
137 struct nouveau_grobj *kelvin = context_eng3d(ctx);
138 struct gl_framebuffer *fb = ctx->DrawBuffer;
139 float a[4] = {};
140
141 get_viewport_translate(ctx, a);
142
143 BEGIN_RING(chan, kelvin, NV20_3D_VIEWPORT_TRANSLATE_X, 4);
144 OUT_RINGp(chan, a, 4);
145
146 BEGIN_RING(chan, kelvin, NV20_3D_VIEWPORT_CLIP_HORIZ(0), 1);
147 OUT_RING(chan, (fb->Width - 1) << 16);
148 BEGIN_RING(chan, kelvin, NV20_3D_VIEWPORT_CLIP_VERT(0), 1);
149 OUT_RING(chan, (fb->Height - 1) << 16);
150
151 context_dirty(ctx, PROJECTION);
152 }