23525509d14388192d8762d8ff71acaa34f14854
[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 *) ctx->DrawBuffer->_ColorDrawBuffers[0];
181 if (!nrb)
182 return;
183
184 nouveau_cliprects_renderbuffer_set(nmesa, nrb);
185
186 /* Viewport depends on window size/position, nouveauCalcViewport
187 * will take care of calling the hw-specific WindowMoved
188 */
189 ctx->Driver.Viewport(ctx, ctx->Viewport.X, ctx->Viewport.Y,
190 ctx->Viewport.Width, ctx->Viewport.Height);
191 /* Scissor depends on window position */
192 ctx->Driver.Scissor(ctx, ctx->Scissor.X, ctx->Scissor.Y,
193 ctx->Scissor.Width, ctx->Scissor.Height);
194 }
195
196 GLboolean
197 nouveau_build_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
198 {
199 nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
200 nouveau_renderbuffer_t *color[MAX_DRAW_BUFFERS];
201 nouveau_renderbuffer_t *depth;
202
203 _mesa_update_framebuffer(ctx);
204 _mesa_update_draw_buffer_bounds(ctx);
205
206 color[0] = (nouveau_renderbuffer_t *) fb->_ColorDrawBuffers[0];
207 if (fb->_DepthBuffer && fb->_DepthBuffer->Wrapped)
208 depth = (nouveau_renderbuffer_t *) fb->_DepthBuffer->Wrapped;
209 else
210 depth = (nouveau_renderbuffer_t *) fb->_DepthBuffer;
211
212 if (!nmesa->hw_func.BindBuffers(nmesa, 1, color, depth))
213 return GL_FALSE;
214 nouveau_window_moved(ctx);
215
216 return GL_TRUE;
217 }
218
219 static void
220 nouveauDrawBuffer(GLcontext *ctx, GLenum buffer)
221 {
222 nouveau_build_framebuffer(ctx, ctx->DrawBuffer);
223 }
224
225 static struct gl_framebuffer *
226 nouveauNewFramebuffer(GLcontext *ctx, GLuint name)
227 {
228 return _mesa_new_framebuffer(ctx, name);
229 }
230
231 static struct gl_renderbuffer *
232 nouveauNewRenderbuffer(GLcontext *ctx, GLuint name)
233 {
234 nouveau_renderbuffer_t *nrb;
235
236 nrb = CALLOC_STRUCT(nouveau_renderbuffer);
237 if (!nrb)
238 return NULL;
239
240 _mesa_init_renderbuffer(&nrb->mesa, name);
241
242 nrb->mesa.AllocStorage = nouveau_renderbuffer_storage;
243 nrb->mesa.Delete = nouveau_renderbuffer_delete;
244 return &nrb->mesa;
245 }
246
247 static void
248 nouveauBindFramebuffer(GLcontext *ctx, GLenum target,
249 struct gl_framebuffer *fb,
250 struct gl_framebuffer *fbread)
251 {
252 if (target == GL_FRAMEBUFFER_EXT || target == GL_DRAW_FRAMEBUFFER_EXT) {
253 nouveau_build_framebuffer(ctx, fb);
254 }
255 }
256
257 static void
258 nouveauFramebufferRenderbuffer(GLcontext *ctx, struct gl_framebuffer *fb,
259 GLenum attachment, struct gl_renderbuffer *rb)
260 {
261 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
262 nouveau_build_framebuffer(ctx, fb);
263 }
264
265 static void
266 nouveauRenderTexture(GLcontext * ctx, struct gl_framebuffer *fb,
267 struct gl_renderbuffer_attachment *att)
268 {
269 }
270
271 static void
272 nouveauFinishRenderTexture(GLcontext * ctx,
273 struct gl_renderbuffer_attachment *att)
274 {
275 }
276
277 void
278 nouveauInitBufferFuncs(struct dd_function_table *func)
279 {
280 func->DrawBuffer = nouveauDrawBuffer;
281
282 func->NewFramebuffer = nouveauNewFramebuffer;
283 func->NewRenderbuffer = nouveauNewRenderbuffer;
284 func->BindFramebuffer = nouveauBindFramebuffer;
285 func->FramebufferRenderbuffer = nouveauFramebufferRenderbuffer;
286 func->RenderTexture = nouveauRenderTexture;
287 func->FinishRenderTexture = nouveauFinishRenderTexture;
288 }
289