st/mesa: unify window-system renderbuffer initialization
[mesa.git] / src / mesa / state_tracker / st_manager.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2010 LunarG Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28 #include "main/mtypes.h"
29 #include "main/extensions.h"
30 #include "main/context.h"
31 #include "main/debug_output.h"
32 #include "main/glthread.h"
33 #include "main/texobj.h"
34 #include "main/teximage.h"
35 #include "main/texstate.h"
36 #include "main/errors.h"
37 #include "main/framebuffer.h"
38 #include "main/fbobject.h"
39 #include "main/renderbuffer.h"
40 #include "main/version.h"
41 #include "util/hash_table.h"
42 #include "st_texture.h"
43
44 #include "st_context.h"
45 #include "st_debug.h"
46 #include "st_extensions.h"
47 #include "st_format.h"
48 #include "st_cb_bitmap.h"
49 #include "st_cb_fbo.h"
50 #include "st_cb_flush.h"
51 #include "st_manager.h"
52 #include "st_sampler_view.h"
53
54 #include "state_tracker/st_gl_api.h"
55
56 #include "pipe/p_context.h"
57 #include "pipe/p_screen.h"
58 #include "util/u_format.h"
59 #include "util/u_helpers.h"
60 #include "util/u_pointer.h"
61 #include "util/u_inlines.h"
62 #include "util/u_atomic.h"
63 #include "util/u_surface.h"
64 #include "util/list.h"
65
66 struct hash_table;
67 struct st_manager_private
68 {
69 struct hash_table *stfbi_ht; /* framebuffer iface objects hash table */
70 mtx_t st_mutex;
71 };
72
73
74 /**
75 * Map an attachment to a buffer index.
76 */
77 static inline gl_buffer_index
78 attachment_to_buffer_index(enum st_attachment_type statt)
79 {
80 gl_buffer_index index;
81
82 switch (statt) {
83 case ST_ATTACHMENT_FRONT_LEFT:
84 index = BUFFER_FRONT_LEFT;
85 break;
86 case ST_ATTACHMENT_BACK_LEFT:
87 index = BUFFER_BACK_LEFT;
88 break;
89 case ST_ATTACHMENT_FRONT_RIGHT:
90 index = BUFFER_FRONT_RIGHT;
91 break;
92 case ST_ATTACHMENT_BACK_RIGHT:
93 index = BUFFER_BACK_RIGHT;
94 break;
95 case ST_ATTACHMENT_DEPTH_STENCIL:
96 index = BUFFER_DEPTH;
97 break;
98 case ST_ATTACHMENT_ACCUM:
99 index = BUFFER_ACCUM;
100 break;
101 case ST_ATTACHMENT_SAMPLE:
102 default:
103 index = BUFFER_COUNT;
104 break;
105 }
106
107 return index;
108 }
109
110
111 /**
112 * Map a buffer index to an attachment.
113 */
114 static inline enum st_attachment_type
115 buffer_index_to_attachment(gl_buffer_index index)
116 {
117 enum st_attachment_type statt;
118
119 switch (index) {
120 case BUFFER_FRONT_LEFT:
121 statt = ST_ATTACHMENT_FRONT_LEFT;
122 break;
123 case BUFFER_BACK_LEFT:
124 statt = ST_ATTACHMENT_BACK_LEFT;
125 break;
126 case BUFFER_FRONT_RIGHT:
127 statt = ST_ATTACHMENT_FRONT_RIGHT;
128 break;
129 case BUFFER_BACK_RIGHT:
130 statt = ST_ATTACHMENT_BACK_RIGHT;
131 break;
132 case BUFFER_DEPTH:
133 statt = ST_ATTACHMENT_DEPTH_STENCIL;
134 break;
135 case BUFFER_ACCUM:
136 statt = ST_ATTACHMENT_ACCUM;
137 break;
138 default:
139 statt = ST_ATTACHMENT_INVALID;
140 break;
141 }
142
143 return statt;
144 }
145
146
147 /**
148 * Make sure a context picks up the latest cached state of the
149 * drawables it binds to.
150 */
151 static void
152 st_context_validate(struct st_context *st,
153 struct st_framebuffer *stdraw,
154 struct st_framebuffer *stread)
155 {
156 if (stdraw && stdraw->stamp != st->draw_stamp) {
157 st->dirty |= ST_NEW_FRAMEBUFFER;
158 _mesa_resize_framebuffer(st->ctx, &stdraw->Base,
159 stdraw->Base.Width,
160 stdraw->Base.Height);
161 st->draw_stamp = stdraw->stamp;
162 }
163
164 if (stread && stread->stamp != st->read_stamp) {
165 if (stread != stdraw) {
166 st->dirty |= ST_NEW_FRAMEBUFFER;
167 _mesa_resize_framebuffer(st->ctx, &stread->Base,
168 stread->Base.Width,
169 stread->Base.Height);
170 }
171 st->read_stamp = stread->stamp;
172 }
173 }
174
175
176 void
177 st_set_ws_renderbuffer_surface(struct st_renderbuffer *strb,
178 struct pipe_surface *surf)
179 {
180 pipe_surface_reference(&strb->surface_srgb, NULL);
181 pipe_surface_reference(&strb->surface_linear, NULL);
182
183 if (util_format_is_srgb(surf->format))
184 pipe_surface_reference(&strb->surface_srgb, surf);
185 else
186 pipe_surface_reference(&strb->surface_linear, surf);
187
188 strb->surface = surf; /* just assign, don't ref */
189 pipe_resource_reference(&strb->texture, surf->texture);
190
191 strb->Base.Width = surf->width;
192 strb->Base.Height = surf->height;
193 }
194
195
196 /**
197 * Validate a framebuffer to make sure up-to-date pipe_textures are used.
198 * The context is only used for creating pipe surfaces and for calling
199 * _mesa_resize_framebuffer().
200 * (That should probably be rethought, since those surfaces become
201 * drawable state, not context state, and can be freed by another pipe
202 * context).
203 */
204 static void
205 st_framebuffer_validate(struct st_framebuffer *stfb,
206 struct st_context *st)
207 {
208 struct pipe_resource *textures[ST_ATTACHMENT_COUNT];
209 uint width, height;
210 unsigned i;
211 boolean changed = FALSE;
212 int32_t new_stamp;
213
214 new_stamp = p_atomic_read(&stfb->iface->stamp);
215 if (stfb->iface_stamp == new_stamp)
216 return;
217
218 memset(textures, 0, stfb->num_statts * sizeof(textures[0]));
219
220 /* validate the fb */
221 do {
222 if (!stfb->iface->validate(&st->iface, stfb->iface, stfb->statts,
223 stfb->num_statts, textures))
224 return;
225
226 stfb->iface_stamp = new_stamp;
227 new_stamp = p_atomic_read(&stfb->iface->stamp);
228 } while(stfb->iface_stamp != new_stamp);
229
230 width = stfb->Base.Width;
231 height = stfb->Base.Height;
232
233 for (i = 0; i < stfb->num_statts; i++) {
234 struct st_renderbuffer *strb;
235 struct pipe_surface *ps, surf_tmpl;
236 gl_buffer_index idx;
237
238 if (!textures[i])
239 continue;
240
241 idx = attachment_to_buffer_index(stfb->statts[i]);
242 if (idx >= BUFFER_COUNT) {
243 pipe_resource_reference(&textures[i], NULL);
244 continue;
245 }
246
247 strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
248 assert(strb);
249 if (strb->texture == textures[i]) {
250 pipe_resource_reference(&textures[i], NULL);
251 continue;
252 }
253
254 u_surface_default_template(&surf_tmpl, textures[i]);
255 ps = st->pipe->create_surface(st->pipe, textures[i], &surf_tmpl);
256 if (ps) {
257 st_set_ws_renderbuffer_surface(strb, ps);
258 pipe_surface_reference(&ps, NULL);
259
260 changed = TRUE;
261
262 width = strb->Base.Width;
263 height = strb->Base.Height;
264 }
265
266 pipe_resource_reference(&textures[i], NULL);
267 }
268
269 if (changed) {
270 ++stfb->stamp;
271 _mesa_resize_framebuffer(st->ctx, &stfb->Base, width, height);
272 }
273 }
274
275
276 /**
277 * Update the attachments to validate by looping the existing renderbuffers.
278 */
279 static void
280 st_framebuffer_update_attachments(struct st_framebuffer *stfb)
281 {
282 gl_buffer_index idx;
283
284 stfb->num_statts = 0;
285 for (idx = 0; idx < BUFFER_COUNT; idx++) {
286 struct st_renderbuffer *strb;
287 enum st_attachment_type statt;
288
289 strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
290 if (!strb || strb->software)
291 continue;
292
293 statt = buffer_index_to_attachment(idx);
294 if (statt != ST_ATTACHMENT_INVALID &&
295 st_visual_have_buffers(stfb->iface->visual, 1 << statt))
296 stfb->statts[stfb->num_statts++] = statt;
297 }
298 stfb->stamp++;
299 }
300
301
302 /**
303 * Add a renderbuffer to the framebuffer. The framebuffer is one that
304 * corresponds to a window and is not a user-created FBO.
305 */
306 static boolean
307 st_framebuffer_add_renderbuffer(struct st_framebuffer *stfb,
308 gl_buffer_index idx)
309 {
310 struct gl_renderbuffer *rb;
311 enum pipe_format format;
312 boolean sw;
313
314 assert(_mesa_is_winsys_fbo(&stfb->Base));
315
316 /* do not distinguish depth/stencil buffers */
317 if (idx == BUFFER_STENCIL)
318 idx = BUFFER_DEPTH;
319
320 switch (idx) {
321 case BUFFER_DEPTH:
322 format = stfb->iface->visual->depth_stencil_format;
323 sw = FALSE;
324 break;
325 case BUFFER_ACCUM:
326 format = stfb->iface->visual->accum_format;
327 sw = TRUE;
328 break;
329 default:
330 format = stfb->iface->visual->color_format;
331 if (stfb->Base.Visual.sRGBCapable)
332 format = util_format_srgb(format);
333 sw = FALSE;
334 break;
335 }
336
337 if (format == PIPE_FORMAT_NONE)
338 return FALSE;
339
340 rb = st_new_renderbuffer_fb(format, stfb->iface->visual->samples, sw);
341 if (!rb)
342 return FALSE;
343
344 if (idx != BUFFER_DEPTH) {
345 _mesa_attach_and_own_rb(&stfb->Base, idx, rb);
346 return TRUE;
347 }
348
349 bool rb_ownership_taken = false;
350 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 0)) {
351 _mesa_attach_and_own_rb(&stfb->Base, BUFFER_DEPTH, rb);
352 rb_ownership_taken = true;
353 }
354
355 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 1)) {
356 if (rb_ownership_taken)
357 _mesa_attach_and_reference_rb(&stfb->Base, BUFFER_STENCIL, rb);
358 else
359 _mesa_attach_and_own_rb(&stfb->Base, BUFFER_STENCIL, rb);
360 }
361
362 return TRUE;
363 }
364
365
366 /**
367 * Intialize a struct gl_config from a visual.
368 */
369 static void
370 st_visual_to_context_mode(const struct st_visual *visual,
371 struct gl_config *mode)
372 {
373 memset(mode, 0, sizeof(*mode));
374
375 if (st_visual_have_buffers(visual, ST_ATTACHMENT_BACK_LEFT_MASK))
376 mode->doubleBufferMode = GL_TRUE;
377
378 if (st_visual_have_buffers(visual,
379 ST_ATTACHMENT_FRONT_RIGHT_MASK | ST_ATTACHMENT_BACK_RIGHT_MASK))
380 mode->stereoMode = GL_TRUE;
381
382 if (visual->color_format != PIPE_FORMAT_NONE) {
383 mode->rgbMode = GL_TRUE;
384
385 mode->redBits =
386 util_format_get_component_bits(visual->color_format,
387 UTIL_FORMAT_COLORSPACE_RGB, 0);
388 mode->greenBits =
389 util_format_get_component_bits(visual->color_format,
390 UTIL_FORMAT_COLORSPACE_RGB, 1);
391 mode->blueBits =
392 util_format_get_component_bits(visual->color_format,
393 UTIL_FORMAT_COLORSPACE_RGB, 2);
394 mode->alphaBits =
395 util_format_get_component_bits(visual->color_format,
396 UTIL_FORMAT_COLORSPACE_RGB, 3);
397
398 mode->rgbBits = mode->redBits +
399 mode->greenBits + mode->blueBits + mode->alphaBits;
400 mode->sRGBCapable = util_format_is_srgb(visual->color_format);
401 }
402
403 if (visual->depth_stencil_format != PIPE_FORMAT_NONE) {
404 mode->depthBits =
405 util_format_get_component_bits(visual->depth_stencil_format,
406 UTIL_FORMAT_COLORSPACE_ZS, 0);
407 mode->stencilBits =
408 util_format_get_component_bits(visual->depth_stencil_format,
409 UTIL_FORMAT_COLORSPACE_ZS, 1);
410
411 mode->haveDepthBuffer = mode->depthBits > 0;
412 mode->haveStencilBuffer = mode->stencilBits > 0;
413 }
414
415 if (visual->accum_format != PIPE_FORMAT_NONE) {
416 mode->haveAccumBuffer = GL_TRUE;
417
418 mode->accumRedBits =
419 util_format_get_component_bits(visual->accum_format,
420 UTIL_FORMAT_COLORSPACE_RGB, 0);
421 mode->accumGreenBits =
422 util_format_get_component_bits(visual->accum_format,
423 UTIL_FORMAT_COLORSPACE_RGB, 1);
424 mode->accumBlueBits =
425 util_format_get_component_bits(visual->accum_format,
426 UTIL_FORMAT_COLORSPACE_RGB, 2);
427 mode->accumAlphaBits =
428 util_format_get_component_bits(visual->accum_format,
429 UTIL_FORMAT_COLORSPACE_RGB, 3);
430 }
431
432 if (visual->samples > 1) {
433 mode->sampleBuffers = 1;
434 mode->samples = visual->samples;
435 }
436 }
437
438
439 /**
440 * Create a framebuffer from a manager interface.
441 */
442 static struct st_framebuffer *
443 st_framebuffer_create(struct st_context *st,
444 struct st_framebuffer_iface *stfbi)
445 {
446 struct st_framebuffer *stfb;
447 struct gl_config mode;
448 gl_buffer_index idx;
449
450 if (!stfbi)
451 return NULL;
452
453 stfb = CALLOC_STRUCT(st_framebuffer);
454 if (!stfb)
455 return NULL;
456
457 st_visual_to_context_mode(stfbi->visual, &mode);
458
459 /*
460 * For desktop GL, sRGB framebuffer write is controlled by both the
461 * capability of the framebuffer and GL_FRAMEBUFFER_SRGB. We should
462 * advertise the capability when the pipe driver (and core Mesa) supports
463 * it so that applications can enable sRGB write when they want to.
464 *
465 * This is not to be confused with GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB. When
466 * the attribute is GLX_TRUE, it tells the st manager to pick a color
467 * format such that util_format_srgb(visual->color_format) can be supported
468 * by the pipe driver. We still need to advertise the capability here.
469 *
470 * For GLES, however, sRGB framebuffer write is controlled only by the
471 * capability of the framebuffer. There is GL_EXT_sRGB_write_control to
472 * give applications the control back, but sRGB write is still enabled by
473 * default. To avoid unexpected results, we should not advertise the
474 * capability. This could change when we add support for
475 * EGL_KHR_gl_colorspace.
476 */
477 if (_mesa_is_desktop_gl(st->ctx)) {
478 struct pipe_screen *screen = st->pipe->screen;
479 const enum pipe_format srgb_format =
480 util_format_srgb(stfbi->visual->color_format);
481
482 if (srgb_format != PIPE_FORMAT_NONE &&
483 st_pipe_format_to_mesa_format(srgb_format) != MESA_FORMAT_NONE &&
484 screen->is_format_supported(screen, srgb_format,
485 PIPE_TEXTURE_2D, stfbi->visual->samples,
486 stfbi->visual->samples,
487 (PIPE_BIND_DISPLAY_TARGET |
488 PIPE_BIND_RENDER_TARGET)))
489 mode.sRGBCapable = GL_TRUE;
490 }
491
492 _mesa_initialize_window_framebuffer(&stfb->Base, &mode);
493
494 stfb->iface = stfbi;
495 stfb->iface_ID = stfbi->ID;
496 stfb->iface_stamp = p_atomic_read(&stfbi->stamp) - 1;
497
498 /* add the color buffer */
499 idx = stfb->Base._ColorDrawBufferIndexes[0];
500 if (!st_framebuffer_add_renderbuffer(stfb, idx)) {
501 free(stfb);
502 return NULL;
503 }
504
505 st_framebuffer_add_renderbuffer(stfb, BUFFER_DEPTH);
506 st_framebuffer_add_renderbuffer(stfb, BUFFER_ACCUM);
507
508 stfb->stamp = 0;
509 st_framebuffer_update_attachments(stfb);
510
511 return stfb;
512 }
513
514
515 /**
516 * Reference a framebuffer.
517 */
518 void
519 st_framebuffer_reference(struct st_framebuffer **ptr,
520 struct st_framebuffer *stfb)
521 {
522 struct gl_framebuffer *fb = &stfb->Base;
523 _mesa_reference_framebuffer((struct gl_framebuffer **) ptr, fb);
524 }
525
526
527 static uint32_t
528 st_framebuffer_iface_hash(const void *key)
529 {
530 return (uintptr_t)key;
531 }
532
533
534 static bool
535 st_framebuffer_iface_equal(const void *a, const void *b)
536 {
537 return (struct st_framebuffer_iface *)a == (struct st_framebuffer_iface *)b;
538 }
539
540
541 static boolean
542 st_framebuffer_iface_lookup(struct st_manager *smapi,
543 const struct st_framebuffer_iface *stfbi)
544 {
545 struct st_manager_private *smPriv =
546 (struct st_manager_private *)smapi->st_manager_private;
547 struct hash_entry *entry;
548
549 assert(smPriv);
550 assert(smPriv->stfbi_ht);
551
552 mtx_lock(&smPriv->st_mutex);
553 entry = _mesa_hash_table_search(smPriv->stfbi_ht, stfbi);
554 mtx_unlock(&smPriv->st_mutex);
555
556 return entry != NULL;
557 }
558
559
560 static boolean
561 st_framebuffer_iface_insert(struct st_manager *smapi,
562 struct st_framebuffer_iface *stfbi)
563 {
564 struct st_manager_private *smPriv =
565 (struct st_manager_private *)smapi->st_manager_private;
566 struct hash_entry *entry;
567
568 assert(smPriv);
569 assert(smPriv->stfbi_ht);
570
571 mtx_lock(&smPriv->st_mutex);
572 entry = _mesa_hash_table_insert(smPriv->stfbi_ht, stfbi, stfbi);
573 mtx_unlock(&smPriv->st_mutex);
574
575 return entry != NULL;
576 }
577
578
579 static void
580 st_framebuffer_iface_remove(struct st_manager *smapi,
581 struct st_framebuffer_iface *stfbi)
582 {
583 struct st_manager_private *smPriv =
584 (struct st_manager_private *)smapi->st_manager_private;
585 struct hash_entry *entry;
586
587 if (!smPriv || !smPriv->stfbi_ht)
588 return;
589
590 mtx_lock(&smPriv->st_mutex);
591 entry = _mesa_hash_table_search(smPriv->stfbi_ht, stfbi);
592 if (!entry)
593 goto unlock;
594
595 _mesa_hash_table_remove(smPriv->stfbi_ht, entry);
596
597 unlock:
598 mtx_unlock(&smPriv->st_mutex);
599 }
600
601
602 /**
603 * The framebuffer interface object is no longer valid.
604 * Remove the object from the framebuffer interface hash table.
605 */
606 static void
607 st_api_destroy_drawable(struct st_api *stapi,
608 struct st_framebuffer_iface *stfbi)
609 {
610 if (!stfbi)
611 return;
612
613 st_framebuffer_iface_remove(stfbi->state_manager, stfbi);
614 }
615
616
617 /**
618 * Purge the winsys buffers list to remove any references to
619 * non-existing framebuffer interface objects.
620 */
621 static void
622 st_framebuffers_purge(struct st_context *st)
623 {
624 struct st_context_iface *st_iface = &st->iface;
625 struct st_manager *smapi = st_iface->state_manager;
626 struct st_framebuffer *stfb, *next;
627
628 assert(smapi);
629
630 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb, next, &st->winsys_buffers, head) {
631 struct st_framebuffer_iface *stfbi = stfb->iface;
632
633 assert(stfbi);
634
635 /**
636 * If the corresponding framebuffer interface object no longer exists,
637 * remove the framebuffer object from the context's winsys buffers list,
638 * and unreference the framebuffer object, so its resources can be
639 * deleted.
640 */
641 if (!st_framebuffer_iface_lookup(smapi, stfbi)) {
642 LIST_DEL(&stfb->head);
643 st_framebuffer_reference(&stfb, NULL);
644 }
645 }
646 }
647
648
649 static void
650 st_context_flush(struct st_context_iface *stctxi, unsigned flags,
651 struct pipe_fence_handle **fence)
652 {
653 struct st_context *st = (struct st_context *) stctxi;
654 unsigned pipe_flags = 0;
655
656 if (flags & ST_FLUSH_END_OF_FRAME)
657 pipe_flags |= PIPE_FLUSH_END_OF_FRAME;
658 if (flags & ST_FLUSH_FENCE_FD)
659 pipe_flags |= PIPE_FLUSH_FENCE_FD;
660
661 FLUSH_VERTICES(st->ctx, 0);
662 FLUSH_CURRENT(st->ctx, 0);
663 st_flush(st, fence, pipe_flags);
664
665 if ((flags & ST_FLUSH_WAIT) && fence && *fence) {
666 st->pipe->screen->fence_finish(st->pipe->screen, NULL, *fence,
667 PIPE_TIMEOUT_INFINITE);
668 st->pipe->screen->fence_reference(st->pipe->screen, fence, NULL);
669 }
670
671 if (flags & ST_FLUSH_FRONT)
672 st_manager_flush_frontbuffer(st);
673
674 /* DRI3 changes the framebuffer after SwapBuffers, but we need to invoke
675 * st_manager_validate_framebuffers to notice that.
676 *
677 * Set gfx_shaders_may_be_dirty to invoke st_validate_state in the next
678 * draw call, which will invoke st_manager_validate_framebuffers, but it
679 * won't dirty states if there is no change.
680 */
681 if (flags & ST_FLUSH_END_OF_FRAME)
682 st->gfx_shaders_may_be_dirty = true;
683 }
684
685 static boolean
686 st_context_teximage(struct st_context_iface *stctxi,
687 enum st_texture_type tex_type,
688 int level, enum pipe_format pipe_format,
689 struct pipe_resource *tex, boolean mipmap)
690 {
691 struct st_context *st = (struct st_context *) stctxi;
692 struct gl_context *ctx = st->ctx;
693 struct gl_texture_object *texObj;
694 struct gl_texture_image *texImage;
695 struct st_texture_object *stObj;
696 struct st_texture_image *stImage;
697 GLenum internalFormat;
698 GLuint width, height, depth;
699 GLenum target;
700
701 switch (tex_type) {
702 case ST_TEXTURE_1D:
703 target = GL_TEXTURE_1D;
704 break;
705 case ST_TEXTURE_2D:
706 target = GL_TEXTURE_2D;
707 break;
708 case ST_TEXTURE_3D:
709 target = GL_TEXTURE_3D;
710 break;
711 case ST_TEXTURE_RECT:
712 target = GL_TEXTURE_RECTANGLE_ARB;
713 break;
714 default:
715 return FALSE;
716 }
717
718 texObj = _mesa_get_current_tex_object(ctx, target);
719
720 _mesa_lock_texture(ctx, texObj);
721
722 stObj = st_texture_object(texObj);
723 /* switch to surface based */
724 if (!stObj->surface_based) {
725 _mesa_clear_texture_object(ctx, texObj, NULL);
726 stObj->surface_based = GL_TRUE;
727 }
728
729 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
730 stImage = st_texture_image(texImage);
731 if (tex) {
732 mesa_format texFormat = st_pipe_format_to_mesa_format(pipe_format);
733
734 if (util_format_has_alpha(tex->format))
735 internalFormat = GL_RGBA;
736 else
737 internalFormat = GL_RGB;
738
739 _mesa_init_teximage_fields(ctx, texImage,
740 tex->width0, tex->height0, 1, 0,
741 internalFormat, texFormat);
742
743 width = tex->width0;
744 height = tex->height0;
745 depth = tex->depth0;
746
747 /* grow the image size until we hit level = 0 */
748 while (level > 0) {
749 if (width != 1)
750 width <<= 1;
751 if (height != 1)
752 height <<= 1;
753 if (depth != 1)
754 depth <<= 1;
755 level--;
756 }
757 }
758 else {
759 _mesa_clear_texture_image(ctx, texImage);
760 width = height = depth = 0;
761 }
762
763 pipe_resource_reference(&stObj->pt, tex);
764 st_texture_release_all_sampler_views(st, stObj);
765 pipe_resource_reference(&stImage->pt, tex);
766 stObj->surface_format = pipe_format;
767
768 stObj->needs_validation = true;
769
770 _mesa_dirty_texobj(ctx, texObj);
771 _mesa_unlock_texture(ctx, texObj);
772
773 return TRUE;
774 }
775
776
777 static void
778 st_context_copy(struct st_context_iface *stctxi,
779 struct st_context_iface *stsrci, unsigned mask)
780 {
781 struct st_context *st = (struct st_context *) stctxi;
782 struct st_context *src = (struct st_context *) stsrci;
783
784 _mesa_copy_context(src->ctx, st->ctx, mask);
785 }
786
787
788 static boolean
789 st_context_share(struct st_context_iface *stctxi,
790 struct st_context_iface *stsrci)
791 {
792 struct st_context *st = (struct st_context *) stctxi;
793 struct st_context *src = (struct st_context *) stsrci;
794
795 return _mesa_share_state(st->ctx, src->ctx);
796 }
797
798
799 static void
800 st_context_destroy(struct st_context_iface *stctxi)
801 {
802 struct st_context *st = (struct st_context *) stctxi;
803 st_destroy_context(st);
804 }
805
806
807 static void
808 st_start_thread(struct st_context_iface *stctxi)
809 {
810 struct st_context *st = (struct st_context *) stctxi;
811
812 _mesa_glthread_init(st->ctx);
813
814 /* Pin all driver threads to one L3 cache for optimal performance
815 * on AMD Zen. This is only done if glthread is enabled.
816 *
817 * If glthread is disabled, st_draw.c re-pins driver threads regularly
818 * based on the location of the app thread.
819 */
820 struct glthread_state *glthread = st->ctx->GLThread;
821 if (glthread && st->pipe->set_context_param) {
822 util_pin_driver_threads_to_random_L3(st->pipe, &glthread->queue.threads[0]);
823 }
824 }
825
826
827 static void
828 st_thread_finish(struct st_context_iface *stctxi)
829 {
830 struct st_context *st = (struct st_context *) stctxi;
831
832 _mesa_glthread_finish(st->ctx);
833 }
834
835
836 static void
837 st_manager_destroy(struct st_manager *smapi)
838 {
839 struct st_manager_private *smPriv = smapi->st_manager_private;
840
841 if (smPriv && smPriv->stfbi_ht) {
842 _mesa_hash_table_destroy(smPriv->stfbi_ht, NULL);
843 mtx_destroy(&smPriv->st_mutex);
844 free(smPriv);
845 smapi->st_manager_private = NULL;
846 }
847 }
848
849
850 static struct st_context_iface *
851 st_api_create_context(struct st_api *stapi, struct st_manager *smapi,
852 const struct st_context_attribs *attribs,
853 enum st_context_error *error,
854 struct st_context_iface *shared_stctxi)
855 {
856 struct st_context *shared_ctx = (struct st_context *) shared_stctxi;
857 struct st_context *st;
858 struct pipe_context *pipe;
859 struct gl_config* mode_ptr;
860 struct gl_config mode;
861 gl_api api;
862 bool no_error = false;
863 unsigned ctx_flags = PIPE_CONTEXT_PREFER_THREADED;
864
865 if (!(stapi->profile_mask & (1 << attribs->profile)))
866 return NULL;
867
868 switch (attribs->profile) {
869 case ST_PROFILE_DEFAULT:
870 api = API_OPENGL_COMPAT;
871 break;
872 case ST_PROFILE_OPENGL_ES1:
873 api = API_OPENGLES;
874 break;
875 case ST_PROFILE_OPENGL_ES2:
876 api = API_OPENGLES2;
877 break;
878 case ST_PROFILE_OPENGL_CORE:
879 api = API_OPENGL_CORE;
880 break;
881 default:
882 *error = ST_CONTEXT_ERROR_BAD_API;
883 return NULL;
884 }
885
886 /* Create a hash table for the framebuffer interface objects
887 * if it has not been created for this st manager.
888 */
889 if (smapi->st_manager_private == NULL) {
890 struct st_manager_private *smPriv;
891
892 smPriv = CALLOC_STRUCT(st_manager_private);
893 mtx_init(&smPriv->st_mutex, mtx_plain);
894 smPriv->stfbi_ht = _mesa_hash_table_create(NULL,
895 st_framebuffer_iface_hash,
896 st_framebuffer_iface_equal);
897 smapi->st_manager_private = smPriv;
898 smapi->destroy = st_manager_destroy;
899 }
900
901 if (attribs->flags & ST_CONTEXT_FLAG_ROBUST_ACCESS)
902 ctx_flags |= PIPE_CONTEXT_ROBUST_BUFFER_ACCESS;
903
904 if (attribs->flags & ST_CONTEXT_FLAG_NO_ERROR)
905 no_error = true;
906
907 if (attribs->flags & ST_CONTEXT_FLAG_LOW_PRIORITY)
908 ctx_flags |= PIPE_CONTEXT_LOW_PRIORITY;
909 else if (attribs->flags & ST_CONTEXT_FLAG_HIGH_PRIORITY)
910 ctx_flags |= PIPE_CONTEXT_HIGH_PRIORITY;
911
912 if (attribs->flags & ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED)
913 ctx_flags |= PIPE_CONTEXT_LOSE_CONTEXT_ON_RESET;
914
915 pipe = smapi->screen->context_create(smapi->screen, NULL, ctx_flags);
916 if (!pipe) {
917 *error = ST_CONTEXT_ERROR_NO_MEMORY;
918 return NULL;
919 }
920
921 st_visual_to_context_mode(&attribs->visual, &mode);
922
923 if (attribs->visual.no_config)
924 mode_ptr = NULL;
925 else
926 mode_ptr = &mode;
927
928 st = st_create_context(api, pipe, mode_ptr, shared_ctx,
929 &attribs->options, no_error);
930 if (!st) {
931 *error = ST_CONTEXT_ERROR_NO_MEMORY;
932 pipe->destroy(pipe);
933 return NULL;
934 }
935
936 if (attribs->flags & ST_CONTEXT_FLAG_DEBUG) {
937 if (!_mesa_set_debug_state_int(st->ctx, GL_DEBUG_OUTPUT, GL_TRUE)) {
938 *error = ST_CONTEXT_ERROR_NO_MEMORY;
939 return NULL;
940 }
941
942 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_DEBUG_BIT;
943 }
944
945 if (st->ctx->Const.ContextFlags & GL_CONTEXT_FLAG_DEBUG_BIT) {
946 st_update_debug_callback(st);
947 }
948
949 if (attribs->flags & ST_CONTEXT_FLAG_FORWARD_COMPATIBLE)
950 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
951 if (attribs->flags & ST_CONTEXT_FLAG_ROBUST_ACCESS) {
952 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB;
953 st->ctx->Const.RobustAccess = GL_TRUE;
954 }
955 if (attribs->flags & ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED) {
956 st->ctx->Const.ResetStrategy = GL_LOSE_CONTEXT_ON_RESET_ARB;
957 st_install_device_reset_callback(st);
958 }
959
960 if (attribs->flags & ST_CONTEXT_FLAG_RELEASE_NONE)
961 st->ctx->Const.ContextReleaseBehavior = GL_NONE;
962
963 /* need to perform version check */
964 if (attribs->major > 1 || attribs->minor > 0) {
965 /* Is the actual version less than the requested version?
966 */
967 if (st->ctx->Version < attribs->major * 10U + attribs->minor) {
968 *error = ST_CONTEXT_ERROR_BAD_VERSION;
969 st_destroy_context(st);
970 return NULL;
971 }
972 }
973
974 st->invalidate_on_gl_viewport =
975 smapi->get_param(smapi, ST_MANAGER_BROKEN_INVALIDATE);
976
977 st->iface.destroy = st_context_destroy;
978 st->iface.flush = st_context_flush;
979 st->iface.teximage = st_context_teximage;
980 st->iface.copy = st_context_copy;
981 st->iface.share = st_context_share;
982 st->iface.start_thread = st_start_thread;
983 st->iface.thread_finish = st_thread_finish;
984 st->iface.st_context_private = (void *) smapi;
985 st->iface.cso_context = st->cso_context;
986 st->iface.pipe = st->pipe;
987 st->iface.state_manager = smapi;
988
989 *error = ST_CONTEXT_SUCCESS;
990 return &st->iface;
991 }
992
993
994 static struct st_context_iface *
995 st_api_get_current(struct st_api *stapi)
996 {
997 GET_CURRENT_CONTEXT(ctx);
998 struct st_context *st = ctx ? ctx->st : NULL;
999
1000 return st ? &st->iface : NULL;
1001 }
1002
1003
1004 static struct st_framebuffer *
1005 st_framebuffer_reuse_or_create(struct st_context *st,
1006 struct gl_framebuffer *fb,
1007 struct st_framebuffer_iface *stfbi)
1008 {
1009 struct st_framebuffer *cur = NULL, *stfb = NULL;
1010
1011 if (!stfbi)
1012 return NULL;
1013
1014 /* Check if there is already a framebuffer object for the specified
1015 * framebuffer interface in this context. If there is one, use it.
1016 */
1017 LIST_FOR_EACH_ENTRY(cur, &st->winsys_buffers, head) {
1018 if (cur->iface_ID == stfbi->ID) {
1019 st_framebuffer_reference(&stfb, cur);
1020 break;
1021 }
1022 }
1023
1024 /* If there is not already a framebuffer object, create one */
1025 if (stfb == NULL) {
1026 cur = st_framebuffer_create(st, stfbi);
1027
1028 if (cur) {
1029 /* add the referenced framebuffer interface object to
1030 * the framebuffer interface object hash table.
1031 */
1032 if (!st_framebuffer_iface_insert(stfbi->state_manager, stfbi)) {
1033 st_framebuffer_reference(&cur, NULL);
1034 return NULL;
1035 }
1036
1037 /* add to the context's winsys buffers list */
1038 LIST_ADD(&cur->head, &st->winsys_buffers);
1039
1040 st_framebuffer_reference(&stfb, cur);
1041 }
1042 }
1043
1044 return stfb;
1045 }
1046
1047
1048 static boolean
1049 st_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
1050 struct st_framebuffer_iface *stdrawi,
1051 struct st_framebuffer_iface *streadi)
1052 {
1053 struct st_context *st = (struct st_context *) stctxi;
1054 struct st_framebuffer *stdraw, *stread;
1055 boolean ret;
1056
1057 if (st) {
1058 /* reuse or create the draw fb */
1059 stdraw = st_framebuffer_reuse_or_create(st,
1060 st->ctx->WinSysDrawBuffer, stdrawi);
1061 if (streadi != stdrawi) {
1062 /* do the same for the read fb */
1063 stread = st_framebuffer_reuse_or_create(st,
1064 st->ctx->WinSysReadBuffer, streadi);
1065 }
1066 else {
1067 stread = NULL;
1068 /* reuse the draw fb for the read fb */
1069 if (stdraw)
1070 st_framebuffer_reference(&stread, stdraw);
1071 }
1072
1073 if (stdraw && stread) {
1074 st_framebuffer_validate(stdraw, st);
1075 if (stread != stdraw)
1076 st_framebuffer_validate(stread, st);
1077
1078 ret = _mesa_make_current(st->ctx, &stdraw->Base, &stread->Base);
1079
1080 st->draw_stamp = stdraw->stamp - 1;
1081 st->read_stamp = stread->stamp - 1;
1082 st_context_validate(st, stdraw, stread);
1083 }
1084 else {
1085 struct gl_framebuffer *incomplete = _mesa_get_incomplete_framebuffer();
1086 ret = _mesa_make_current(st->ctx, incomplete, incomplete);
1087 }
1088
1089 st_framebuffer_reference(&stdraw, NULL);
1090 st_framebuffer_reference(&stread, NULL);
1091
1092 /* Purge the context's winsys_buffers list in case any
1093 * of the referenced drawables no longer exist.
1094 */
1095 st_framebuffers_purge(st);
1096 }
1097 else {
1098 ret = _mesa_make_current(NULL, NULL, NULL);
1099 }
1100
1101 return ret;
1102 }
1103
1104
1105 static void
1106 st_api_destroy(struct st_api *stapi)
1107 {
1108 }
1109
1110
1111 /**
1112 * Flush the front buffer if the current context renders to the front buffer.
1113 */
1114 void
1115 st_manager_flush_frontbuffer(struct st_context *st)
1116 {
1117 struct st_framebuffer *stfb = st_ws_framebuffer(st->ctx->DrawBuffer);
1118 struct st_renderbuffer *strb = NULL;
1119
1120 if (stfb)
1121 strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_FRONT_LEFT].
1122 Renderbuffer);
1123
1124 /* Do we have a front color buffer and has it been drawn to since last
1125 * frontbuffer flush?
1126 */
1127 if (strb && strb->defined) {
1128 stfb->iface->flush_front(&st->iface, stfb->iface,
1129 ST_ATTACHMENT_FRONT_LEFT);
1130 strb->defined = GL_FALSE;
1131
1132 /* Trigger an update of strb->defined on next draw */
1133 st->dirty |= ST_NEW_FB_STATE;
1134 }
1135 }
1136
1137
1138 /**
1139 * Re-validate the framebuffers.
1140 */
1141 void
1142 st_manager_validate_framebuffers(struct st_context *st)
1143 {
1144 struct st_framebuffer *stdraw = st_ws_framebuffer(st->ctx->DrawBuffer);
1145 struct st_framebuffer *stread = st_ws_framebuffer(st->ctx->ReadBuffer);
1146
1147 if (stdraw)
1148 st_framebuffer_validate(stdraw, st);
1149 if (stread && stread != stdraw)
1150 st_framebuffer_validate(stread, st);
1151
1152 st_context_validate(st, stdraw, stread);
1153 }
1154
1155
1156 /**
1157 * Flush any outstanding swapbuffers on the current draw framebuffer.
1158 */
1159 void
1160 st_manager_flush_swapbuffers(void)
1161 {
1162 GET_CURRENT_CONTEXT(ctx);
1163 struct st_context *st = (ctx) ? ctx->st : NULL;
1164 struct st_framebuffer *stfb;
1165
1166 if (!st)
1167 return;
1168
1169 stfb = st_ws_framebuffer(ctx->DrawBuffer);
1170 if (!stfb || !stfb->iface->flush_swapbuffers)
1171 return;
1172
1173 stfb->iface->flush_swapbuffers(&st->iface, stfb->iface);
1174 }
1175
1176
1177 /**
1178 * Add a color renderbuffer on demand. The FBO must correspond to a window,
1179 * not a user-created FBO.
1180 */
1181 boolean
1182 st_manager_add_color_renderbuffer(struct st_context *st,
1183 struct gl_framebuffer *fb,
1184 gl_buffer_index idx)
1185 {
1186 struct st_framebuffer *stfb = st_ws_framebuffer(fb);
1187
1188 /* FBO */
1189 if (!stfb)
1190 return FALSE;
1191
1192 assert(_mesa_is_winsys_fbo(fb));
1193
1194 if (stfb->Base.Attachment[idx].Renderbuffer)
1195 return TRUE;
1196
1197 switch (idx) {
1198 case BUFFER_FRONT_LEFT:
1199 case BUFFER_BACK_LEFT:
1200 case BUFFER_FRONT_RIGHT:
1201 case BUFFER_BACK_RIGHT:
1202 break;
1203 default:
1204 return FALSE;
1205 }
1206
1207 if (!st_framebuffer_add_renderbuffer(stfb, idx))
1208 return FALSE;
1209
1210 st_framebuffer_update_attachments(stfb);
1211
1212 /*
1213 * Force a call to the state tracker manager to validate the
1214 * new renderbuffer. It might be that there is a window system
1215 * renderbuffer available.
1216 */
1217 if (stfb->iface)
1218 stfb->iface_stamp = p_atomic_read(&stfb->iface->stamp) - 1;
1219
1220 st_invalidate_buffers(st);
1221
1222 return TRUE;
1223 }
1224
1225
1226 static unsigned
1227 get_version(struct pipe_screen *screen,
1228 struct st_config_options *options, gl_api api)
1229 {
1230 struct gl_constants consts = {0};
1231 struct gl_extensions extensions = {0};
1232 GLuint version;
1233
1234 if (_mesa_override_gl_version_contextless(&consts, &api, &version)) {
1235 return version;
1236 }
1237
1238 _mesa_init_constants(&consts, api);
1239 _mesa_init_extensions(&extensions);
1240
1241 st_init_limits(screen, &consts, &extensions, api);
1242 st_init_extensions(screen, &consts, &extensions, options, api);
1243
1244 return _mesa_get_version(&extensions, &consts, api);
1245 }
1246
1247
1248 static void
1249 st_api_query_versions(struct st_api *stapi, struct st_manager *sm,
1250 struct st_config_options *options,
1251 int *gl_core_version,
1252 int *gl_compat_version,
1253 int *gl_es1_version,
1254 int *gl_es2_version)
1255 {
1256 *gl_core_version = get_version(sm->screen, options, API_OPENGL_CORE);
1257 *gl_compat_version = get_version(sm->screen, options, API_OPENGL_COMPAT);
1258 *gl_es1_version = get_version(sm->screen, options, API_OPENGLES);
1259 *gl_es2_version = get_version(sm->screen, options, API_OPENGLES2);
1260 }
1261
1262
1263 static const struct st_api st_gl_api = {
1264 .name = "Mesa " PACKAGE_VERSION,
1265 .api = ST_API_OPENGL,
1266 .profile_mask = ST_PROFILE_DEFAULT_MASK |
1267 ST_PROFILE_OPENGL_CORE_MASK |
1268 ST_PROFILE_OPENGL_ES1_MASK |
1269 ST_PROFILE_OPENGL_ES2_MASK |
1270 0,
1271 .feature_mask = ST_API_FEATURE_MS_VISUALS_MASK,
1272 .destroy = st_api_destroy,
1273 .query_versions = st_api_query_versions,
1274 .create_context = st_api_create_context,
1275 .make_current = st_api_make_current,
1276 .get_current = st_api_get_current,
1277 .destroy_drawable = st_api_destroy_drawable,
1278 };
1279
1280
1281 struct st_api *
1282 st_gl_api_create(void)
1283 {
1284 return (struct st_api *) &st_gl_api;
1285 }