Merge branch 'master' into i915-unification
[mesa.git] / src / mesa / drivers / dri / nouveau / nouveau_fbo.c
1 #include "utils.h"
2 #include "framebuffer.h"
3 #include "renderbuffer.h"
4 #include "fbobject.h"
5
6 #include "nouveau_context.h"
7 #include "nouveau_fbo.h"
8 #include "nouveau_fifo.h"
9 #include "nouveau_msg.h"
10 #include "nouveau_object.h"
11 #include "nouveau_reg.h"
12
13 static GLboolean
14 nouveau_renderbuffer_pixelformat(nouveau_renderbuffer_t * nrb,
15 GLenum internalFormat)
16 {
17 nrb->mesa.InternalFormat = internalFormat;
18
19 /*TODO: We probably want to extend this a bit, and maybe make
20 * card-specific?
21 */
22 switch (internalFormat) {
23 case GL_RGBA:
24 case GL_RGBA8:
25 nrb->mesa._BaseFormat = GL_RGBA;
26 nrb->mesa._ActualFormat = GL_RGBA8;
27 nrb->mesa.DataType = GL_UNSIGNED_BYTE;
28 nrb->mesa.RedBits = 8;
29 nrb->mesa.GreenBits = 8;
30 nrb->mesa.BlueBits = 8;
31 nrb->mesa.AlphaBits = 8;
32 nrb->cpp = 4;
33 break;
34 case GL_RGB:
35 case GL_RGB5:
36 nrb->mesa._BaseFormat = GL_RGB;
37 nrb->mesa._ActualFormat = GL_RGB5;
38 nrb->mesa.DataType = GL_UNSIGNED_BYTE;
39 nrb->mesa.RedBits = 5;
40 nrb->mesa.GreenBits = 6;
41 nrb->mesa.BlueBits = 5;
42 nrb->mesa.AlphaBits = 0;
43 nrb->cpp = 2;
44 break;
45 case GL_DEPTH_COMPONENT16:
46 nrb->mesa._BaseFormat = GL_DEPTH_COMPONENT;
47 nrb->mesa._ActualFormat = GL_DEPTH_COMPONENT16;
48 nrb->mesa.DataType = GL_UNSIGNED_SHORT;
49 nrb->mesa.DepthBits = 16;
50 nrb->cpp = 2;
51 break;
52 case GL_DEPTH_COMPONENT24:
53 nrb->mesa._BaseFormat = GL_DEPTH_COMPONENT;
54 nrb->mesa._ActualFormat = GL_DEPTH24_STENCIL8_EXT;
55 nrb->mesa.DataType = GL_UNSIGNED_INT_24_8_EXT;
56 nrb->mesa.DepthBits = 24;
57 nrb->cpp = 4;
58 break;
59 case GL_STENCIL_INDEX8_EXT:
60 nrb->mesa._BaseFormat = GL_STENCIL_INDEX;
61 nrb->mesa._ActualFormat = GL_DEPTH24_STENCIL8_EXT;
62 nrb->mesa.DataType = GL_UNSIGNED_INT_24_8_EXT;
63 nrb->mesa.StencilBits = 8;
64 nrb->cpp = 4;
65 break;
66 case GL_DEPTH24_STENCIL8_EXT:
67 nrb->mesa._BaseFormat = GL_DEPTH_STENCIL_EXT;
68 nrb->mesa._ActualFormat = GL_DEPTH24_STENCIL8_EXT;
69 nrb->mesa.DataType = GL_UNSIGNED_INT_24_8_EXT;
70 nrb->mesa.DepthBits = 24;
71 nrb->mesa.StencilBits = 8;
72 nrb->cpp = 4;
73 break;
74 default:
75 return GL_FALSE;
76 break;
77 }
78
79 return GL_TRUE;
80 }
81
82 static GLboolean
83 nouveau_renderbuffer_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
84 GLenum internalFormat,
85 GLuint width, GLuint height)
86 {
87 nouveau_renderbuffer_t *nrb = (nouveau_renderbuffer_t *) rb;
88
89 if (!nouveau_renderbuffer_pixelformat(nrb, internalFormat)) {
90 fprintf(stderr, "%s: unknown internalFormat\n", __func__);
91 return GL_FALSE;
92 }
93
94 /* If this buffer isn't statically alloc'd, we may need to ask the
95 * drm for more memory */
96 if (rb->Width != width || rb->Height != height) {
97 GLuint pitch;
98
99 /* align pitches to 64 bytes */
100 pitch = ((width * nrb->cpp) + 63) & ~63;
101
102 if (nrb->mem)
103 nouveau_mem_free(ctx, nrb->mem);
104 nrb->mem = nouveau_mem_alloc(ctx,
105 NOUVEAU_MEM_FB |
106 NOUVEAU_MEM_MAPPED,
107 pitch * height, 0);
108 if (!nrb->mem)
109 return GL_FALSE;
110
111 /* update nouveau_renderbuffer info */
112 nrb->offset = nouveau_mem_gpu_offset_get(ctx, nrb->mem);
113 nrb->pitch = pitch;
114 }
115
116 rb->Width = width;
117 rb->Height = height;
118 rb->InternalFormat = internalFormat;
119
120 nouveauSpanSetFunctions(nrb);
121
122 return GL_TRUE;
123 }
124
125 static void
126 nouveau_renderbuffer_delete(struct gl_renderbuffer *rb)
127 {
128 GET_CURRENT_CONTEXT(ctx);
129 nouveau_renderbuffer_t *nrb = (nouveau_renderbuffer_t *) rb;
130
131 if (nrb->mem)
132 nouveau_mem_free(ctx, nrb->mem);
133 FREE(nrb);
134 }
135
136 nouveau_renderbuffer_t *
137 nouveau_renderbuffer_new(GLenum internalFormat)
138 {
139 nouveau_renderbuffer_t *nrb;
140
141 nrb = CALLOC_STRUCT(nouveau_renderbuffer);
142 if (!nrb)
143 return NULL;
144
145 _mesa_init_renderbuffer(&nrb->mesa, 0);
146
147 if (!nouveau_renderbuffer_pixelformat(nrb, internalFormat)) {
148 fprintf(stderr, "%s: unknown internalFormat\n", __func__);
149 return GL_FALSE;
150 }
151
152 nrb->mesa.AllocStorage = nouveau_renderbuffer_storage;
153 nrb->mesa.Delete = nouveau_renderbuffer_delete;
154
155 return nrb;
156 }
157
158 static void
159 nouveau_cliprects_renderbuffer_set(nouveauContextPtr nmesa,
160 nouveau_renderbuffer_t * nrb)
161 {
162 nmesa->numClipRects = 1;
163 nmesa->pClipRects = &nmesa->osClipRect;
164 nmesa->osClipRect.x1 = 0;
165 nmesa->osClipRect.y1 = 0;
166 nmesa->osClipRect.x2 = nrb->mesa.Width;
167 nmesa->osClipRect.y2 = nrb->mesa.Height;
168 nmesa->drawX = 0;
169 nmesa->drawY = 0;
170 nmesa->drawW = nrb->mesa.Width;
171 nmesa->drawH = nrb->mesa.Height;
172 }
173
174 void
175 nouveau_window_moved(GLcontext * ctx)
176 {
177 nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
178 nouveau_renderbuffer_t *nrb;
179
180 nrb = (nouveau_renderbuffer_t *)
181 ctx->DrawBuffer->_ColorDrawBuffers[0][0];
182 if (!nrb)
183 return;
184
185 nouveau_cliprects_renderbuffer_set(nmesa, nrb);
186
187 /* Viewport depends on window size/position, nouveauCalcViewport
188 * will take care of calling the hw-specific WindowMoved
189 */
190 ctx->Driver.Viewport(ctx, ctx->Viewport.X, ctx->Viewport.Y,
191 ctx->Viewport.Width, ctx->Viewport.Height);
192 /* Scissor depends on window position */
193 ctx->Driver.Scissor(ctx, ctx->Scissor.X, ctx->Scissor.Y,
194 ctx->Scissor.Width, ctx->Scissor.Height);
195 }
196
197 GLboolean
198 nouveau_build_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
199 {
200 nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
201 nouveau_renderbuffer_t *color[MAX_DRAW_BUFFERS];
202 nouveau_renderbuffer_t *depth;
203
204 _mesa_update_framebuffer(ctx);
205 _mesa_update_draw_buffer_bounds(ctx);
206
207 color[0] = (nouveau_renderbuffer_t *) fb->_ColorDrawBuffers[0][0];
208 if (fb->_DepthBuffer && fb->_DepthBuffer->Wrapped)
209 depth = (nouveau_renderbuffer_t *) fb->_DepthBuffer->Wrapped;
210 else
211 depth = (nouveau_renderbuffer_t *) fb->_DepthBuffer;
212
213 if (!nmesa->hw_func.BindBuffers(nmesa, 1, color, depth))
214 return GL_FALSE;
215 nouveau_window_moved(ctx);
216
217 return GL_TRUE;
218 }
219
220 static void
221 nouveauDrawBuffer(GLcontext *ctx, GLenum buffer)
222 {
223 nouveau_build_framebuffer(ctx, ctx->DrawBuffer);
224 }
225
226 static struct gl_framebuffer *
227 nouveauNewFramebuffer(GLcontext *ctx, GLuint name)
228 {
229 return _mesa_new_framebuffer(ctx, name);
230 }
231
232 static struct gl_renderbuffer *
233 nouveauNewRenderbuffer(GLcontext *ctx, GLuint name)
234 {
235 nouveau_renderbuffer_t *nrb;
236
237 nrb = CALLOC_STRUCT(nouveau_renderbuffer);
238 if (!nrb)
239 return NULL;
240
241 _mesa_init_renderbuffer(&nrb->mesa, name);
242
243 nrb->mesa.AllocStorage = nouveau_renderbuffer_storage;
244 nrb->mesa.Delete = nouveau_renderbuffer_delete;
245 return &nrb->mesa;
246 }
247
248 static void
249 nouveauBindFramebuffer(GLcontext *ctx, GLenum target,
250 struct gl_framebuffer *fb,
251 struct gl_framebuffer *fbread)
252 {
253 if (target == GL_FRAMEBUFFER_EXT || target == GL_DRAW_FRAMEBUFFER_EXT) {
254 nouveau_build_framebuffer(ctx, fb);
255 }
256 }
257
258 static void
259 nouveauFramebufferRenderbuffer(GLcontext *ctx, struct gl_framebuffer *fb,
260 GLenum attachment, struct gl_renderbuffer *rb)
261 {
262 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
263 nouveau_build_framebuffer(ctx, fb);
264 }
265
266 static void
267 nouveauRenderTexture(GLcontext * ctx, struct gl_framebuffer *fb,
268 struct gl_renderbuffer_attachment *att)
269 {
270 }
271
272 static void
273 nouveauFinishRenderTexture(GLcontext * ctx,
274 struct gl_renderbuffer_attachment *att)
275 {
276 }
277
278 void
279 nouveauInitBufferFuncs(struct dd_function_table *func)
280 {
281 func->DrawBuffer = nouveauDrawBuffer;
282
283 func->NewFramebuffer = nouveauNewFramebuffer;
284 func->NewRenderbuffer = nouveauNewRenderbuffer;
285 func->BindFramebuffer = nouveauBindFramebuffer;
286 func->FramebufferRenderbuffer = nouveauFramebufferRenderbuffer;
287 func->RenderTexture = nouveauRenderTexture;
288 func->FinishRenderTexture = nouveauFinishRenderTexture;
289 }
290