1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-11-23 03:32:32 +00:00

Starts splitting ring-buffer stuff from OpenGL stuff.

Initially via two very codependent classes.
This commit is contained in:
Thomas Harte 2020-07-21 22:49:46 -04:00
parent e8cd5a0511
commit 0da5c07942
2 changed files with 146 additions and 109 deletions

View File

@ -119,7 +119,7 @@ void ScanTarget::set_target_framebuffer(GLuint target_framebuffer) {
is_updating_.clear();
}
void ScanTarget::set_modals(Modals modals) {
void BufferingScanTarget::set_modals(Modals modals) {
// Don't change the modals while drawing is ongoing; a previous set might be
// in the process of being established.
while(is_updating_.test_and_set());
@ -128,7 +128,7 @@ void ScanTarget::set_modals(Modals modals) {
is_updating_.clear();
}
Outputs::Display::ScanTarget::Scan *ScanTarget::begin_scan() {
Outputs::Display::ScanTarget::Scan *BufferingScanTarget::begin_scan() {
if(allocation_has_failed_) return nullptr;
std::lock_guard lock_guard(write_pointers_mutex_);
@ -154,7 +154,7 @@ Outputs::Display::ScanTarget::Scan *ScanTarget::begin_scan() {
return &result->scan;
}
void ScanTarget::end_scan() {
void BufferingScanTarget::end_scan() {
if(vended_scan_) {
std::lock_guard lock_guard(write_pointers_mutex_);
vended_scan_->data_y = TextureAddressGetY(vended_write_area_pointer_);
@ -176,7 +176,7 @@ void ScanTarget::end_scan() {
vended_scan_ = nullptr;
}
uint8_t *ScanTarget::begin_data(size_t required_length, size_t required_alignment) {
uint8_t *BufferingScanTarget::begin_data(size_t required_length, size_t required_alignment) {
assert(required_alignment);
if(allocation_has_failed_) return nullptr;
@ -226,7 +226,7 @@ uint8_t *ScanTarget::begin_data(size_t required_length, size_t required_alignmen
// write_pointers_.write_area points to the first pixel the client is expected to draw to.
}
void ScanTarget::end_data(size_t actual_length) {
void BufferingScanTarget::end_data(size_t actual_length) {
if(allocation_has_failed_ || !data_is_allocated_) return;
std::lock_guard lock_guard(write_pointers_mutex_);
@ -260,7 +260,7 @@ void ScanTarget::will_change_owner() {
vended_scan_ = nullptr;
}
void ScanTarget::announce(Event event, bool is_visible, const Outputs::Display::ScanTarget::Scan::EndPoint &location, uint8_t composite_amplitude) {
void BufferingScanTarget::announce(Event event, bool is_visible, const Outputs::Display::ScanTarget::Scan::EndPoint &location, uint8_t composite_amplitude) {
// Forward the event to the display metrics tracker.
display_metrics_.announce_event(event);
@ -400,7 +400,7 @@ void ScanTarget::setup_pipeline() {
input_shader_->set_uniform("textureName", GLint(SourceDataTextureUnit - GL_TEXTURE0));
}
Outputs::Display::Metrics &ScanTarget::display_metrics() {
const Outputs::Display::Metrics &BufferingScanTarget::display_metrics() {
return display_metrics_;
}

View File

@ -32,12 +32,150 @@ namespace Outputs {
namespace Display {
namespace OpenGL {
/*!
Provides basic thread-safe (hopefully) circular queues for any scan target that:
* will store incoming Scans into a linear circular buffer and pack regions of
incoming pixel data into a 2d texture;
* will compose whole lines of content by partioning the Scans based on sync
placement and then pasting together their content;
* will process those lines as necessary to map from input format to whatever
suits the display; and
* will then output the lines.
This buffer rejects new data when full.
*/
class BufferingScanTarget: public Outputs::Display::ScanTarget {
public:
/*! @returns The DisplayMetrics object that this ScanTarget has been providing with announcements and draw overages. */
const Metrics &display_metrics();
protected:
// Extends the definition of a Scan to include two extra fields,
// completing this scan's source data and destination locations.
struct Scan {
Outputs::Display::ScanTarget::Scan scan;
/// Stores the y coordinate for this scan's data within the write area texture.
/// Use this plus the scan's endpoints' data_offsets to locate this data in 2d.
uint16_t data_y;
/// Stores the y coordinate assigned to this scan within the intermediate buffers.
/// Use this plus this scan's endpoints' x locations to determine where to composite
/// this data for intermediate processing.
uint16_t line;
};
/// Defines the boundaries of a complete line of video — a 2d start and end location,
/// composite phase and amplitude (if relevant), the source line in the intermediate buffer
/// plus the start and end offsets of the area that is visible from the intermediate buffer.
struct Line {
struct EndPoint {
uint16_t x, y;
uint16_t cycles_since_end_of_horizontal_retrace;
int16_t composite_angle;
} end_points[2];
uint16_t line;
uint8_t composite_amplitude;
};
/// Provides additional metadata about lines; this is separate because it's unlikely to be of
/// interest to the GPU, unlike the fields in Line.
struct LineMetadata {
/// @c true if this line was the first drawn after vertical sync; @c false otherwise.
bool is_first_in_frame;
/// @c true if this line is the first in the frame and if every single piece of output
/// from the previous frame was recorded; @c false otherwise. Data can be dropped
/// from a frame if performance problems mean that the emulated machine is running
/// more quickly than complete frames can be generated.
bool previous_frame_was_complete;
};
// TODO: put this behind accessors.
std::atomic_flag is_updating_;
// These are safe to read if you have is_updating_.
Modals modals_;
bool modals_are_dirty_ = false;
// Track allocation failures.
bool data_is_allocated_ = false;
bool allocation_has_failed_ = false;
/// Maintains a buffer of the most recent scans.
// TODO: have the owner supply a buffer and its size.
// That'll allow owners to place this in shared video memory if possible.
std::array<Scan, 16384> scan_buffer_;
/// A mutex for gettng access to write_pointers_; access to write_pointers_,
/// data_type_size_ or write_area_texture_ is almost never contended, so this
/// is cheap for the main use case.
std::mutex write_pointers_mutex_;
struct PointerSet {
// This constructor is here to appease GCC's interpretation of
// an ambiguity in the C++ standard; cf. https://stackoverflow.com/questions/17430377
PointerSet() noexcept {}
// Squeezing this struct into 64 bits makes the std::atomics more likely
// to be lock free; they are under LLVM x86-64.
int write_area = 1; // By convention this points to the vended area. Which is preceded by a guard pixel. So a sensible default construction is write_area = 1.
uint16_t scan_buffer = 0;
uint16_t line = 0;
};
/// A pointer to the next thing that should be provided to the caller for data.
PointerSet write_pointers_;
/// A pointer to the final thing currently cleared for submission.
std::atomic<PointerSet> submit_pointers_;
/// A pointer to the first thing not yet submitted for display.
std::atomic<PointerSet> read_pointers_;
// Ephemeral state that helps in line composition.
Line *active_line_ = nullptr;
int provided_scans_ = 0;
bool is_first_in_frame_ = true;
bool frame_is_complete_ = true;
bool previous_frame_was_complete_ = true;
// Ephemeral information for the begin/end functions.
Scan *vended_scan_ = nullptr;
int vended_write_area_pointer_ = 0;
static constexpr int WriteAreaWidth = 2048;
static constexpr int WriteAreaHeight = 2048;
static constexpr int LineBufferWidth = 2048;
static constexpr int LineBufferHeight = 2048;
Metrics display_metrics_;
// Uses a texture to vend write areas.
std::vector<uint8_t> write_area_texture_;
size_t data_type_size_ = 0;
bool output_is_visible_ = false;
std::array<Line, LineBufferHeight> line_buffer_;
std::array<LineMetadata, LineBufferHeight> line_metadata_buffer_;
private:
// ScanTarget overrides.
void set_modals(Modals) final;
Outputs::Display::ScanTarget::Scan *begin_scan() final;
void end_scan() final;
uint8_t *begin_data(size_t required_length, size_t required_alignment) final;
void end_data(size_t actual_length) final;
void announce(Event event, bool is_visible, const Outputs::Display::ScanTarget::Scan::EndPoint &location, uint8_t colour_burst_amplitude) final;
};
/*!
Provides a ScanTarget that uses OpenGL to render its output;
this uses various internal buffers so that the only geometry
drawn to the target framebuffer is a quad.
*/
class ScanTarget: public Outputs::Display::ScanTarget {
class ScanTarget: public BufferingScanTarget {
public:
ScanTarget(GLuint target_framebuffer = 0, float output_gamma = 2.2f);
~ScanTarget();
@ -49,9 +187,6 @@ class ScanTarget: public Outputs::Display::ScanTarget {
/*! Processes all the latest input, at a resolution suitable for later output to a framebuffer of the specified size. */
void update(int output_width, int output_height);
/*! @returns The DisplayMetrics object that this ScanTarget has been providing with announcements and draw overages. */
Metrics &display_metrics();
private:
#ifndef NDEBUG
struct OpenGLVersionDumper {
@ -62,93 +197,18 @@ class ScanTarget: public Outputs::Display::ScanTarget {
} dumper_;
#endif
static constexpr int WriteAreaWidth = 2048;
static constexpr int WriteAreaHeight = 2048;
static constexpr int LineBufferWidth = 2048;
static constexpr int LineBufferHeight = 2048;
GLuint target_framebuffer_;
const float output_gamma_;
// Outputs::Display::ScanTarget finals.
void set_modals(Modals) final;
Scan *begin_scan() final;
void end_scan() final;
uint8_t *begin_data(size_t required_length, size_t required_alignment) final;
void end_data(size_t actual_length) final;
void announce(Event event, bool is_visible, const Outputs::Display::ScanTarget::Scan::EndPoint &location, uint8_t colour_burst_amplitude) final;
void will_change_owner() final;
bool output_is_visible_ = false;
Metrics display_metrics_;
int resolution_reduction_level_ = 1;
int output_height_ = 0;
size_t lines_submitted_ = 0;
std::chrono::high_resolution_clock::time_point line_submission_begin_time_;
// Extends the definition of a Scan to include two extra fields,
// relevant to the way that this scan target processes video.
struct Scan {
Outputs::Display::ScanTarget::Scan scan;
/// Stores the y coordinate that this scan's data is at, within the write area texture.
uint16_t data_y;
/// Stores the y coordinate of this scan within the line buffer.
uint16_t line;
};
struct PointerSet {
// This constructor is here to appease GCC's interpretation of
// an ambiguity in the C++ standard; cf. https://stackoverflow.com/questions/17430377
PointerSet() noexcept {}
// The sizes below might be less hassle as something more natural like ints,
// but squeezing this struct into 64 bits makes the std::atomics more likely
// to be lock free; they are under LLVM x86-64.
int write_area = 1; // By convention this points to the vended area. Which is preceded by a guard pixel. So a sensible default construction is write_area = 1.
uint16_t scan_buffer = 0;
uint16_t line = 0;
};
/// A pointer to the next thing that should be provided to the caller for data.
PointerSet write_pointers_;
/// A mutex for gettng access to write_pointers_; access to write_pointers_,
/// data_type_size_ or write_area_texture_ is almost never contended, so this
/// is cheap for the main use case.
std::mutex write_pointers_mutex_;
/// A pointer to the final thing currently cleared for submission.
std::atomic<PointerSet> submit_pointers_;
/// A pointer to the first thing not yet submitted for display.
std::atomic<PointerSet> read_pointers_;
/// Maintains a buffer of the most recent scans.
std::array<Scan, 16384> scan_buffer_;
// Maintains a list of composite scan buffer coordinates; the Line struct
// is transported to the GPU in its entirety; the LineMetadatas live in CPU
// space only.
struct Line {
struct EndPoint {
uint16_t x, y;
uint16_t cycles_since_end_of_horizontal_retrace;
int16_t composite_angle;
} end_points[2];
uint16_t line;
uint8_t composite_amplitude;
};
struct LineMetadata {
bool is_first_in_frame;
bool previous_frame_was_complete;
};
std::array<Line, LineBufferHeight> line_buffer_;
std::array<LineMetadata, LineBufferHeight> line_metadata_buffer_;
// Contains the first composition of scans into lines;
// they're accumulated prior to output to allow for continuous
// application of any necessary conversions — e.g. composite processing.
@ -164,13 +224,6 @@ class ScanTarget: public Outputs::Display::ScanTarget {
Rectangle full_display_rectangle_;
bool stencil_is_valid_ = false;
// Ephemeral state that helps in line composition.
Line *active_line_ = nullptr;
int provided_scans_ = 0;
bool is_first_in_frame_ = true;
bool frame_is_complete_ = true;
bool previous_frame_was_complete_ = true;
// OpenGL storage handles for buffer data.
GLuint scan_buffer_name_ = 0, scan_vertex_array_ = 0;
GLuint line_buffer_name_ = 0, line_vertex_array_ = 0;
@ -178,24 +231,10 @@ class ScanTarget: public Outputs::Display::ScanTarget {
template <typename T> void allocate_buffer(const T &array, GLuint &buffer_name, GLuint &vertex_array_name);
template <typename T> void patch_buffer(const T &array, GLuint target, uint16_t submit_pointer, uint16_t read_pointer);
// Uses a texture to vend write areas.
std::vector<uint8_t> write_area_texture_;
size_t data_type_size_ = 0;
GLuint write_area_texture_name_ = 0;
bool texture_exists_ = false;
// Ephemeral information for the begin/end functions.
Scan *vended_scan_ = nullptr;
int vended_write_area_pointer_ = 0;
// Track allocation failures.
bool data_is_allocated_ = false;
bool allocation_has_failed_ = false;
// Receives scan target modals.
Modals modals_;
bool modals_are_dirty_ = false;
void setup_pipeline();
enum class ShaderType {
@ -213,14 +252,12 @@ class ScanTarget: public Outputs::Display::ScanTarget {
std::vector<std::string> bindings(ShaderType type) const;
GLsync fence_ = nullptr;
std::atomic_flag is_updating_;
std::atomic_flag is_drawing_to_accumulation_buffer_;
std::unique_ptr<Shader> input_shader_;
std::unique_ptr<Shader> output_shader_;
std::unique_ptr<Shader> qam_separation_shader_;
/*!
Produces a shader that composes fragment of the input stream to a single buffer,
normalising the data into one of four forms: RGB, 8-bit luminance,