From 46d86da6300ebcd2134996c76b9238fcf8e0fb6e Mon Sep 17 00:00:00 2001 From: Niklas Haas Date: Wed, 16 Aug 2017 22:13:51 +0200 Subject: vo_opengl: refactor RA texture and buffer updates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - tex_uploads args are moved to a struct - the ability to directly upload texture data without going through a buffer is made explicit - the concept of buffer updates and buffer polling is made more explicit and generalized to buf_update as well (not just mapped buffers) - the ability to call tex_upload/buf_update on a tex/buf is made explicit during tex/buf creation - uploading from buffers now uses an explicit offset instead of implicitly comparing *src against buf->data, because not all buffers may actually be persistently mapped - the initial_data = immutable requirement is dropped. (May be re-added later for D3D11 if that ever becomes a thing) This change helps the vulkan abstraction immensely and also helps move common code (like the PBO pooling) out of ra_gl and into the opengl/utils.c This also technically has the side-benefit / side-constraint of using PBOs for OSD texture uploads as well, which actually seems to help performance on machines where --opengl-pbo is faster than the naive code path. Because of this, I decided to hook up the OSD code to the opengl-pbo option as well. One drawback of this refactor is that the GL_STREAM_COPY hack for texture uploads "got lost", but I think I'm happy with that going away anyway since DR almost fully deprecates it, and it's not the "right thing" anyway - but instead an nvidia-only hack to make this stuff work somewhat better on NUMA systems with discrete GPUs. Another change is that due to the way fencing works with ra_buf (we get one fence per ra_buf per upload) we have to use multiple ra_bufs instead of offsets into a shared buffer. But for OpenGL this is probably better anyway. It's possible that in future, we could support having independent “buffer slices” (each with their own fence/sync object), but this would be an optimization more than anything. I also think that we could address the underlying problem (memory closeness) differently by making the ra_vk memory allocator smart enough to chunk together allocations under the hood. --- video/out/opengl/utils.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'video/out/opengl/utils.c') diff --git a/video/out/opengl/utils.c b/video/out/opengl/utils.c index 522ce04c0a..2a120dd5e3 100644 --- a/video/out/opengl/utils.c +++ b/video/out/opengl/utils.c @@ -120,6 +120,66 @@ void fbotex_uninit(struct fbotex *fbo) } } +bool tex_upload(struct ra *ra, struct tex_upload *pbo, bool want_pbo, + const struct ra_tex_upload_params *params) +{ + if (!(ra->caps & RA_CAP_DIRECT_UPLOAD)) + want_pbo = true; + + if (!want_pbo || params->buf) { + ra->fns->tex_upload(ra, params); + return true; + } + + struct ra_tex *tex = params->tex; + size_t row_size = tex->params.dimensions == 2 ? params->stride : + tex->params.w * tex->params.format->pixel_size; + size_t needed_size = row_size * tex->params.h * tex->params.d; + + if (needed_size > pbo->buffer_size) + tex_upload_uninit(ra, pbo); + + if (!pbo->buffers[0]) { + struct ra_buf_params bufparams = { + .type = RA_BUF_TYPE_TEX_UPLOAD, + .size = needed_size, + .host_mutable = true, + }; + + pbo->buffer_size = bufparams.size; + for (int i = 0; i < NUM_PBO_BUFFERS; i++) { + pbo->buffers[i] = ra_buf_create(ra, &bufparams); + if (!pbo->buffers[i]) + return false; + } + } + + struct ra_buf *buf = pbo->buffers[pbo->index++]; + pbo->index %= NUM_PBO_BUFFERS; + + if (!ra->fns->buf_poll(ra, buf)) { + MP_WARN(ra, "Texture upload buffer was not free to use! Try " + "increasing NUM_PBO_BUFFERS.\n"); + return false; + } + + ra->fns->buf_update(ra, buf, 0, params->src, needed_size); + + struct ra_tex_upload_params newparams = *params; + newparams.buf = buf; + newparams.src = NULL; + + ra->fns->tex_upload(ra, &newparams); + return true; +} + +void tex_upload_uninit(struct ra *ra, struct tex_upload *pbo) +{ + for (int i = 0; i < NUM_PBO_BUFFERS; i++) + ra_buf_free(ra, &pbo->buffers[i]); + *pbo = (struct tex_upload){0}; +} + struct timer_pool { struct ra *ra; ra_timer *timer; -- cgit v1.2.3