From 2ea02ed1273a58dc92d48756bd1dc00c4748abf7 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Wed, 13 Apr 2016 22:14:18 -0400 Subject: [PATCH] A source array buffer is also now created, mapped, unmapped, etc. --- Machines/Electron/Electron.cpp | 1 - Outputs/CRT/Internals/CRTConstants.hpp | 3 ++- Outputs/CRT/Internals/CRTOpenGL.cpp | 21 +++++++++++++++++++-- Outputs/CRT/Internals/CRTOpenGL.hpp | 5 ++++- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Machines/Electron/Electron.cpp b/Machines/Electron/Electron.cpp index 14333d068..81c466d41 100644 --- a/Machines/Electron/Electron.cpp +++ b/Machines/Electron/Electron.cpp @@ -64,7 +64,6 @@ void Machine::setup_output(float aspect_ratio) "uint texValue = texture(sampler, coordinate).r;" "texValue >>= 4 - (int(icoordinate.x * 8) & 4);" "return vec3( uvec3(texValue) & uvec3(4u, 2u, 1u));" -// "return vec3(1.0);" "}"); _crt->set_output_device(Outputs::CRT::Monitor); _crt->set_visible_area(_crt->get_rect_for_area(first_graphics_line - 3, 256, first_graphics_cycle * crt_cycles_multiplier, 80 * crt_cycles_multiplier, 4.0f / 3.0f)); diff --git a/Outputs/CRT/Internals/CRTConstants.hpp b/Outputs/CRT/Internals/CRTConstants.hpp index d14e7aee5..d343164f8 100644 --- a/Outputs/CRT/Internals/CRTConstants.hpp +++ b/Outputs/CRT/Internals/CRTConstants.hpp @@ -42,8 +42,9 @@ const int InputBufferBuilderHeight = 1024; const int IntermediateBufferWidth = 2048; const int IntermediateBufferHeight = 2048; -// Some internal +// Some internal buffer sizes const GLsizeiptr OutputVertexBufferDataSize = 262080; // a multiple of 6 * OutputVertexSize +const GLsizeiptr SourceVertexBufferDataSize = 87360; // a multiple of 2 * OutputVertexSize // Runs are divided discretely by vertical syncs in order to put a usable bounds on the uniform used to track diff --git a/Outputs/CRT/Internals/CRTOpenGL.cpp b/Outputs/CRT/Internals/CRTOpenGL.cpp index b21dc0e22..5ab01bb02 100644 --- a/Outputs/CRT/Internals/CRTOpenGL.cpp +++ b/Outputs/CRT/Internals/CRTOpenGL.cpp @@ -51,7 +51,7 @@ OpenGLOutputBuilder::OpenGLOutputBuilder(unsigned int buffer_depth) : _composite_shader(nullptr), _rgb_shader(nullptr), _output_buffer_data(nullptr), - _output_buffer_sync(nullptr), + _source_buffer_data(nullptr), _input_texture_data(nullptr), _output_buffer_data_pointer(0) { @@ -100,6 +100,14 @@ OpenGLOutputBuilder::OpenGLOutputBuilder(unsigned int buffer_depth) : // map that buffer too, for any CRT activity that may occur before the first draw _output_buffer_data = (uint8_t *)glMapBufferRange(GL_ARRAY_BUFFER, 0, OutputVertexBufferDataSize, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT); + + // create a buffer for source vertex attributes + glGenBuffers(1, &source_array_buffer); + glBindBuffer(GL_ARRAY_BUFFER, source_array_buffer); + glBufferData(GL_ARRAY_BUFFER, SourceVertexBufferDataSize, NULL, GL_STREAM_DRAW); + + // map that buffer too, for any CRT activity that may occur before the first draw + _source_buffer_data = (uint8_t *)glMapBufferRange(GL_ARRAY_BUFFER, 0, SourceVertexBufferDataSize, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT); } OpenGLOutputBuilder::~OpenGLOutputBuilder() @@ -115,6 +123,7 @@ OpenGLOutputBuilder::~OpenGLOutputBuilder() glDeleteTextures(1, &textureName); glDeleteBuffers(1, &_input_texture_array); glDeleteBuffers(1, &output_array_buffer); + glDeleteBuffers(1, &source_array_buffer); glDeleteVertexArrays(1, &output_vertex_array); free(_composite_shader); @@ -143,11 +152,14 @@ void OpenGLOutputBuilder::draw_frame(unsigned int output_width, unsigned int out _output_mutex->lock(); // release the mapping, giving up on trying to draw if data has been lost + glBindBuffer(GL_ARRAY_BUFFER, output_array_buffer); if(glUnmapBuffer(GL_ARRAY_BUFFER) == GL_FALSE) { for(int c = 0; c < NumberOfFields; c++) _run_builders[c]->reset(); } + glBindBuffer(GL_ARRAY_BUFFER, source_array_buffer); + glUnmapBuffer(GL_ARRAY_BUFFER); glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); // upload more source pixel data if any; we'll always resubmit the last line submitted last @@ -194,8 +206,9 @@ void OpenGLOutputBuilder::draw_frame(unsigned int output_width, unsigned int out // update uniforms push_size_uniforms(output_width, output_height); - // Ensure we're back on the output framebuffer + // Ensure we're back on the output framebuffer, drawing from the output array buffer glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer); +// glBindBuffer(GL_ARRAY_BUFFER, output_array_buffer); // clear the buffer glClear(GL_COLOR_BUFFER_BIT); @@ -233,7 +246,10 @@ void OpenGLOutputBuilder::draw_frame(unsigned int output_width, unsigned int out } // drawing commands having been issued, reclaim the array buffer pointer + glBindBuffer(GL_ARRAY_BUFFER, output_array_buffer); _output_buffer_data = (uint8_t *)glMapBufferRange(GL_ARRAY_BUFFER, 0, OutputVertexBufferDataSize, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT); + glBindBuffer(GL_ARRAY_BUFFER, source_array_buffer); + _source_buffer_data = (uint8_t *)glMapBufferRange(GL_ARRAY_BUFFER, 0, SourceVertexBufferDataSize, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT); _input_texture_data = (uint8_t *)glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, _input_texture_array_size, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT); _output_mutex->unlock(); } @@ -554,6 +570,7 @@ void OpenGLOutputBuilder::prepare_output_vertex_array() glEnableVertexAttribArray((GLuint)timestampAttribute); const GLsizei vertexStride = OutputVertexSize; + glBindBuffer(GL_ARRAY_BUFFER, output_array_buffer); glVertexAttribPointer((GLuint)positionAttribute, 2, GL_UNSIGNED_SHORT, GL_FALSE, vertexStride, (void *)OutputVertexOffsetOfPosition); glVertexAttribPointer((GLuint)textureCoordinatesAttribute, 2, GL_UNSIGNED_SHORT, GL_FALSE, vertexStride, (void *)OutputVertexOffsetOfTexCoord); glVertexAttribPointer((GLuint)timestampAttribute, 4, GL_UNSIGNED_INT, GL_FALSE, vertexStride, (void *)OutputVertexOffsetOfTimestamp); diff --git a/Outputs/CRT/Internals/CRTOpenGL.hpp b/Outputs/CRT/Internals/CRTOpenGL.hpp index 07866d68b..a6c8837b1 100644 --- a/Outputs/CRT/Internals/CRTOpenGL.hpp +++ b/Outputs/CRT/Internals/CRTOpenGL.hpp @@ -75,6 +75,7 @@ class OpenGLOutputBuilder { std::unique_ptr composite_input_shader_program, composite_output_shader_program; GLuint output_array_buffer, output_vertex_array; + GLuint source_array_buffer; GLint windowSizeUniform, timestampBaseUniform; GLint boundsOriginUniform, boundsSizeUniform; @@ -212,7 +213,9 @@ class OpenGLOutputBuilder { uint8_t *_output_buffer_data; size_t _output_buffer_data_pointer; - GLsync _output_buffer_sync; + + uint8_t *_source_buffer_data; + size_t _source_buffer_data_pointer; }; }