- unsigned field_start_line = (field == 1) ? second_field_start : extra_lines_top + field * (height + 22);
-
- if (userdata->tex_y[field] == 0 ||
- userdata->tex_cbcr[field] == 0 ||
- width != userdata->last_width[field] ||
- height != userdata->last_height[field]) {
- // We changed resolution since last use of this texture, so we need to create
- // a new object. Note that this each card has its own PBOFrameAllocator,
- // we don't need to worry about these flip-flopping between resolutions.
+ // Put the actual texture upload in a lambda that is executed in the main thread.
+ // It is entirely possible to do this in the same thread (and it might even be
+ // faster, depending on the GPU and driver), but it appears to be trickling
+ // driver bugs very easily.
+ //
+ // Note that this means we must hold on to the actual frame data in <userdata>
+ // until the upload command is run, but we hold on to <frame> much longer than that
+ // (in fact, all the way until we no longer use the texture in rendering).
+ auto upload_func = [field, video_format, y_offset, cbcr_offset, cbcr_width, userdata]() {
+ unsigned field_start_line = (field == 1) ? video_format.second_field_start : video_format.extra_lines_top + field * (video_format.height + 22);
+
+ if (userdata->tex_y[field] == 0 ||
+ userdata->tex_cbcr[field] == 0 ||
+ video_format.width != userdata->last_width[field] ||
+ video_format.height != userdata->last_height[field]) {
+ // We changed resolution since last use of this texture, so we need to create
+ // a new object. Note that this each card has its own PBOFrameAllocator,
+ // we don't need to worry about these flip-flopping between resolutions.
+ glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
+ check_error();
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, cbcr_width, video_format.height, 0, GL_RG, GL_UNSIGNED_BYTE, nullptr);
+ check_error();
+ glBindTexture(GL_TEXTURE_2D, userdata->tex_y[field]);
+ check_error();
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, video_format.width, video_format.height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
+ check_error();
+ userdata->last_width[field] = video_format.width;
+ userdata->last_height[field] = video_format.height;
+ }
+
+ GLuint pbo = userdata->pbo;
+ check_error();
+ glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
+ check_error();
+
+ size_t field_y_start = y_offset + video_format.width * field_start_line;
+ size_t field_cbcr_start = cbcr_offset + cbcr_width * field_start_line * sizeof(uint16_t);
+
+ if (global_flags.flush_pbos) {
+ glFlushMappedBufferRange(GL_PIXEL_UNPACK_BUFFER, field_y_start, video_format.width * video_format.height);
+ check_error();
+ glFlushMappedBufferRange(GL_PIXEL_UNPACK_BUFFER, field_cbcr_start, cbcr_width * video_format.height * sizeof(uint16_t));
+ check_error();
+ }
+