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