DRI2: report swap events correctly in direct rendered case
[mesa.git] / src / gallium / drivers / nv40 / nv40_state_fb.c
1 #include "nv40_context.h"
2 #include "nouveau/nouveau_util.h"
3
4 static struct pipe_buffer *
5 nv40_do_surface_buffer(struct pipe_surface *surface)
6 {
7 struct nv40_miptree *mt = (struct nv40_miptree *)surface->texture;
8 return mt->buffer;
9 }
10
11 #define nv40_surface_buffer(ps) nouveau_bo(nv40_do_surface_buffer(ps))
12
13 static boolean
14 nv40_state_framebuffer_validate(struct nv40_context *nv40)
15 {
16 struct nouveau_channel *chan = nv40->screen->base.channel;
17 struct nouveau_grobj *curie = nv40->screen->curie;
18 struct pipe_framebuffer_state *fb = &nv40->framebuffer;
19 struct nv04_surface *rt[4], *zeta;
20 uint32_t rt_enable, rt_format;
21 int i, colour_format = 0, zeta_format = 0;
22 struct nouveau_stateobj *so = so_new(18, 24, 10);
23 unsigned rt_flags = NOUVEAU_BO_RDWR | NOUVEAU_BO_VRAM;
24 unsigned w = fb->width;
25 unsigned h = fb->height;
26
27 rt_enable = 0;
28 for (i = 0; i < fb->nr_cbufs; i++) {
29 if (colour_format) {
30 assert(colour_format == fb->cbufs[i]->format);
31 } else {
32 colour_format = fb->cbufs[i]->format;
33 rt_enable |= (NV40TCL_RT_ENABLE_COLOR0 << i);
34 rt[i] = (struct nv04_surface *)fb->cbufs[i];
35 }
36 }
37
38 if (rt_enable & (NV40TCL_RT_ENABLE_COLOR1 | NV40TCL_RT_ENABLE_COLOR2 |
39 NV40TCL_RT_ENABLE_COLOR3))
40 rt_enable |= NV40TCL_RT_ENABLE_MRT;
41
42 if (fb->zsbuf) {
43 zeta_format = fb->zsbuf->format;
44 zeta = (struct nv04_surface *)fb->zsbuf;
45 }
46
47 if (!(rt[0]->base.texture->tex_usage & NOUVEAU_TEXTURE_USAGE_LINEAR)) {
48 assert(!(fb->width & (fb->width - 1)) && !(fb->height & (fb->height - 1)));
49 for (i = 1; i < fb->nr_cbufs; i++)
50 assert(!(rt[i]->base.texture->tex_usage & NOUVEAU_TEXTURE_USAGE_LINEAR));
51
52 rt_format = NV40TCL_RT_FORMAT_TYPE_SWIZZLED |
53 log2i(fb->width) << NV40TCL_RT_FORMAT_LOG2_WIDTH_SHIFT |
54 log2i(fb->height) << NV40TCL_RT_FORMAT_LOG2_HEIGHT_SHIFT;
55 }
56 else
57 rt_format = NV40TCL_RT_FORMAT_TYPE_LINEAR;
58
59 switch (colour_format) {
60 case PIPE_FORMAT_B8G8R8X8_UNORM:
61 rt_format |= NV40TCL_RT_FORMAT_COLOR_X8R8G8B8;
62 break;
63 case PIPE_FORMAT_B8G8R8A8_UNORM:
64 case 0:
65 rt_format |= NV40TCL_RT_FORMAT_COLOR_A8R8G8B8;
66 break;
67 case PIPE_FORMAT_B5G6R5_UNORM:
68 rt_format |= NV40TCL_RT_FORMAT_COLOR_R5G6B5;
69 break;
70 default:
71 assert(0);
72 }
73
74 switch (zeta_format) {
75 case PIPE_FORMAT_Z16_UNORM:
76 rt_format |= NV40TCL_RT_FORMAT_ZETA_Z16;
77 break;
78 case PIPE_FORMAT_S8Z24_UNORM:
79 case PIPE_FORMAT_X8Z24_UNORM:
80 case 0:
81 rt_format |= NV40TCL_RT_FORMAT_ZETA_Z24S8;
82 break;
83 default:
84 assert(0);
85 }
86
87 if (rt_enable & NV40TCL_RT_ENABLE_COLOR0) {
88 so_method(so, curie, NV40TCL_DMA_COLOR0, 1);
89 so_reloc (so, nv40_surface_buffer(&rt[0]->base), 0,
90 rt_flags | NOUVEAU_BO_OR,
91 chan->vram->handle, chan->gart->handle);
92 so_method(so, curie, NV40TCL_COLOR0_PITCH, 2);
93 so_data (so, rt[0]->pitch);
94 so_reloc (so, nv40_surface_buffer(&rt[0]->base),
95 rt[0]->base.offset, rt_flags | NOUVEAU_BO_LOW,
96 0, 0);
97 }
98
99 if (rt_enable & NV40TCL_RT_ENABLE_COLOR1) {
100 so_method(so, curie, NV40TCL_DMA_COLOR1, 1);
101 so_reloc (so, nv40_surface_buffer(&rt[1]->base), 0,
102 rt_flags | NOUVEAU_BO_OR,
103 chan->vram->handle, chan->gart->handle);
104 so_method(so, curie, NV40TCL_COLOR1_OFFSET, 2);
105 so_reloc (so, nv40_surface_buffer(&rt[1]->base),
106 rt[1]->base.offset, rt_flags | NOUVEAU_BO_LOW,
107 0, 0);
108 so_data (so, rt[1]->pitch);
109 }
110
111 if (rt_enable & NV40TCL_RT_ENABLE_COLOR2) {
112 so_method(so, curie, NV40TCL_DMA_COLOR2, 1);
113 so_reloc (so, nv40_surface_buffer(&rt[2]->base), 0,
114 rt_flags | NOUVEAU_BO_OR,
115 chan->vram->handle, chan->gart->handle);
116 so_method(so, curie, NV40TCL_COLOR2_OFFSET, 1);
117 so_reloc (so, nv40_surface_buffer(&rt[2]->base),
118 rt[2]->base.offset, rt_flags | NOUVEAU_BO_LOW,
119 0, 0);
120 so_method(so, curie, NV40TCL_COLOR2_PITCH, 1);
121 so_data (so, rt[2]->pitch);
122 }
123
124 if (rt_enable & NV40TCL_RT_ENABLE_COLOR3) {
125 so_method(so, curie, NV40TCL_DMA_COLOR3, 1);
126 so_reloc (so, nv40_surface_buffer(&rt[3]->base), 0,
127 rt_flags | NOUVEAU_BO_OR,
128 chan->vram->handle, chan->gart->handle);
129 so_method(so, curie, NV40TCL_COLOR3_OFFSET, 1);
130 so_reloc (so, nv40_surface_buffer(&rt[3]->base),
131 rt[3]->base.offset, rt_flags | NOUVEAU_BO_LOW,
132 0, 0);
133 so_method(so, curie, NV40TCL_COLOR3_PITCH, 1);
134 so_data (so, rt[3]->pitch);
135 }
136
137 if (zeta_format) {
138 so_method(so, curie, NV40TCL_DMA_ZETA, 1);
139 so_reloc (so, nv40_surface_buffer(&zeta->base), 0,
140 rt_flags | NOUVEAU_BO_OR,
141 chan->vram->handle, chan->gart->handle);
142 so_method(so, curie, NV40TCL_ZETA_OFFSET, 1);
143 so_reloc (so, nv40_surface_buffer(&zeta->base),
144 zeta->base.offset, rt_flags | NOUVEAU_BO_LOW, 0, 0);
145 so_method(so, curie, NV40TCL_ZETA_PITCH, 1);
146 so_data (so, zeta->pitch);
147 }
148
149 so_method(so, curie, NV40TCL_RT_ENABLE, 1);
150 so_data (so, rt_enable);
151 so_method(so, curie, NV40TCL_RT_HORIZ, 3);
152 so_data (so, (w << 16) | 0);
153 so_data (so, (h << 16) | 0);
154 so_data (so, rt_format);
155 so_method(so, curie, NV40TCL_VIEWPORT_HORIZ, 2);
156 so_data (so, (w << 16) | 0);
157 so_data (so, (h << 16) | 0);
158 so_method(so, curie, NV40TCL_VIEWPORT_CLIP_HORIZ(0), 2);
159 so_data (so, ((w - 1) << 16) | 0);
160 so_data (so, ((h - 1) << 16) | 0);
161 so_method(so, curie, 0x1d88, 1);
162 so_data (so, (1 << 12) | h);
163
164 so_ref(so, &nv40->state.hw[NV40_STATE_FB]);
165 so_ref(NULL, &so);
166 return TRUE;
167 }
168
169 struct nv40_state_entry nv40_state_framebuffer = {
170 .validate = nv40_state_framebuffer_validate,
171 .dirty = {
172 .pipe = NV40_NEW_FB,
173 .hw = NV40_STATE_FB
174 }
175 };