2018-11-07 03:23:38 +00:00
|
|
|
//
|
|
|
|
// ScanTarget.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 05/11/2018.
|
|
|
|
// Copyright © 2018 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef ScanTarget_hpp
|
|
|
|
#define ScanTarget_hpp
|
|
|
|
|
|
|
|
#include "../ScanTarget.hpp"
|
2018-11-08 03:53:46 +00:00
|
|
|
#include "OpenGL.hpp"
|
2018-11-11 20:11:32 +00:00
|
|
|
#include "Primitives/TextureTarget.hpp"
|
2018-11-19 02:39:11 +00:00
|
|
|
#include "Primitives/Rectangle.hpp"
|
2018-11-07 03:23:38 +00:00
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <atomic>
|
|
|
|
#include <cstdint>
|
2018-11-12 02:41:13 +00:00
|
|
|
#include <string>
|
2018-11-07 03:23:38 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace Outputs {
|
|
|
|
namespace Display {
|
|
|
|
namespace OpenGL {
|
|
|
|
|
2018-11-20 04:25:26 +00:00
|
|
|
/*!
|
|
|
|
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.
|
|
|
|
*/
|
2018-11-07 03:23:38 +00:00
|
|
|
class ScanTarget: public Outputs::Display::ScanTarget {
|
|
|
|
public:
|
2018-11-08 03:53:46 +00:00
|
|
|
ScanTarget();
|
|
|
|
~ScanTarget();
|
2018-11-14 02:15:33 +00:00
|
|
|
void draw(bool synchronous, int output_width, int output_height);
|
2018-11-08 03:53:46 +00:00
|
|
|
|
2018-11-11 20:11:32 +00:00
|
|
|
private:
|
2018-11-14 04:08:51 +00:00
|
|
|
static constexpr int WriteAreaWidth = 2048;
|
|
|
|
static constexpr int WriteAreaHeight = 2048;
|
|
|
|
|
|
|
|
static constexpr int LineBufferWidth = 2048;
|
|
|
|
static constexpr int LineBufferHeight = 2048;
|
|
|
|
|
2018-11-08 03:53:46 +00:00
|
|
|
// Outputs::Display::ScanTarget overrides.
|
2018-11-07 03:23:38 +00:00
|
|
|
void set_modals(Modals) override;
|
2018-11-11 00:52:57 +00:00
|
|
|
Scan *begin_scan() override;
|
2018-11-11 02:10:33 +00:00
|
|
|
void end_scan() override;
|
2018-11-11 00:52:57 +00:00
|
|
|
uint8_t *begin_data(size_t required_length, size_t required_alignment) override;
|
|
|
|
void end_data(size_t actual_length) override;
|
2018-11-07 03:23:38 +00:00
|
|
|
void submit() override;
|
2018-11-11 20:11:32 +00:00
|
|
|
void announce(Event event, uint16_t x, uint16_t y) override;
|
2018-11-07 03:23:38 +00:00
|
|
|
|
|
|
|
// Extends the definition of a Scan to include two extra fields,
|
|
|
|
// relevant to the way that this scan target processes video.
|
2018-11-12 02:41:13 +00:00
|
|
|
struct Scan {
|
|
|
|
Outputs::Display::ScanTarget::Scan scan;
|
|
|
|
|
2018-11-07 03:23:38 +00:00
|
|
|
/// Stores the y coordinate that this scan's data is at, within the write area texture.
|
|
|
|
uint16_t data_y;
|
2018-11-12 02:41:13 +00:00
|
|
|
/// Stores the y coordinate of this scan within the line buffer.
|
|
|
|
uint16_t line;
|
2018-11-07 03:23:38 +00:00
|
|
|
};
|
|
|
|
|
2018-11-09 02:57:28 +00:00
|
|
|
struct PointerSet {
|
|
|
|
// 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.
|
2018-11-09 04:02:36 +00:00
|
|
|
int write_area = 0;
|
|
|
|
uint16_t scan_buffer = 0;
|
2018-11-12 02:41:13 +00:00
|
|
|
uint16_t line = 0;
|
2018-11-07 03:23:38 +00:00
|
|
|
};
|
|
|
|
|
2018-11-09 02:57:28 +00:00
|
|
|
/// 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_;
|
|
|
|
|
2018-11-07 03:23:38 +00:00
|
|
|
// Maintains a buffer of the most recent 3072 scans.
|
|
|
|
std::array<Scan, 3072> scan_buffer_;
|
2018-11-11 20:11:32 +00:00
|
|
|
|
2018-11-19 02:39:11 +00:00
|
|
|
// 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.
|
2018-11-12 02:41:13 +00:00
|
|
|
struct Line {
|
2018-11-11 20:11:32 +00:00
|
|
|
struct EndPoint {
|
|
|
|
uint16_t x, y;
|
|
|
|
} end_points[2];
|
2018-11-12 02:41:13 +00:00
|
|
|
uint16_t line;
|
2018-11-11 20:11:32 +00:00
|
|
|
};
|
2018-11-19 02:39:11 +00:00
|
|
|
struct LineMetadata {
|
|
|
|
bool is_first_in_frame;
|
2018-11-22 18:22:04 +00:00
|
|
|
bool previous_frame_was_complete;
|
2018-11-19 02:39:11 +00:00
|
|
|
};
|
2018-11-14 04:08:51 +00:00
|
|
|
std::array<Line, LineBufferHeight> line_buffer_;
|
2018-11-19 02:39:11 +00:00
|
|
|
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.
|
2018-11-11 21:22:14 +00:00
|
|
|
TextureTarget unprocessed_line_texture_;
|
2018-11-19 02:39:11 +00:00
|
|
|
|
|
|
|
// Scans are accumulated to the accumulation texture; the full-display
|
|
|
|
// rectangle is used to ensure untouched pixels properly decay.
|
|
|
|
std::unique_ptr<TextureTarget> accumulation_texture_;
|
|
|
|
Rectangle full_display_rectangle_;
|
2018-11-21 00:51:11 +00:00
|
|
|
bool stencil_is_valid_ = false;
|
2018-11-19 02:39:11 +00:00
|
|
|
|
|
|
|
// Ephemeral state that helps in line composition.
|
2018-11-12 02:41:13 +00:00
|
|
|
Line *active_line_ = nullptr;
|
2018-11-16 02:51:27 +00:00
|
|
|
int provided_scans_ = 0;
|
2018-11-19 02:39:11 +00:00
|
|
|
bool is_first_in_frame_ = true;
|
2018-11-22 18:22:04 +00:00
|
|
|
bool frame_was_complete_ = true;
|
2018-11-07 03:23:38 +00:00
|
|
|
|
2018-11-12 23:47:55 +00:00
|
|
|
// OpenGL storage handles for buffer data.
|
|
|
|
GLuint scan_buffer_name_ = 0, scan_vertex_array_ = 0;
|
|
|
|
GLuint line_buffer_name_ = 0, line_vertex_array_ = 0;
|
|
|
|
|
2018-11-13 00:10:48 +00:00
|
|
|
template <typename T> void allocate_buffer(const T &array, GLuint &buffer_name, GLuint &vertex_array_name);
|
2018-11-14 02:15:33 +00:00
|
|
|
template <typename T> void patch_buffer(const T &array, GLuint target, uint16_t submit_pointer, uint16_t read_pointer);
|
2018-11-12 23:47:55 +00:00
|
|
|
|
2018-11-07 03:23:38 +00:00
|
|
|
// Uses a texture to vend write areas.
|
|
|
|
std::vector<uint8_t> write_area_texture_;
|
|
|
|
size_t data_type_size_ = 0;
|
2018-11-09 04:02:36 +00:00
|
|
|
|
|
|
|
GLuint write_area_texture_name_ = 0;
|
|
|
|
bool texture_exists_ = false;
|
2018-11-07 03:23:38 +00:00
|
|
|
|
2018-11-11 02:10:33 +00:00
|
|
|
// Ephemeral information for the begin/end functions.
|
|
|
|
Scan *vended_scan_ = nullptr;
|
|
|
|
int vended_write_area_pointer_ = 0;
|
|
|
|
|
2018-11-07 03:23:38 +00:00
|
|
|
// Track allocation failures.
|
|
|
|
bool allocation_has_failed_ = false;
|
|
|
|
|
|
|
|
// Receives scan target modals.
|
|
|
|
Modals modals_;
|
2018-11-12 02:41:13 +00:00
|
|
|
|
|
|
|
enum class ShaderType {
|
|
|
|
Scan,
|
|
|
|
Line
|
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@returns A string containing GLSL code describing the standard set of
|
|
|
|
@c in and @c uniform variables to bind to the relevant struct
|
2018-11-12 04:23:42 +00:00
|
|
|
from [...]OpenGL::ScanTarget and a vertex function to provide
|
|
|
|
the standard varyings.
|
2018-11-12 02:41:13 +00:00
|
|
|
*/
|
2018-11-16 02:02:46 +00:00
|
|
|
static std::string glsl_globals(ShaderType type);
|
2018-11-12 23:56:54 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
*/
|
2018-11-16 02:21:54 +00:00
|
|
|
static std::string glsl_default_vertex_shader(ShaderType type);
|
2018-11-12 02:41:13 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Calls @c taret.enable_vertex_attribute_with_pointer to attach all
|
|
|
|
globals for shaders of @c type to @c target.
|
|
|
|
*/
|
|
|
|
void enable_vertex_attributes(ShaderType type, Shader &target);
|
2018-11-14 02:15:33 +00:00
|
|
|
void set_uniforms(ShaderType type, Shader &target);
|
2018-11-12 04:23:42 +00:00
|
|
|
|
2018-11-13 01:15:38 +00:00
|
|
|
GLsync fence_ = nullptr;
|
2018-11-13 03:52:26 +00:00
|
|
|
std::atomic_flag is_drawing_;
|
2018-11-13 23:33:44 +00:00
|
|
|
|
|
|
|
int processing_width_ = 0;
|
2018-11-14 02:15:33 +00:00
|
|
|
std::unique_ptr<Shader> input_shader_;
|
|
|
|
std::unique_ptr<Shader> output_shader_;
|
2018-11-16 02:02:46 +00:00
|
|
|
|
|
|
|
static std::unique_ptr<Shader> input_shader(InputDataType input_data_type, OutputType output_type);
|
2018-11-07 03:23:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* ScanTarget_hpp */
|