Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / drivers / dri / nouveau / nv04_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_util.h"
31 #include "nv04_3d.xml.h"
32 #include "nv04_driver.h"
33
34 static inline unsigned
35 get_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
49 void
50 nv04_emit_framebuffer(struct gl_context *ctx, int emit)
51 {
52         struct nouveau_channel *chan = context_chan(ctx);
53         struct nouveau_hw_state *hw = &to_nouveau_context(ctx)->hw;
54         struct nouveau_grobj *surf3d = hw->surf3d;
55         struct nouveau_bo_context *bctx = context_bctx(ctx, FRAMEBUFFER);
56         struct gl_framebuffer *fb = ctx->DrawBuffer;
57         struct nouveau_surface *s;
58         uint32_t rt_format = NV04_CONTEXT_SURFACES_3D_FORMAT_TYPE_PITCH;
59         uint32_t rt_pitch = 0, zeta_pitch = 0;
60         unsigned bo_flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR;
61
62         if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT)
63                 return;
64
65         /* Render target */
66         if (fb->_ColorDrawBuffers[0]) {
67                 s = &to_nouveau_renderbuffer(
68                         fb->_ColorDrawBuffers[0])->surface;
69
70                 rt_format |= get_rt_format(s->format);
71                 zeta_pitch = rt_pitch = s->pitch;
72
73                 nouveau_bo_markl(bctx, surf3d,
74                                  NV04_CONTEXT_SURFACES_3D_OFFSET_COLOR,
75                                  s->bo, 0, bo_flags);
76         }
77
78         /* depth/stencil */
79         if (fb->_DepthBuffer) {
80                 s = &to_nouveau_renderbuffer(
81                         fb->_DepthBuffer->Wrapped)->surface;
82
83                 zeta_pitch = s->pitch;
84
85                 nouveau_bo_markl(bctx, surf3d,
86                                  NV04_CONTEXT_SURFACES_3D_OFFSET_ZETA,
87                                  s->bo, 0, bo_flags);
88         }
89
90         BEGIN_RING(chan, surf3d, NV04_CONTEXT_SURFACES_3D_FORMAT, 1);
91         OUT_RING(chan, rt_format);
92         BEGIN_RING(chan, surf3d, NV04_CONTEXT_SURFACES_3D_PITCH, 1);
93         OUT_RING(chan, zeta_pitch << 16 | rt_pitch);
94
95         /* Recompute the scissor state. */
96         context_dirty(ctx, SCISSOR);
97 }
98
99 void
100 nv04_emit_scissor(struct gl_context *ctx, int emit)
101 {
102         struct nouveau_channel *chan = context_chan(ctx);
103         struct nouveau_hw_state *hw = &to_nouveau_context(ctx)->hw;
104         struct nouveau_grobj *surf3d = hw->surf3d;
105         int x, y, w, h;
106
107         get_scissors(ctx->DrawBuffer, &x, &y, &w, &h);
108
109         BEGIN_RING(chan, surf3d, NV04_CONTEXT_SURFACES_3D_CLIP_HORIZONTAL, 2);
110         OUT_RING(chan, w << 16 | x);
111         OUT_RING(chan, h << 16 | y);
112
113         /* Messing with surf3d invalidates the engine state. */
114         context_dirty_i(ctx, TEX_ENV, 0);
115         context_dirty_i(ctx, TEX_ENV, 1);
116         context_dirty_i(ctx, TEX_OBJ, 0);
117         context_dirty_i(ctx, TEX_OBJ, 1);
118         context_dirty(ctx, CONTROL);
119         context_dirty(ctx, BLEND);
120 }