gallium: add st_context_iface::flush_resource to call FLUSH_VERTICES
[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/format/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 simple_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 bool 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 bool
307 st_framebuffer_add_renderbuffer(struct st_framebuffer *stfb,
308 gl_buffer_index idx, bool prefer_srgb)
309 {
310 struct gl_renderbuffer *rb;
311 enum pipe_format format;
312 bool 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 (prefer_srgb)
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->redBits =
384 util_format_get_component_bits(visual->color_format,
385 UTIL_FORMAT_COLORSPACE_RGB, 0);
386 mode->greenBits =
387 util_format_get_component_bits(visual->color_format,
388 UTIL_FORMAT_COLORSPACE_RGB, 1);
389 mode->blueBits =
390 util_format_get_component_bits(visual->color_format,
391 UTIL_FORMAT_COLORSPACE_RGB, 2);
392 mode->alphaBits =
393 util_format_get_component_bits(visual->color_format,
394 UTIL_FORMAT_COLORSPACE_RGB, 3);
395
396 mode->rgbBits = mode->redBits +
397 mode->greenBits + mode->blueBits + mode->alphaBits;
398 mode->sRGBCapable = util_format_is_srgb(visual->color_format);
399 }
400
401 if (visual->depth_stencil_format != PIPE_FORMAT_NONE) {
402 mode->depthBits =
403 util_format_get_component_bits(visual->depth_stencil_format,
404 UTIL_FORMAT_COLORSPACE_ZS, 0);
405 mode->stencilBits =
406 util_format_get_component_bits(visual->depth_stencil_format,
407 UTIL_FORMAT_COLORSPACE_ZS, 1);
408 }
409
410 if (visual->accum_format != PIPE_FORMAT_NONE) {
411 mode->accumRedBits =
412 util_format_get_component_bits(visual->accum_format,
413 UTIL_FORMAT_COLORSPACE_RGB, 0);
414 mode->accumGreenBits =
415 util_format_get_component_bits(visual->accum_format,
416 UTIL_FORMAT_COLORSPACE_RGB, 1);
417 mode->accumBlueBits =
418 util_format_get_component_bits(visual->accum_format,
419 UTIL_FORMAT_COLORSPACE_RGB, 2);
420 mode->accumAlphaBits =
421 util_format_get_component_bits(visual->accum_format,
422 UTIL_FORMAT_COLORSPACE_RGB, 3);
423 }
424
425 if (visual->samples > 1) {
426 mode->sampleBuffers = 1;
427 mode->samples = visual->samples;
428 }
429 }
430
431
432 /**
433 * Create a framebuffer from a manager interface.
434 */
435 static struct st_framebuffer *
436 st_framebuffer_create(struct st_context *st,
437 struct st_framebuffer_iface *stfbi)
438 {
439 struct st_framebuffer *stfb;
440 struct gl_config mode;
441 gl_buffer_index idx;
442 bool prefer_srgb = false;
443
444 if (!stfbi)
445 return NULL;
446
447 stfb = CALLOC_STRUCT(st_framebuffer);
448 if (!stfb)
449 return NULL;
450
451 st_visual_to_context_mode(stfbi->visual, &mode);
452
453 /*
454 * For desktop GL, sRGB framebuffer write is controlled by both the
455 * capability of the framebuffer and GL_FRAMEBUFFER_SRGB. We should
456 * advertise the capability when the pipe driver (and core Mesa) supports
457 * it so that applications can enable sRGB write when they want to.
458 *
459 * This is not to be confused with GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB. When
460 * the attribute is GLX_TRUE, it tells the st manager to pick a color
461 * format such that util_format_srgb(visual->color_format) can be supported
462 * by the pipe driver. We still need to advertise the capability here.
463 *
464 * For GLES, however, sRGB framebuffer write is initially only controlled
465 * by the capability of the framebuffer, with GL_EXT_sRGB_write_control
466 * control is given back to the applications, but GL_FRAMEBUFFER_SRGB is
467 * still enabled by default since this is the behaviour when
468 * EXT_sRGB_write_control is not available. Since GL_EXT_sRGB_write_control
469 * brings GLES on par with desktop GLs EXT_framebuffer_sRGB, in mesa this
470 * is also expressed by using the same extension flag
471 */
472 if (_mesa_has_EXT_framebuffer_sRGB(st->ctx)) {
473 struct pipe_screen *screen = st->pipe->screen;
474 const enum pipe_format srgb_format =
475 util_format_srgb(stfbi->visual->color_format);
476
477 if (srgb_format != PIPE_FORMAT_NONE &&
478 st_pipe_format_to_mesa_format(srgb_format) != MESA_FORMAT_NONE &&
479 screen->is_format_supported(screen, srgb_format,
480 PIPE_TEXTURE_2D, stfbi->visual->samples,
481 stfbi->visual->samples,
482 (PIPE_BIND_DISPLAY_TARGET |
483 PIPE_BIND_RENDER_TARGET))) {
484 mode.sRGBCapable = GL_TRUE;
485 /* Since GL_FRAMEBUFFER_SRGB is enabled by default on GLES we must not
486 * create renderbuffers with an sRGB format derived from the
487 * visual->color_format, but we still want sRGB for desktop GL.
488 */
489 prefer_srgb = _mesa_is_desktop_gl(st->ctx);
490 }
491 }
492
493 _mesa_initialize_window_framebuffer(&stfb->Base, &mode);
494
495 stfb->iface = stfbi;
496 stfb->iface_ID = stfbi->ID;
497 stfb->iface_stamp = p_atomic_read(&stfbi->stamp) - 1;
498
499 /* add the color buffer */
500 idx = stfb->Base._ColorDrawBufferIndexes[0];
501 if (!st_framebuffer_add_renderbuffer(stfb, idx, prefer_srgb)) {
502 free(stfb);
503 return NULL;
504 }
505
506 st_framebuffer_add_renderbuffer(stfb, BUFFER_DEPTH, false);
507 st_framebuffer_add_renderbuffer(stfb, BUFFER_ACCUM, false);
508
509 stfb->stamp = 0;
510 st_framebuffer_update_attachments(stfb);
511
512 return stfb;
513 }
514
515
516 /**
517 * Reference a framebuffer.
518 */
519 void
520 st_framebuffer_reference(struct st_framebuffer **ptr,
521 struct st_framebuffer *stfb)
522 {
523 struct gl_framebuffer *fb = stfb ? &stfb->Base : NULL;
524 _mesa_reference_framebuffer((struct gl_framebuffer **) ptr, fb);
525 }
526
527
528 static uint32_t
529 st_framebuffer_iface_hash(const void *key)
530 {
531 return (uintptr_t)key;
532 }
533
534
535 static bool
536 st_framebuffer_iface_equal(const void *a, const void *b)
537 {
538 return (struct st_framebuffer_iface *)a == (struct st_framebuffer_iface *)b;
539 }
540
541
542 static bool
543 st_framebuffer_iface_lookup(struct st_manager *smapi,
544 const struct st_framebuffer_iface *stfbi)
545 {
546 struct st_manager_private *smPriv =
547 (struct st_manager_private *)smapi->st_manager_private;
548 struct hash_entry *entry;
549
550 assert(smPriv);
551 assert(smPriv->stfbi_ht);
552
553 simple_mtx_lock(&smPriv->st_mutex);
554 entry = _mesa_hash_table_search(smPriv->stfbi_ht, stfbi);
555 simple_mtx_unlock(&smPriv->st_mutex);
556
557 return entry != NULL;
558 }
559
560
561 static bool
562 st_framebuffer_iface_insert(struct st_manager *smapi,
563 struct st_framebuffer_iface *stfbi)
564 {
565 struct st_manager_private *smPriv =
566 (struct st_manager_private *)smapi->st_manager_private;
567 struct hash_entry *entry;
568
569 assert(smPriv);
570 assert(smPriv->stfbi_ht);
571
572 simple_mtx_lock(&smPriv->st_mutex);
573 entry = _mesa_hash_table_insert(smPriv->stfbi_ht, stfbi, stfbi);
574 simple_mtx_unlock(&smPriv->st_mutex);
575
576 return entry != NULL;
577 }
578
579
580 static void
581 st_framebuffer_iface_remove(struct st_manager *smapi,
582 struct st_framebuffer_iface *stfbi)
583 {
584 struct st_manager_private *smPriv =
585 (struct st_manager_private *)smapi->st_manager_private;
586 struct hash_entry *entry;
587
588 if (!smPriv || !smPriv->stfbi_ht)
589 return;
590
591 simple_mtx_lock(&smPriv->st_mutex);
592 entry = _mesa_hash_table_search(smPriv->stfbi_ht, stfbi);
593 if (!entry)
594 goto unlock;
595
596 _mesa_hash_table_remove(smPriv->stfbi_ht, entry);
597
598 unlock:
599 simple_mtx_unlock(&smPriv->st_mutex);
600 }
601
602
603 /**
604 * The framebuffer interface object is no longer valid.
605 * Remove the object from the framebuffer interface hash table.
606 */
607 static void
608 st_api_destroy_drawable(struct st_api *stapi,
609 struct st_framebuffer_iface *stfbi)
610 {
611 if (!stfbi)
612 return;
613
614 st_framebuffer_iface_remove(stfbi->state_manager, stfbi);
615 }
616
617
618 /**
619 * Purge the winsys buffers list to remove any references to
620 * non-existing framebuffer interface objects.
621 */
622 static void
623 st_framebuffers_purge(struct st_context *st)
624 {
625 struct st_context_iface *st_iface = &st->iface;
626 struct st_manager *smapi = st_iface->state_manager;
627 struct st_framebuffer *stfb, *next;
628
629 assert(smapi);
630
631 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb, next, &st->winsys_buffers, head) {
632 struct st_framebuffer_iface *stfbi = stfb->iface;
633
634 assert(stfbi);
635
636 /**
637 * If the corresponding framebuffer interface object no longer exists,
638 * remove the framebuffer object from the context's winsys buffers list,
639 * and unreference the framebuffer object, so its resources can be
640 * deleted.
641 */
642 if (!st_framebuffer_iface_lookup(smapi, stfbi)) {
643 list_del(&stfb->head);
644 st_framebuffer_reference(&stfb, NULL);
645 }
646 }
647 }
648
649
650 static void
651 st_context_flush(struct st_context_iface *stctxi, unsigned flags,
652 struct pipe_fence_handle **fence,
653 void (*before_flush_cb) (void*),
654 void* args)
655 {
656 struct st_context *st = (struct st_context *) stctxi;
657 unsigned pipe_flags = 0;
658
659 if (flags & ST_FLUSH_END_OF_FRAME)
660 pipe_flags |= PIPE_FLUSH_END_OF_FRAME;
661 if (flags & ST_FLUSH_FENCE_FD)
662 pipe_flags |= PIPE_FLUSH_FENCE_FD;
663
664 FLUSH_VERTICES(st->ctx, 0);
665 FLUSH_CURRENT(st->ctx, 0);
666 /* Notify the caller that we're ready to flush */
667 if (before_flush_cb)
668 before_flush_cb(args);
669 st_flush(st, fence, pipe_flags);
670
671 if ((flags & ST_FLUSH_WAIT) && fence && *fence) {
672 st->pipe->screen->fence_finish(st->pipe->screen, NULL, *fence,
673 PIPE_TIMEOUT_INFINITE);
674 st->pipe->screen->fence_reference(st->pipe->screen, fence, NULL);
675 }
676
677 if (flags & ST_FLUSH_FRONT)
678 st_manager_flush_frontbuffer(st);
679
680 /* DRI3 changes the framebuffer after SwapBuffers, but we need to invoke
681 * st_manager_validate_framebuffers to notice that.
682 *
683 * Set gfx_shaders_may_be_dirty to invoke st_validate_state in the next
684 * draw call, which will invoke st_manager_validate_framebuffers, but it
685 * won't dirty states if there is no change.
686 */
687 if (flags & ST_FLUSH_END_OF_FRAME)
688 st->gfx_shaders_may_be_dirty = true;
689 }
690
691 static void
692 st_context_flush_resource(struct st_context_iface *stctxi,
693 struct pipe_resource *resource)
694 {
695 struct st_context *st = (struct st_context *) stctxi;
696 struct pipe_context *pipe = st->pipe;
697
698 FLUSH_VERTICES(st->ctx, 0);
699 FLUSH_CURRENT(st->ctx, 0);
700
701 pipe->flush_resource(pipe, resource);
702 }
703
704 static bool
705 st_context_teximage(struct st_context_iface *stctxi,
706 enum st_texture_type tex_type,
707 int level, enum pipe_format pipe_format,
708 struct pipe_resource *tex, bool mipmap)
709 {
710 struct st_context *st = (struct st_context *) stctxi;
711 struct gl_context *ctx = st->ctx;
712 struct gl_texture_object *texObj;
713 struct gl_texture_image *texImage;
714 struct st_texture_object *stObj;
715 struct st_texture_image *stImage;
716 GLenum internalFormat;
717 GLuint width, height, depth;
718 GLenum target;
719
720 switch (tex_type) {
721 case ST_TEXTURE_1D:
722 target = GL_TEXTURE_1D;
723 break;
724 case ST_TEXTURE_2D:
725 target = GL_TEXTURE_2D;
726 break;
727 case ST_TEXTURE_3D:
728 target = GL_TEXTURE_3D;
729 break;
730 case ST_TEXTURE_RECT:
731 target = GL_TEXTURE_RECTANGLE_ARB;
732 break;
733 default:
734 return FALSE;
735 }
736
737 texObj = _mesa_get_current_tex_object(ctx, target);
738
739 _mesa_lock_texture(ctx, texObj);
740
741 stObj = st_texture_object(texObj);
742 /* switch to surface based */
743 if (!stObj->surface_based) {
744 _mesa_clear_texture_object(ctx, texObj, NULL);
745 stObj->surface_based = GL_TRUE;
746 }
747
748 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
749 stImage = st_texture_image(texImage);
750 if (tex) {
751 mesa_format texFormat = st_pipe_format_to_mesa_format(pipe_format);
752
753 if (util_format_has_alpha(tex->format))
754 internalFormat = GL_RGBA;
755 else
756 internalFormat = GL_RGB;
757
758 _mesa_init_teximage_fields(ctx, texImage,
759 tex->width0, tex->height0, 1, 0,
760 internalFormat, texFormat);
761
762 width = tex->width0;
763 height = tex->height0;
764 depth = tex->depth0;
765
766 /* grow the image size until we hit level = 0 */
767 while (level > 0) {
768 if (width != 1)
769 width <<= 1;
770 if (height != 1)
771 height <<= 1;
772 if (depth != 1)
773 depth <<= 1;
774 level--;
775 }
776 }
777 else {
778 _mesa_clear_texture_image(ctx, texImage);
779 width = height = depth = 0;
780 }
781
782 pipe_resource_reference(&stObj->pt, tex);
783 st_texture_release_all_sampler_views(st, stObj);
784 pipe_resource_reference(&stImage->pt, tex);
785 stObj->surface_format = pipe_format;
786
787 stObj->needs_validation = true;
788
789 _mesa_dirty_texobj(ctx, texObj);
790 _mesa_unlock_texture(ctx, texObj);
791
792 return true;
793 }
794
795
796 static void
797 st_context_copy(struct st_context_iface *stctxi,
798 struct st_context_iface *stsrci, unsigned mask)
799 {
800 struct st_context *st = (struct st_context *) stctxi;
801 struct st_context *src = (struct st_context *) stsrci;
802
803 _mesa_copy_context(src->ctx, st->ctx, mask);
804 }
805
806
807 static bool
808 st_context_share(struct st_context_iface *stctxi,
809 struct st_context_iface *stsrci)
810 {
811 struct st_context *st = (struct st_context *) stctxi;
812 struct st_context *src = (struct st_context *) stsrci;
813
814 return _mesa_share_state(st->ctx, src->ctx);
815 }
816
817
818 static void
819 st_context_destroy(struct st_context_iface *stctxi)
820 {
821 struct st_context *st = (struct st_context *) stctxi;
822 st_destroy_context(st);
823 }
824
825
826 static void
827 st_start_thread(struct st_context_iface *stctxi)
828 {
829 struct st_context *st = (struct st_context *) stctxi;
830
831 _mesa_glthread_init(st->ctx);
832
833 /* Pin all driver threads to one L3 cache for optimal performance
834 * on AMD Zen. This is only done if glthread is enabled.
835 *
836 * If glthread is disabled, st_draw.c re-pins driver threads regularly
837 * based on the location of the app thread.
838 */
839 struct glthread_state *glthread = st->ctx->GLThread;
840 if (glthread && st->pipe->set_context_param) {
841 util_pin_driver_threads_to_random_L3(st->pipe, &glthread->queue.threads[0]);
842 }
843 }
844
845
846 static void
847 st_thread_finish(struct st_context_iface *stctxi)
848 {
849 struct st_context *st = (struct st_context *) stctxi;
850
851 _mesa_glthread_finish(st->ctx);
852 }
853
854
855 static void
856 st_manager_destroy(struct st_manager *smapi)
857 {
858 struct st_manager_private *smPriv = smapi->st_manager_private;
859
860 if (smPriv && smPriv->stfbi_ht) {
861 _mesa_hash_table_destroy(smPriv->stfbi_ht, NULL);
862 simple_mtx_destroy(&smPriv->st_mutex);
863 free(smPriv);
864 smapi->st_manager_private = NULL;
865 }
866 }
867
868
869 static struct st_context_iface *
870 st_api_create_context(struct st_api *stapi, struct st_manager *smapi,
871 const struct st_context_attribs *attribs,
872 enum st_context_error *error,
873 struct st_context_iface *shared_stctxi)
874 {
875 struct st_context *shared_ctx = (struct st_context *) shared_stctxi;
876 struct st_context *st;
877 struct pipe_context *pipe;
878 struct gl_config* mode_ptr;
879 struct gl_config mode;
880 gl_api api;
881 bool no_error = false;
882 unsigned ctx_flags = PIPE_CONTEXT_PREFER_THREADED;
883
884 if (!(stapi->profile_mask & (1 << attribs->profile)))
885 return NULL;
886
887 switch (attribs->profile) {
888 case ST_PROFILE_DEFAULT:
889 api = API_OPENGL_COMPAT;
890 break;
891 case ST_PROFILE_OPENGL_ES1:
892 api = API_OPENGLES;
893 break;
894 case ST_PROFILE_OPENGL_ES2:
895 api = API_OPENGLES2;
896 break;
897 case ST_PROFILE_OPENGL_CORE:
898 api = API_OPENGL_CORE;
899 break;
900 default:
901 *error = ST_CONTEXT_ERROR_BAD_API;
902 return NULL;
903 }
904
905 /* Create a hash table for the framebuffer interface objects
906 * if it has not been created for this st manager.
907 */
908 if (smapi->st_manager_private == NULL) {
909 struct st_manager_private *smPriv;
910
911 smPriv = CALLOC_STRUCT(st_manager_private);
912 simple_mtx_init(&smPriv->st_mutex, mtx_plain);
913 smPriv->stfbi_ht = _mesa_hash_table_create(NULL,
914 st_framebuffer_iface_hash,
915 st_framebuffer_iface_equal);
916 smapi->st_manager_private = smPriv;
917 smapi->destroy = st_manager_destroy;
918 }
919
920 if (attribs->flags & ST_CONTEXT_FLAG_ROBUST_ACCESS)
921 ctx_flags |= PIPE_CONTEXT_ROBUST_BUFFER_ACCESS;
922
923 if (attribs->flags & ST_CONTEXT_FLAG_NO_ERROR)
924 no_error = true;
925
926 if (attribs->flags & ST_CONTEXT_FLAG_LOW_PRIORITY)
927 ctx_flags |= PIPE_CONTEXT_LOW_PRIORITY;
928 else if (attribs->flags & ST_CONTEXT_FLAG_HIGH_PRIORITY)
929 ctx_flags |= PIPE_CONTEXT_HIGH_PRIORITY;
930
931 if (attribs->flags & ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED)
932 ctx_flags |= PIPE_CONTEXT_LOSE_CONTEXT_ON_RESET;
933
934 pipe = smapi->screen->context_create(smapi->screen, NULL, ctx_flags);
935 if (!pipe) {
936 *error = ST_CONTEXT_ERROR_NO_MEMORY;
937 return NULL;
938 }
939
940 st_visual_to_context_mode(&attribs->visual, &mode);
941
942 if (attribs->visual.no_config)
943 mode_ptr = NULL;
944 else
945 mode_ptr = &mode;
946
947 st = st_create_context(api, pipe, mode_ptr, shared_ctx,
948 &attribs->options, no_error);
949 if (!st) {
950 *error = ST_CONTEXT_ERROR_NO_MEMORY;
951 pipe->destroy(pipe);
952 return NULL;
953 }
954
955 if (attribs->flags & ST_CONTEXT_FLAG_DEBUG) {
956 if (!_mesa_set_debug_state_int(st->ctx, GL_DEBUG_OUTPUT, GL_TRUE)) {
957 *error = ST_CONTEXT_ERROR_NO_MEMORY;
958 return NULL;
959 }
960
961 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_DEBUG_BIT;
962 }
963
964 if (st->ctx->Const.ContextFlags & GL_CONTEXT_FLAG_DEBUG_BIT) {
965 st_update_debug_callback(st);
966 }
967
968 if (attribs->flags & ST_CONTEXT_FLAG_FORWARD_COMPATIBLE)
969 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
970 if (attribs->flags & ST_CONTEXT_FLAG_ROBUST_ACCESS) {
971 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB;
972 st->ctx->Const.RobustAccess = GL_TRUE;
973 }
974 if (attribs->flags & ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED) {
975 st->ctx->Const.ResetStrategy = GL_LOSE_CONTEXT_ON_RESET_ARB;
976 st_install_device_reset_callback(st);
977 }
978
979 if (attribs->flags & ST_CONTEXT_FLAG_RELEASE_NONE)
980 st->ctx->Const.ContextReleaseBehavior = GL_NONE;
981
982 /* need to perform version check */
983 if (attribs->major > 1 || attribs->minor > 0) {
984 /* Is the actual version less than the requested version?
985 */
986 if (st->ctx->Version < attribs->major * 10U + attribs->minor) {
987 *error = ST_CONTEXT_ERROR_BAD_VERSION;
988 st_destroy_context(st);
989 return NULL;
990 }
991 }
992
993 st->invalidate_on_gl_viewport =
994 smapi->get_param(smapi, ST_MANAGER_BROKEN_INVALIDATE);
995
996 st->iface.destroy = st_context_destroy;
997 st->iface.flush = st_context_flush;
998 st->iface.flush_resource = st_context_flush_resource;
999 st->iface.teximage = st_context_teximage;
1000 st->iface.copy = st_context_copy;
1001 st->iface.share = st_context_share;
1002 st->iface.start_thread = st_start_thread;
1003 st->iface.thread_finish = st_thread_finish;
1004 st->iface.st_context_private = (void *) smapi;
1005 st->iface.cso_context = st->cso_context;
1006 st->iface.pipe = st->pipe;
1007 st->iface.state_manager = smapi;
1008
1009 *error = ST_CONTEXT_SUCCESS;
1010 return &st->iface;
1011 }
1012
1013
1014 static struct st_context_iface *
1015 st_api_get_current(struct st_api *stapi)
1016 {
1017 GET_CURRENT_CONTEXT(ctx);
1018 struct st_context *st = ctx ? ctx->st : NULL;
1019
1020 return st ? &st->iface : NULL;
1021 }
1022
1023
1024 static struct st_framebuffer *
1025 st_framebuffer_reuse_or_create(struct st_context *st,
1026 struct gl_framebuffer *fb,
1027 struct st_framebuffer_iface *stfbi)
1028 {
1029 struct st_framebuffer *cur = NULL, *stfb = NULL;
1030
1031 if (!stfbi)
1032 return NULL;
1033
1034 /* Check if there is already a framebuffer object for the specified
1035 * framebuffer interface in this context. If there is one, use it.
1036 */
1037 LIST_FOR_EACH_ENTRY(cur, &st->winsys_buffers, head) {
1038 if (cur->iface_ID == stfbi->ID) {
1039 st_framebuffer_reference(&stfb, cur);
1040 break;
1041 }
1042 }
1043
1044 /* If there is not already a framebuffer object, create one */
1045 if (stfb == NULL) {
1046 cur = st_framebuffer_create(st, stfbi);
1047
1048 if (cur) {
1049 /* add the referenced framebuffer interface object to
1050 * the framebuffer interface object hash table.
1051 */
1052 if (!st_framebuffer_iface_insert(stfbi->state_manager, stfbi)) {
1053 st_framebuffer_reference(&cur, NULL);
1054 return NULL;
1055 }
1056
1057 /* add to the context's winsys buffers list */
1058 list_add(&cur->head, &st->winsys_buffers);
1059
1060 st_framebuffer_reference(&stfb, cur);
1061 }
1062 }
1063
1064 return stfb;
1065 }
1066
1067
1068 static bool
1069 st_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
1070 struct st_framebuffer_iface *stdrawi,
1071 struct st_framebuffer_iface *streadi)
1072 {
1073 struct st_context *st = (struct st_context *) stctxi;
1074 struct st_framebuffer *stdraw, *stread;
1075 bool ret;
1076
1077 if (st) {
1078 /* reuse or create the draw fb */
1079 stdraw = st_framebuffer_reuse_or_create(st,
1080 st->ctx->WinSysDrawBuffer, stdrawi);
1081 if (streadi != stdrawi) {
1082 /* do the same for the read fb */
1083 stread = st_framebuffer_reuse_or_create(st,
1084 st->ctx->WinSysReadBuffer, streadi);
1085 }
1086 else {
1087 stread = NULL;
1088 /* reuse the draw fb for the read fb */
1089 if (stdraw)
1090 st_framebuffer_reference(&stread, stdraw);
1091 }
1092
1093 if (stdraw && stread) {
1094 st_framebuffer_validate(stdraw, st);
1095 if (stread != stdraw)
1096 st_framebuffer_validate(stread, st);
1097
1098 ret = _mesa_make_current(st->ctx, &stdraw->Base, &stread->Base);
1099
1100 st->draw_stamp = stdraw->stamp - 1;
1101 st->read_stamp = stread->stamp - 1;
1102 st_context_validate(st, stdraw, stread);
1103 }
1104 else {
1105 struct gl_framebuffer *incomplete = _mesa_get_incomplete_framebuffer();
1106 ret = _mesa_make_current(st->ctx, incomplete, incomplete);
1107 }
1108
1109 st_framebuffer_reference(&stdraw, NULL);
1110 st_framebuffer_reference(&stread, NULL);
1111
1112 /* Purge the context's winsys_buffers list in case any
1113 * of the referenced drawables no longer exist.
1114 */
1115 st_framebuffers_purge(st);
1116 }
1117 else {
1118 GET_CURRENT_CONTEXT(ctx);
1119
1120 if (ctx) {
1121 /* Before releasing the context, release its associated
1122 * winsys buffers first. Then purge the context's winsys buffers list
1123 * to free the resources of any winsys buffers that no longer have
1124 * an existing drawable.
1125 */
1126 ret = _mesa_make_current(ctx, NULL, NULL);
1127 st_framebuffers_purge(ctx->st);
1128 }
1129
1130 ret = _mesa_make_current(NULL, NULL, NULL);
1131 }
1132
1133 return ret;
1134 }
1135
1136
1137 static void
1138 st_api_destroy(struct st_api *stapi)
1139 {
1140 }
1141
1142
1143 /**
1144 * Flush the front buffer if the current context renders to the front buffer.
1145 */
1146 void
1147 st_manager_flush_frontbuffer(struct st_context *st)
1148 {
1149 struct st_framebuffer *stfb = st_ws_framebuffer(st->ctx->DrawBuffer);
1150 struct st_renderbuffer *strb = NULL;
1151
1152 if (!stfb)
1153 return;
1154
1155 /* If the context uses a doublebuffered visual, but the buffer is
1156 * single-buffered, guess that it's a pbuffer, which doesn't need
1157 * flushing.
1158 */
1159 if (st->ctx->Visual.doubleBufferMode &&
1160 !stfb->Base.Visual.doubleBufferMode)
1161 return;
1162
1163 strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_FRONT_LEFT].
1164 Renderbuffer);
1165
1166 /* Do we have a front color buffer and has it been drawn to since last
1167 * frontbuffer flush?
1168 */
1169 if (strb && strb->defined) {
1170 stfb->iface->flush_front(&st->iface, stfb->iface,
1171 ST_ATTACHMENT_FRONT_LEFT);
1172 strb->defined = GL_FALSE;
1173
1174 /* Trigger an update of strb->defined on next draw */
1175 st->dirty |= ST_NEW_FB_STATE;
1176 }
1177 }
1178
1179
1180 /**
1181 * Re-validate the framebuffers.
1182 */
1183 void
1184 st_manager_validate_framebuffers(struct st_context *st)
1185 {
1186 struct st_framebuffer *stdraw = st_ws_framebuffer(st->ctx->DrawBuffer);
1187 struct st_framebuffer *stread = st_ws_framebuffer(st->ctx->ReadBuffer);
1188
1189 if (stdraw)
1190 st_framebuffer_validate(stdraw, st);
1191 if (stread && stread != stdraw)
1192 st_framebuffer_validate(stread, st);
1193
1194 st_context_validate(st, stdraw, stread);
1195 }
1196
1197
1198 /**
1199 * Flush any outstanding swapbuffers on the current draw framebuffer.
1200 */
1201 void
1202 st_manager_flush_swapbuffers(void)
1203 {
1204 GET_CURRENT_CONTEXT(ctx);
1205 struct st_context *st = (ctx) ? ctx->st : NULL;
1206 struct st_framebuffer *stfb;
1207
1208 if (!st)
1209 return;
1210
1211 stfb = st_ws_framebuffer(ctx->DrawBuffer);
1212 if (!stfb || !stfb->iface->flush_swapbuffers)
1213 return;
1214
1215 stfb->iface->flush_swapbuffers(&st->iface, stfb->iface);
1216 }
1217
1218
1219 /**
1220 * Add a color renderbuffer on demand. The FBO must correspond to a window,
1221 * not a user-created FBO.
1222 */
1223 bool
1224 st_manager_add_color_renderbuffer(struct st_context *st,
1225 struct gl_framebuffer *fb,
1226 gl_buffer_index idx)
1227 {
1228 struct st_framebuffer *stfb = st_ws_framebuffer(fb);
1229
1230 /* FBO */
1231 if (!stfb)
1232 return false;
1233
1234 assert(_mesa_is_winsys_fbo(fb));
1235
1236 if (stfb->Base.Attachment[idx].Renderbuffer)
1237 return true;
1238
1239 switch (idx) {
1240 case BUFFER_FRONT_LEFT:
1241 case BUFFER_BACK_LEFT:
1242 case BUFFER_FRONT_RIGHT:
1243 case BUFFER_BACK_RIGHT:
1244 break;
1245 default:
1246 return false;
1247 }
1248
1249 if (!st_framebuffer_add_renderbuffer(stfb, idx,
1250 stfb->Base.Visual.sRGBCapable))
1251 return false;
1252
1253 st_framebuffer_update_attachments(stfb);
1254
1255 /*
1256 * Force a call to the state tracker manager to validate the
1257 * new renderbuffer. It might be that there is a window system
1258 * renderbuffer available.
1259 */
1260 if (stfb->iface)
1261 stfb->iface_stamp = p_atomic_read(&stfb->iface->stamp) - 1;
1262
1263 st_invalidate_buffers(st);
1264
1265 return true;
1266 }
1267
1268
1269 static unsigned
1270 get_version(struct pipe_screen *screen,
1271 struct st_config_options *options, gl_api api)
1272 {
1273 struct gl_constants consts = {0};
1274 struct gl_extensions extensions = {0};
1275 GLuint version;
1276
1277 if (_mesa_override_gl_version_contextless(&consts, &api, &version)) {
1278 return version;
1279 }
1280
1281 _mesa_init_constants(&consts, api);
1282 _mesa_init_extensions(&extensions);
1283
1284 st_init_limits(screen, &consts, &extensions);
1285 st_init_extensions(screen, &consts, &extensions, options, api);
1286 version = _mesa_get_version(&extensions, &consts, api);
1287 free(consts.SpirVExtensions);
1288 return version;
1289 }
1290
1291
1292 static void
1293 st_api_query_versions(struct st_api *stapi, struct st_manager *sm,
1294 struct st_config_options *options,
1295 int *gl_core_version,
1296 int *gl_compat_version,
1297 int *gl_es1_version,
1298 int *gl_es2_version)
1299 {
1300 *gl_core_version = get_version(sm->screen, options, API_OPENGL_CORE);
1301 *gl_compat_version = get_version(sm->screen, options, API_OPENGL_COMPAT);
1302 *gl_es1_version = get_version(sm->screen, options, API_OPENGLES);
1303 *gl_es2_version = get_version(sm->screen, options, API_OPENGLES2);
1304 }
1305
1306
1307 static const struct st_api st_gl_api = {
1308 .name = "Mesa " PACKAGE_VERSION,
1309 .api = ST_API_OPENGL,
1310 .profile_mask = ST_PROFILE_DEFAULT_MASK |
1311 ST_PROFILE_OPENGL_CORE_MASK |
1312 ST_PROFILE_OPENGL_ES1_MASK |
1313 ST_PROFILE_OPENGL_ES2_MASK |
1314 0,
1315 .feature_mask = ST_API_FEATURE_MS_VISUALS_MASK,
1316 .destroy = st_api_destroy,
1317 .query_versions = st_api_query_versions,
1318 .create_context = st_api_create_context,
1319 .make_current = st_api_make_current,
1320 .get_current = st_api_get_current,
1321 .destroy_drawable = st_api_destroy_drawable,
1322 };
1323
1324
1325 struct st_api *
1326 st_gl_api_create(void)
1327 {
1328 return (struct st_api *) &st_gl_api;
1329 }