nouveau: Add support for ARB_sampler_objects
[mesa.git] / src / mesa / drivers / dri / nouveau / nouveau_screen.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_texture.h"
31 #include "nv04_driver.h"
32 #include "nv10_driver.h"
33 #include "nv20_driver.h"
34
35 #include "main/framebuffer.h"
36 #include "main/fbobject.h"
37 #include "main/renderbuffer.h"
38 #include "swrast/s_renderbuffer.h"
39
40 static const __DRIextension *nouveau_screen_extensions[];
41
42 static void
43 nouveau_destroy_screen(__DRIscreen *dri_screen);
44
45 static const __DRIconfig **
46 nouveau_get_configs(void)
47 {
48 __DRIconfig **configs = NULL;
49 int i;
50
51 const uint8_t depth_bits[] = { 0, 16, 24, 24 };
52 const uint8_t stencil_bits[] = { 0, 0, 0, 8 };
53 const uint8_t msaa_samples[] = { 0 };
54
55 const struct {
56 GLenum format;
57 GLenum type;
58 } fb_formats[] = {
59 { GL_RGB , GL_UNSIGNED_SHORT_5_6_5 },
60 { GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV },
61 { GL_BGR , GL_UNSIGNED_INT_8_8_8_8_REV },
62 };
63
64 const GLenum back_buffer_modes[] = {
65 GLX_NONE, GLX_SWAP_UNDEFINED_OML
66 };
67
68 for (i = 0; i < Elements(fb_formats); i++) {
69 __DRIconfig **config;
70
71 config = driCreateConfigs(fb_formats[i].format,
72 fb_formats[i].type,
73 depth_bits, stencil_bits,
74 Elements(depth_bits),
75 back_buffer_modes,
76 Elements(back_buffer_modes),
77 msaa_samples,
78 Elements(msaa_samples),
79 GL_TRUE);
80 assert(config);
81
82 configs = configs ? driConcatConfigs(configs, config)
83 : config;
84 }
85
86 return (const __DRIconfig **)configs;
87 }
88
89 static const __DRIconfig **
90 nouveau_init_screen2(__DRIscreen *dri_screen)
91 {
92 const __DRIconfig **configs;
93 struct nouveau_screen *screen;
94 int ret;
95
96 /* Allocate the screen. */
97 screen = CALLOC_STRUCT(nouveau_screen);
98 if (!screen)
99 return NULL;
100
101 dri_screen->driverPrivate = screen;
102 dri_screen->extensions = nouveau_screen_extensions;
103 screen->dri_screen = dri_screen;
104
105 /* Open the DRM device. */
106 ret = nouveau_device_wrap(dri_screen->fd, 0, &screen->device);
107 if (ret) {
108 nouveau_error("Error opening the DRM device.\n");
109 goto fail;
110 }
111
112 /* Choose the card specific function pointers. */
113 switch (screen->device->chipset & 0xf0) {
114 case 0x00:
115 screen->driver = &nv04_driver;
116 break;
117 case 0x10:
118 screen->driver = &nv10_driver;
119 break;
120 case 0x20:
121 screen->driver = &nv20_driver;
122 break;
123 default:
124 assert(0);
125 }
126
127 configs = nouveau_get_configs();
128 if (!configs)
129 goto fail;
130
131 return configs;
132 fail:
133 nouveau_destroy_screen(dri_screen);
134 return NULL;
135
136 }
137
138 static void
139 nouveau_destroy_screen(__DRIscreen *dri_screen)
140 {
141 struct nouveau_screen *screen = dri_screen->driverPrivate;
142
143 if (!screen)
144 return;
145
146 nouveau_device_del(&screen->device);
147
148 FREE(screen);
149 dri_screen->driverPrivate = NULL;
150 }
151
152 static GLboolean
153 nouveau_create_buffer(__DRIscreen *dri_screen,
154 __DRIdrawable *drawable,
155 const struct gl_config *visual,
156 GLboolean is_pixmap)
157 {
158 struct gl_renderbuffer *rb;
159 struct gl_framebuffer *fb;
160 GLenum color_format;
161
162 if (is_pixmap)
163 return GL_FALSE; /* not implemented */
164
165 if (visual->redBits == 5)
166 color_format = GL_RGB5;
167 else if (visual->alphaBits == 0)
168 color_format = GL_RGB8;
169 else
170 color_format = GL_RGBA8;
171
172 fb = nouveau_framebuffer_dri_new(visual);
173 if (!fb)
174 return GL_FALSE;
175
176 /* Front buffer. */
177 rb = nouveau_renderbuffer_dri_new(color_format, drawable);
178 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, rb);
179
180 /* Back buffer */
181 if (visual->doubleBufferMode) {
182 rb = nouveau_renderbuffer_dri_new(color_format, drawable);
183 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, rb);
184 }
185
186 /* Depth/stencil buffer. */
187 if (visual->depthBits == 24 && visual->stencilBits == 8) {
188 rb = nouveau_renderbuffer_dri_new(GL_DEPTH24_STENCIL8_EXT, drawable);
189 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, rb);
190 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, rb);
191
192 } else if (visual->depthBits == 24) {
193 rb = nouveau_renderbuffer_dri_new(GL_DEPTH_COMPONENT24, drawable);
194 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, rb);
195
196 } else if (visual->depthBits == 16) {
197 rb = nouveau_renderbuffer_dri_new(GL_DEPTH_COMPONENT16, drawable);
198 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, rb);
199 }
200
201 /* Software renderbuffers. */
202 _swrast_add_soft_renderbuffers(fb, GL_FALSE, GL_FALSE, GL_FALSE,
203 visual->accumRedBits > 0,
204 GL_FALSE, GL_FALSE);
205
206 drawable->driverPrivate = fb;
207
208 return GL_TRUE;
209 }
210
211 static void
212 nouveau_destroy_buffer(__DRIdrawable *drawable)
213 {
214 _mesa_reference_framebuffer(
215 (struct gl_framebuffer **)&drawable->driverPrivate, NULL);
216 }
217
218 static void
219 nouveau_drawable_flush(__DRIdrawable *draw)
220 {
221 }
222
223 static const struct __DRI2flushExtensionRec nouveau_flush_extension = {
224 { __DRI2_FLUSH, __DRI2_FLUSH_VERSION },
225 nouveau_drawable_flush,
226 dri2InvalidateDrawable,
227 };
228
229 static const struct __DRItexBufferExtensionRec nouveau_texbuffer_extension = {
230 { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
231 NULL,
232 nouveau_set_texbuffer,
233 };
234
235 static const __DRIextension *nouveau_screen_extensions[] = {
236 &nouveau_flush_extension.base,
237 &nouveau_texbuffer_extension.base,
238 &dri2ConfigQueryExtension.base,
239 NULL
240 };
241
242 const struct __DriverAPIRec driDriverAPI = {
243 .InitScreen = nouveau_init_screen2,
244 .DestroyScreen = nouveau_destroy_screen,
245 .CreateBuffer = nouveau_create_buffer,
246 .DestroyBuffer = nouveau_destroy_buffer,
247 .CreateContext = nouveau_context_create,
248 .DestroyContext = nouveau_context_destroy,
249 .MakeCurrent = nouveau_context_make_current,
250 .UnbindContext = nouveau_context_unbind,
251 };
252
253 /* This is the table of extensions that the loader will dlsym() for. */
254 PUBLIC const __DRIextension *__driDriverExtensions[] = {
255 &driCoreExtension.base,
256 &driDRI2Extension.base,
257 NULL
258 };