2018-11-07 03:23:38 +00:00
|
|
|
//
|
|
|
|
// ScanTarget.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 05/11/2018.
|
|
|
|
// Copyright © 2018 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "ScanTarget.hpp"
|
2019-01-26 01:19:50 +00:00
|
|
|
|
2019-02-18 15:29:40 +00:00
|
|
|
#include "OpenGL.hpp"
|
2018-11-08 03:53:46 +00:00
|
|
|
#include "Primitives/Rectangle.hpp"
|
2018-11-07 03:23:38 +00:00
|
|
|
|
2019-01-26 01:19:50 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
2019-03-06 02:41:20 +00:00
|
|
|
#include <limits>
|
2019-01-26 01:19:50 +00:00
|
|
|
|
2018-11-07 03:23:38 +00:00
|
|
|
using namespace Outputs::Display::OpenGL;
|
|
|
|
|
2019-02-24 19:30:39 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
//#define LOG_LINES
|
|
|
|
//#define LOG_SCANS
|
|
|
|
#endif
|
|
|
|
|
2018-11-07 03:23:38 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-01-02 02:02:21 +00:00
|
|
|
/// The texture unit from which to source input data.
|
2019-01-17 03:22:29 +00:00
|
|
|
constexpr GLenum SourceDataTextureUnit = GL_TEXTURE0;
|
2018-11-11 21:22:14 +00:00
|
|
|
|
2018-11-24 03:34:38 +00:00
|
|
|
/// The texture unit which contains raw line-by-line composite, S-Video or RGB data.
|
2019-01-17 03:22:29 +00:00
|
|
|
constexpr GLenum UnprocessedLineBufferTextureUnit = GL_TEXTURE1;
|
2018-11-11 21:22:14 +00:00
|
|
|
|
2019-02-09 21:54:31 +00:00
|
|
|
/// The texture unit that contains a pre-lowpass-filtered but fixed-resolution version of the chroma signal;
|
|
|
|
/// this is used when processing composite video only, and for chroma information only. Luminance is calculated
|
|
|
|
/// at the fidelity permitted by the output target, but my efforts to separate, demodulate and filter
|
|
|
|
/// chrominance during output without either massively sampling or else incurring significant high-frequency
|
|
|
|
/// noise that sampling reduces into a Moire, have proven to be unsuccessful for the time being.
|
|
|
|
constexpr GLenum QAMChromaTextureUnit = GL_TEXTURE2;
|
2018-11-11 20:11:32 +00:00
|
|
|
|
2019-02-10 03:45:20 +00:00
|
|
|
/// The texture unit that contains the current display.
|
|
|
|
constexpr GLenum AccumulationTextureUnit = GL_TEXTURE3;
|
|
|
|
|
2020-05-30 04:37:06 +00:00
|
|
|
constexpr GLint internalFormatForDepth(std::size_t depth) {
|
2018-11-09 04:02:36 +00:00
|
|
|
switch(depth) {
|
|
|
|
default: return GL_FALSE;
|
|
|
|
case 1: return GL_R8UI;
|
|
|
|
case 2: return GL_RG8UI;
|
|
|
|
case 3: return GL_RGB8UI;
|
|
|
|
case 4: return GL_RGBA8UI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 04:37:06 +00:00
|
|
|
constexpr GLenum formatForDepth(std::size_t depth) {
|
2018-11-09 04:02:36 +00:00
|
|
|
switch(depth) {
|
|
|
|
default: return GL_FALSE;
|
|
|
|
case 1: return GL_RED_INTEGER;
|
|
|
|
case 2: return GL_RG_INTEGER;
|
|
|
|
case 3: return GL_RGB_INTEGER;
|
|
|
|
case 4: return GL_RGBA_INTEGER;
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 03:23:38 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-13 00:10:48 +00:00
|
|
|
template <typename T> void ScanTarget::allocate_buffer(const T &array, GLuint &buffer_name, GLuint &vertex_array_name) {
|
2018-11-12 23:47:55 +00:00
|
|
|
const auto buffer_size = array.size() * sizeof(array[0]);
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glGenBuffers, 1, &buffer_name);
|
|
|
|
test_gl(glBindBuffer, GL_ARRAY_BUFFER, buffer_name);
|
|
|
|
test_gl(glBufferData, GL_ARRAY_BUFFER, GLsizeiptr(buffer_size), NULL, GL_STREAM_DRAW);
|
2018-11-13 00:10:48 +00:00
|
|
|
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glGenVertexArrays, 1, &vertex_array_name);
|
|
|
|
test_gl(glBindVertexArray, vertex_array_name);
|
|
|
|
test_gl(glBindBuffer, GL_ARRAY_BUFFER, buffer_name);
|
2018-11-12 23:47:55 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 02:33:30 +00:00
|
|
|
ScanTarget::ScanTarget(GLuint target_framebuffer, float output_gamma) :
|
|
|
|
target_framebuffer_(target_framebuffer),
|
|
|
|
output_gamma_(output_gamma),
|
2018-11-27 03:34:31 +00:00
|
|
|
unprocessed_line_texture_(LineBufferWidth, LineBufferHeight, UnprocessedLineBufferTextureUnit, GL_NEAREST, false),
|
2018-11-24 03:34:38 +00:00
|
|
|
full_display_rectangle_(-1.0f, -1.0f, 2.0f, 2.0f) {
|
2018-11-11 20:11:32 +00:00
|
|
|
|
2020-07-27 02:46:03 +00:00
|
|
|
set_scan_buffer(scan_buffer_.data(), scan_buffer_.size());
|
|
|
|
set_line_buffer(line_buffer_.data(), line_metadata_buffer_.data(), line_buffer_.size());
|
|
|
|
|
2018-11-12 23:47:55 +00:00
|
|
|
// Allocate space for the scans and lines.
|
2018-11-13 00:10:48 +00:00
|
|
|
allocate_buffer(scan_buffer_, scan_buffer_name_, scan_vertex_array_);
|
|
|
|
allocate_buffer(line_buffer_, line_buffer_name_, line_vertex_array_);
|
2018-11-09 03:21:11 +00:00
|
|
|
|
|
|
|
// TODO: if this is OpenGL 4.4 or newer, use glBufferStorage rather than glBufferData
|
|
|
|
// and specify GL_MAP_PERSISTENT_BIT. Then map the buffer now, and let the client
|
|
|
|
// write straight into it.
|
2018-11-09 04:02:36 +00:00
|
|
|
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glGenTextures, 1, &write_area_texture_name_);
|
2018-11-12 04:23:42 +00:00
|
|
|
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glBlendFunc, GL_SRC_ALPHA, GL_CONSTANT_COLOR);
|
|
|
|
test_gl(glBlendColor, 0.4f, 0.4f, 0.4f, 1.0f);
|
2018-11-16 02:51:27 +00:00
|
|
|
|
2020-07-24 02:54:40 +00:00
|
|
|
// Establish initial state for is_drawing_to_accumulation_buffer_.
|
2019-03-08 00:28:32 +00:00
|
|
|
is_drawing_to_accumulation_buffer_.clear();
|
2018-11-08 03:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ScanTarget::~ScanTarget() {
|
2020-07-26 21:27:19 +00:00
|
|
|
perform([=] {
|
|
|
|
glDeleteBuffers(1, &scan_buffer_name_);
|
|
|
|
glDeleteTextures(1, &write_area_texture_name_);
|
|
|
|
glDeleteVertexArrays(1, &scan_vertex_array_);
|
|
|
|
});
|
2018-11-08 03:53:46 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 03:28:02 +00:00
|
|
|
void ScanTarget::set_target_framebuffer(GLuint target_framebuffer) {
|
2020-07-26 21:27:19 +00:00
|
|
|
perform([=] {
|
|
|
|
target_framebuffer_ = target_framebuffer;
|
|
|
|
});
|
2019-01-18 03:28:02 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 03:41:54 +00:00
|
|
|
void ScanTarget::setup_pipeline() {
|
2020-07-28 03:33:39 +00:00
|
|
|
auto modals = BufferingScanTarget::modals();
|
|
|
|
const auto data_type_size = Outputs::Display::size_for_data_type(modals.input_data_type);
|
2018-11-30 03:41:54 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
// Resize the texture only if required.
|
2020-08-13 02:08:41 +00:00
|
|
|
const size_t required_size = WriteAreaWidth*WriteAreaHeight*data_type_size;
|
|
|
|
if(required_size != write_area_data_size()) {
|
|
|
|
write_area_texture_.resize(required_size);
|
2020-07-24 02:54:40 +00:00
|
|
|
set_write_area(write_area_texture_.data());
|
2018-11-30 03:41:54 +00:00
|
|
|
}
|
|
|
|
|
2019-02-11 03:39:24 +00:00
|
|
|
// Prepare to bind line shaders.
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glBindVertexArray, line_vertex_array_);
|
|
|
|
test_gl(glBindBuffer, GL_ARRAY_BUFFER, line_buffer_name_);
|
2019-02-11 03:39:24 +00:00
|
|
|
|
2019-02-09 21:54:31 +00:00
|
|
|
// Destroy or create a QAM buffer and shader, if appropriate.
|
2020-07-28 03:33:39 +00:00
|
|
|
const bool needs_qam_buffer = (modals.display_type == DisplayType::CompositeColour || modals.display_type == DisplayType::SVideo);
|
2019-02-09 21:54:31 +00:00
|
|
|
if(needs_qam_buffer) {
|
2019-02-09 22:20:13 +00:00
|
|
|
if(!qam_chroma_texture_) {
|
2019-12-24 02:31:46 +00:00
|
|
|
qam_chroma_texture_ = std::make_unique<TextureTarget>(LineBufferWidth, LineBufferHeight, QAMChromaTextureUnit, GL_NEAREST, false);
|
2019-02-09 22:20:13 +00:00
|
|
|
}
|
|
|
|
|
2019-02-09 21:54:31 +00:00
|
|
|
qam_separation_shader_ = qam_separation_shader();
|
|
|
|
enable_vertex_attributes(ShaderType::QAMSeparation, *qam_separation_shader_);
|
|
|
|
set_uniforms(ShaderType::QAMSeparation, *qam_separation_shader_);
|
|
|
|
qam_separation_shader_->set_uniform("textureName", GLint(UnprocessedLineBufferTextureUnit - GL_TEXTURE0));
|
2019-02-09 22:20:13 +00:00
|
|
|
} else {
|
|
|
|
qam_chroma_texture_.reset();
|
|
|
|
qam_separation_shader_.reset();
|
2019-02-09 21:54:31 +00:00
|
|
|
}
|
2018-11-30 03:41:54 +00:00
|
|
|
|
2019-01-16 02:33:30 +00:00
|
|
|
// Establish an output shader.
|
2019-01-23 03:20:12 +00:00
|
|
|
output_shader_ = conversion_shader();
|
2019-01-14 04:07:50 +00:00
|
|
|
enable_vertex_attributes(ShaderType::Conversion, *output_shader_);
|
|
|
|
set_uniforms(ShaderType::Conversion, *output_shader_);
|
2020-07-28 03:33:39 +00:00
|
|
|
output_shader_->set_uniform("origin", modals.visible_area.origin.x, modals.visible_area.origin.y);
|
|
|
|
output_shader_->set_uniform("size", modals.visible_area.size.width, modals.visible_area.size.height);
|
2019-01-02 02:02:21 +00:00
|
|
|
output_shader_->set_uniform("textureName", GLint(UnprocessedLineBufferTextureUnit - GL_TEXTURE0));
|
2019-02-09 21:54:31 +00:00
|
|
|
output_shader_->set_uniform("qamTextureName", GLint(QAMChromaTextureUnit - GL_TEXTURE0));
|
2018-11-30 03:41:54 +00:00
|
|
|
|
2019-01-02 02:02:21 +00:00
|
|
|
// Establish an input shader.
|
2019-01-23 03:20:12 +00:00
|
|
|
input_shader_ = composition_shader();
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glBindVertexArray, scan_vertex_array_);
|
|
|
|
test_gl(glBindBuffer, GL_ARRAY_BUFFER, scan_buffer_name_);
|
2019-01-14 04:07:50 +00:00
|
|
|
enable_vertex_attributes(ShaderType::Composition, *input_shader_);
|
|
|
|
set_uniforms(ShaderType::Composition, *input_shader_);
|
2019-01-02 02:02:21 +00:00
|
|
|
input_shader_->set_uniform("textureName", GLint(SourceDataTextureUnit - GL_TEXTURE0));
|
2018-11-30 03:41:54 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 19:57:31 +00:00
|
|
|
bool ScanTarget::is_soft_display_type() {
|
2020-07-28 03:33:39 +00:00
|
|
|
const auto display_type = modals().display_type;
|
|
|
|
return display_type == DisplayType::CompositeColour || display_type == DisplayType::CompositeMonochrome;
|
2019-06-03 19:57:31 +00:00
|
|
|
}
|
|
|
|
|
2020-05-30 04:37:06 +00:00
|
|
|
void ScanTarget::update(int, int output_height) {
|
2020-07-26 21:27:19 +00:00
|
|
|
// If the GPU is still busy, don't wait; we'll catch it next time.
|
2018-11-13 01:15:38 +00:00
|
|
|
if(fence_ != nullptr) {
|
2019-03-03 00:33:28 +00:00
|
|
|
if(glClientWaitSync(fence_, GL_SYNC_FLUSH_COMMANDS_BIT, 0) == GL_TIMEOUT_EXPIRED) {
|
2019-03-05 02:54:50 +00:00
|
|
|
display_metrics_.announce_draw_status(
|
|
|
|
lines_submitted_,
|
|
|
|
std::chrono::high_resolution_clock::now() - line_submission_begin_time_,
|
|
|
|
false);
|
2018-11-13 01:15:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
fence_ = nullptr;
|
|
|
|
}
|
2020-07-26 21:27:19 +00:00
|
|
|
|
|
|
|
// Update the display metrics.
|
2019-03-05 02:54:50 +00:00
|
|
|
display_metrics_.announce_draw_status(
|
|
|
|
lines_submitted_,
|
|
|
|
std::chrono::high_resolution_clock::now() - line_submission_begin_time_,
|
|
|
|
true);
|
2018-11-12 23:47:55 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
// Grab the new output list.
|
2020-08-13 02:08:41 +00:00
|
|
|
perform([=] {
|
|
|
|
OutputArea area = get_output_area();
|
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
// Establish the pipeline if necessary.
|
2020-07-28 03:33:39 +00:00
|
|
|
const auto new_modals = BufferingScanTarget::new_modals();
|
|
|
|
const bool did_setup_pipeline = bool(new_modals);
|
|
|
|
if(did_setup_pipeline) {
|
2020-07-26 21:27:19 +00:00
|
|
|
setup_pipeline();
|
|
|
|
}
|
2018-11-13 03:52:26 +00:00
|
|
|
|
2020-07-26 21:54:33 +00:00
|
|
|
// Determine the start time of this submission group and the number of lines it will contain.
|
2020-07-26 21:27:19 +00:00
|
|
|
line_submission_begin_time_ = std::chrono::high_resolution_clock::now();
|
|
|
|
lines_submitted_ = (area.end.line - area.start.line + line_buffer_.size()) % line_buffer_.size();
|
2018-11-14 02:15:33 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
// Submit scans; only the new ones need to be communicated.
|
|
|
|
size_t new_scans = (area.end.scan - area.start.scan + scan_buffer_.size()) % scan_buffer_.size();
|
|
|
|
if(new_scans) {
|
|
|
|
test_gl(glBindBuffer, GL_ARRAY_BUFFER, scan_buffer_name_);
|
2018-11-08 03:53:46 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
// Map only the required portion of the buffer.
|
|
|
|
const size_t new_scans_size = new_scans * sizeof(Scan);
|
|
|
|
uint8_t *const destination = static_cast<uint8_t *>(
|
|
|
|
glMapBufferRange(GL_ARRAY_BUFFER, 0, GLsizeiptr(new_scans_size), GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT)
|
|
|
|
);
|
|
|
|
test_gl_error();
|
|
|
|
|
|
|
|
// Copy as a single chunk if possible; otherwise copy in two parts.
|
|
|
|
if(area.start.scan < area.end.scan) {
|
|
|
|
memcpy(destination, &scan_buffer_[size_t(area.start.scan)], new_scans_size);
|
|
|
|
} else {
|
|
|
|
const size_t first_portion_length = (scan_buffer_.size() - area.start.scan) * sizeof(Scan);
|
|
|
|
memcpy(destination, &scan_buffer_[area.start.scan], first_portion_length);
|
|
|
|
memcpy(&destination[first_portion_length], &scan_buffer_[0], new_scans_size - first_portion_length);
|
|
|
|
}
|
2018-11-09 04:02:36 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
// Flush and unmap the buffer.
|
|
|
|
test_gl(glFlushMappedBufferRange, GL_ARRAY_BUFFER, 0, GLsizeiptr(new_scans_size));
|
|
|
|
test_gl(glUnmapBuffer, GL_ARRAY_BUFFER);
|
2018-11-09 04:02:36 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
// Submit texture.
|
|
|
|
if(area.start.write_area_x != area.end.write_area_x || area.start.write_area_y != area.end.write_area_y) {
|
|
|
|
test_gl(glActiveTexture, SourceDataTextureUnit);
|
|
|
|
test_gl(glBindTexture, GL_TEXTURE_2D, write_area_texture_name_);
|
|
|
|
|
|
|
|
// Create storage for the texture if it doesn't yet exist; this was deferred until here
|
|
|
|
// because the pixel format wasn't initially known.
|
|
|
|
if(!texture_exists_) {
|
|
|
|
test_gl(glTexParameteri, GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
test_gl(glTexParameteri, GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
test_gl(glTexParameteri, GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
test_gl(glTexParameteri, GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
test_gl(glTexImage2D,
|
|
|
|
GL_TEXTURE_2D,
|
|
|
|
0,
|
|
|
|
internalFormatForDepth(write_area_data_size()),
|
|
|
|
WriteAreaWidth,
|
|
|
|
WriteAreaHeight,
|
|
|
|
0,
|
|
|
|
formatForDepth(write_area_data_size()),
|
|
|
|
GL_UNSIGNED_BYTE,
|
|
|
|
nullptr);
|
|
|
|
texture_exists_ = true;
|
2019-02-26 03:07:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
if(area.end.write_area_y >= area.start.write_area_y) {
|
|
|
|
// Submit the direct region from the submit pointer to the read pointer.
|
|
|
|
test_gl(glTexSubImage2D,
|
|
|
|
GL_TEXTURE_2D, 0,
|
|
|
|
0, area.start.write_area_y,
|
|
|
|
WriteAreaWidth,
|
|
|
|
1 + area.end.write_area_y - area.start.write_area_y,
|
|
|
|
formatForDepth(write_area_data_size()),
|
|
|
|
GL_UNSIGNED_BYTE,
|
2020-07-27 02:46:03 +00:00
|
|
|
&write_area_texture_[size_t(area.start.write_area_y * WriteAreaWidth) * write_area_data_size()]);
|
2018-11-15 02:19:14 +00:00
|
|
|
} else {
|
2020-07-26 21:27:19 +00:00
|
|
|
// The circular buffer wrapped around; submit the data from the read pointer to the end of
|
|
|
|
// the buffer and from the start of the buffer to the submit pointer.
|
|
|
|
test_gl(glTexSubImage2D,
|
|
|
|
GL_TEXTURE_2D, 0,
|
|
|
|
0, area.start.write_area_y,
|
|
|
|
WriteAreaWidth,
|
|
|
|
WriteAreaHeight - area.start.write_area_y,
|
|
|
|
formatForDepth(write_area_data_size()),
|
|
|
|
GL_UNSIGNED_BYTE,
|
2020-07-27 02:46:03 +00:00
|
|
|
&write_area_texture_[size_t(area.start.write_area_y * WriteAreaWidth) * write_area_data_size()]);
|
2020-07-26 21:27:19 +00:00
|
|
|
test_gl(glTexSubImage2D,
|
|
|
|
GL_TEXTURE_2D, 0,
|
|
|
|
0, 0,
|
|
|
|
WriteAreaWidth,
|
|
|
|
1 + area.end.write_area_y,
|
|
|
|
formatForDepth(write_area_data_size()),
|
|
|
|
GL_UNSIGNED_BYTE,
|
|
|
|
&write_area_texture_[0]);
|
2018-11-15 02:19:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
// Push new input to the unprocessed line buffer.
|
|
|
|
if(new_scans) {
|
|
|
|
unprocessed_line_texture_.bind_framebuffer();
|
|
|
|
|
|
|
|
// Clear newly-touched lines; that is everything from (read+1) to submit.
|
|
|
|
const auto first_line_to_clear = GLsizei((area.start.line+1)%line_buffer_.size());
|
|
|
|
const auto final_line_to_clear = GLsizei(area.end.line);
|
|
|
|
if(first_line_to_clear != final_line_to_clear) {
|
|
|
|
test_gl(glEnable, GL_SCISSOR_TEST);
|
|
|
|
|
|
|
|
// Determine the proper clear colour — this needs to be anything that describes black
|
|
|
|
// in the input colour encoding at use.
|
2020-07-28 03:33:39 +00:00
|
|
|
if(modals().input_data_type == InputDataType::Luminance8Phase8) {
|
2020-07-26 21:27:19 +00:00
|
|
|
// Supply both a zero luminance and a colour-subcarrier-disengaging phase.
|
|
|
|
test_gl(glClearColor, 0.0f, 1.0f, 0.0f, 0.0f);
|
|
|
|
} else {
|
|
|
|
test_gl(glClearColor, 0.0f, 0.0f, 0.0f, 0.0f);
|
|
|
|
}
|
2018-11-09 03:21:11 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
if(first_line_to_clear < final_line_to_clear) {
|
|
|
|
test_gl(glScissor, GLint(0), GLint(first_line_to_clear), unprocessed_line_texture_.get_width(), final_line_to_clear - first_line_to_clear);
|
|
|
|
test_gl(glClear, GL_COLOR_BUFFER_BIT);
|
|
|
|
} else {
|
|
|
|
test_gl(glScissor, GLint(0), GLint(0), unprocessed_line_texture_.get_width(), final_line_to_clear);
|
|
|
|
test_gl(glClear, GL_COLOR_BUFFER_BIT);
|
|
|
|
test_gl(glScissor, GLint(0), GLint(first_line_to_clear), unprocessed_line_texture_.get_width(), unprocessed_line_texture_.get_height() - first_line_to_clear);
|
|
|
|
test_gl(glClear, GL_COLOR_BUFFER_BIT);
|
|
|
|
}
|
2019-03-06 02:41:20 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
test_gl(glDisable, GL_SCISSOR_TEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply new spans. They definitely always go to the first buffer.
|
|
|
|
test_gl(glBindVertexArray, scan_vertex_array_);
|
|
|
|
input_shader_->bind();
|
|
|
|
test_gl(glDrawArraysInstanced, GL_TRIANGLE_STRIP, 0, 4, GLsizei(new_scans));
|
2018-11-19 02:39:11 +00:00
|
|
|
}
|
2018-11-21 00:51:11 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
// Logic for reducing resolution: start doing so if the metrics object reports that
|
|
|
|
// it's a good idea. Go up to a quarter of the requested resolution, subject to
|
|
|
|
// clamping at each stage. If the output resolution changes, or anything else about
|
|
|
|
// the output pipeline, just start trying the highest size again.
|
|
|
|
if(display_metrics_.should_lower_resolution() && is_soft_display_type()) {
|
|
|
|
resolution_reduction_level_ = std::min(resolution_reduction_level_+1, 4);
|
|
|
|
}
|
|
|
|
if(output_height_ != output_height || did_setup_pipeline) {
|
|
|
|
resolution_reduction_level_ = 1;
|
|
|
|
output_height_ = output_height;
|
|
|
|
}
|
2018-11-19 02:39:11 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
// Ensure the accumulation buffer is properly sized, allowing for the metrics object's
|
|
|
|
// feelings about whether too high a resolution is being used.
|
|
|
|
const int framebuffer_height = std::max(output_height / resolution_reduction_level_, std::min(540, output_height));
|
|
|
|
const int proportional_width = (framebuffer_height * 4) / 3;
|
|
|
|
const bool did_create_accumulation_texture = !accumulation_texture_ || ( (accumulation_texture_->get_width() != proportional_width || accumulation_texture_->get_height() != framebuffer_height));
|
|
|
|
|
|
|
|
// Work with the accumulation_buffer_ potentially starts from here onwards; set its flag.
|
|
|
|
while(is_drawing_to_accumulation_buffer_.test_and_set());
|
|
|
|
if(did_create_accumulation_texture) {
|
|
|
|
LOG("Changed output resolution to " << proportional_width << " by " << framebuffer_height);
|
|
|
|
display_metrics_.announce_did_resize();
|
|
|
|
std::unique_ptr<OpenGL::TextureTarget> new_framebuffer(
|
|
|
|
new TextureTarget(
|
|
|
|
GLsizei(proportional_width),
|
|
|
|
GLsizei(framebuffer_height),
|
|
|
|
AccumulationTextureUnit,
|
|
|
|
GL_NEAREST,
|
|
|
|
true));
|
|
|
|
if(accumulation_texture_) {
|
|
|
|
new_framebuffer->bind_framebuffer();
|
|
|
|
test_gl(glClear, GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
|
|
|
|
|
|
|
test_gl(glActiveTexture, AccumulationTextureUnit);
|
|
|
|
accumulation_texture_->bind_texture();
|
|
|
|
accumulation_texture_->draw(4.0f / 3.0f);
|
2019-02-27 03:35:55 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
test_gl(glClear, GL_STENCIL_BUFFER_BIT);
|
2019-02-10 03:45:20 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
new_framebuffer->bind_texture();
|
|
|
|
}
|
|
|
|
accumulation_texture_ = std::move(new_framebuffer);
|
2018-11-20 04:25:26 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
// In the absence of a way to resize a stencil buffer, just mark
|
|
|
|
// what's currently present as invalid to avoid an improper clear
|
|
|
|
// for this frame.
|
|
|
|
stencil_is_valid_ = false;
|
2019-02-10 03:45:20 +00:00
|
|
|
}
|
2019-02-18 02:49:53 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
if(did_setup_pipeline || did_create_accumulation_texture) {
|
|
|
|
set_sampling_window(proportional_width, framebuffer_height, *output_shader_);
|
|
|
|
}
|
2018-11-20 04:25:26 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
// Figure out how many new lines are ready.
|
|
|
|
auto new_lines = (area.end.line - area.start.line + LineBufferHeight) % LineBufferHeight;
|
|
|
|
if(new_lines) {
|
|
|
|
// Prepare to output lines.
|
|
|
|
test_gl(glBindVertexArray, line_vertex_array_);
|
2018-11-20 04:25:26 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
// Bind the accumulation framebuffer, unless there's going to be QAM work first.
|
|
|
|
if(!qam_separation_shader_ || line_metadata_buffer_[area.start.line].is_first_in_frame) {
|
|
|
|
accumulation_texture_->bind_framebuffer();
|
|
|
|
output_shader_->bind();
|
2018-11-20 04:25:26 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
// Enable blending and stenciling.
|
|
|
|
test_gl(glEnable, GL_BLEND);
|
|
|
|
test_gl(glEnable, GL_STENCIL_TEST);
|
2018-11-20 04:25:26 +00:00
|
|
|
}
|
2018-11-19 03:22:43 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
// Set the proper stencil function regardless.
|
|
|
|
test_gl(glStencilFunc, GL_EQUAL, 0, GLuint(~0));
|
|
|
|
test_gl(glStencilOp, GL_KEEP, GL_KEEP, GL_INCR);
|
|
|
|
|
|
|
|
// Prepare to upload data that will consitute lines.
|
|
|
|
test_gl(glBindBuffer, GL_ARRAY_BUFFER, line_buffer_name_);
|
|
|
|
|
|
|
|
// Divide spans by which frame they're in.
|
|
|
|
auto start_line = area.start.line;
|
|
|
|
while(new_lines) {
|
|
|
|
uint16_t end_line = (start_line + 1) % LineBufferHeight;
|
|
|
|
|
|
|
|
// Find the limit of spans to draw in this cycle.
|
|
|
|
size_t lines = 1;
|
|
|
|
while(end_line != area.end.line && !line_metadata_buffer_[end_line].is_first_in_frame) {
|
|
|
|
end_line = (end_line + 1) % LineBufferHeight;
|
|
|
|
++lines;
|
2018-11-21 23:27:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
// If this is start-of-frame, clear any untouched pixels and flush the stencil buffer
|
|
|
|
if(line_metadata_buffer_[start_line].is_first_in_frame) {
|
|
|
|
if(stencil_is_valid_ && line_metadata_buffer_[start_line].previous_frame_was_complete) {
|
|
|
|
full_display_rectangle_.draw(0.0f, 0.0f, 0.0f);
|
|
|
|
}
|
|
|
|
stencil_is_valid_ = true;
|
|
|
|
test_gl(glClear, GL_STENCIL_BUFFER_BIT);
|
|
|
|
|
|
|
|
// Rebind the program for span output.
|
|
|
|
test_gl(glBindVertexArray, line_vertex_array_);
|
|
|
|
if(!qam_separation_shader_) {
|
|
|
|
output_shader_->bind();
|
|
|
|
}
|
2019-02-09 21:54:31 +00:00
|
|
|
}
|
2018-11-21 23:27:04 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
// Upload.
|
|
|
|
const auto buffer_size = lines * sizeof(Line);
|
|
|
|
if(!end_line || end_line > start_line) {
|
|
|
|
test_gl(glBufferSubData, GL_ARRAY_BUFFER, 0, GLsizeiptr(buffer_size), &line_buffer_[start_line]);
|
|
|
|
} else {
|
|
|
|
uint8_t *destination = static_cast<uint8_t *>(
|
|
|
|
glMapBufferRange(GL_ARRAY_BUFFER, 0, GLsizeiptr(buffer_size), GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT)
|
|
|
|
);
|
|
|
|
assert(destination);
|
|
|
|
test_gl_error();
|
|
|
|
|
|
|
|
const size_t buffer_length = line_buffer_.size() * sizeof(Line);
|
|
|
|
const size_t start_position = start_line * sizeof(Line);
|
|
|
|
memcpy(&destination[0], &line_buffer_[start_line], buffer_length - start_position);
|
|
|
|
memcpy(&destination[buffer_length - start_position], &line_buffer_[0], end_line * sizeof(Line));
|
|
|
|
|
|
|
|
test_gl(glFlushMappedBufferRange, GL_ARRAY_BUFFER, 0, GLsizeiptr(buffer_size));
|
|
|
|
test_gl(glUnmapBuffer, GL_ARRAY_BUFFER);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Produce colour information, if required.
|
|
|
|
if(qam_separation_shader_) {
|
|
|
|
qam_separation_shader_->bind();
|
|
|
|
qam_chroma_texture_->bind_framebuffer();
|
|
|
|
test_gl(glClear, GL_COLOR_BUFFER_BIT); // TODO: this is here as a hint that the old framebuffer doesn't need reloading;
|
|
|
|
// test whether that's a valid optimisation on desktop OpenGL.
|
2018-11-14 02:15:33 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
test_gl(glDisable, GL_BLEND);
|
|
|
|
test_gl(glDisable, GL_STENCIL_TEST);
|
|
|
|
test_gl(glDrawArraysInstanced, GL_TRIANGLE_STRIP, 0, 4, GLsizei(lines));
|
2019-02-09 21:54:31 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
accumulation_texture_->bind_framebuffer();
|
|
|
|
output_shader_->bind();
|
|
|
|
test_gl(glEnable, GL_BLEND);
|
|
|
|
test_gl(glEnable, GL_STENCIL_TEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render to the output.
|
2019-02-18 15:29:40 +00:00
|
|
|
test_gl(glDrawArraysInstanced, GL_TRIANGLE_STRIP, 0, 4, GLsizei(lines));
|
2019-02-09 21:54:31 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
start_line = end_line;
|
|
|
|
new_lines -= lines;
|
2019-02-09 21:54:31 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
// Disable blending and the stencil test again.
|
|
|
|
test_gl(glDisable, GL_STENCIL_TEST);
|
|
|
|
test_gl(glDisable, GL_BLEND);
|
2018-11-20 04:25:26 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
// That's it for operations affecting the accumulation buffer.
|
|
|
|
is_drawing_to_accumulation_buffer_.clear();
|
2019-03-03 00:33:28 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
// Grab a fence sync object to avoid busy waiting upon the next extry into this
|
|
|
|
// function, and reset the is_updating_ flag.
|
|
|
|
fence_ = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
|
2020-08-13 02:08:41 +00:00
|
|
|
complete_output_area(area);
|
2020-07-26 21:27:19 +00:00
|
|
|
});
|
2019-03-03 00:33:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScanTarget::draw(int output_width, int output_height) {
|
2020-07-26 21:27:19 +00:00
|
|
|
while(is_drawing_to_accumulation_buffer_.test_and_set(std::memory_order_acquire));
|
2019-03-03 00:33:28 +00:00
|
|
|
|
2019-03-03 20:04:14 +00:00
|
|
|
if(accumulation_texture_) {
|
|
|
|
// Copy the accumulation texture to the target.
|
|
|
|
test_gl(glBindFramebuffer, GL_FRAMEBUFFER, target_framebuffer_);
|
|
|
|
test_gl(glViewport, 0, 0, (GLsizei)output_width, (GLsizei)output_height);
|
|
|
|
|
|
|
|
test_gl(glClearColor, 0.0f, 0.0f, 0.0f, 0.0f);
|
|
|
|
test_gl(glClear, GL_COLOR_BUFFER_BIT);
|
|
|
|
accumulation_texture_->bind_texture();
|
|
|
|
accumulation_texture_->draw(float(output_width) / float(output_height), 4.0f / 255.0f);
|
|
|
|
}
|
2018-11-19 02:39:11 +00:00
|
|
|
|
2020-07-26 21:27:19 +00:00
|
|
|
is_drawing_to_accumulation_buffer_.clear(std::memory_order_release);
|
2018-11-07 03:23:38 +00:00
|
|
|
}
|