2016-11-17 01:20:49 +00:00
|
|
|
//
|
|
|
|
// ArrayBuilder.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 17/11/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef ArrayBuilder_hpp
|
|
|
|
#define ArrayBuilder_hpp
|
|
|
|
|
2016-12-06 23:48:30 +00:00
|
|
|
#include <functional>
|
2016-11-17 05:37:53 +00:00
|
|
|
#include <memory>
|
2016-12-06 23:48:30 +00:00
|
|
|
#include <vector>
|
2016-11-17 01:20:49 +00:00
|
|
|
|
|
|
|
#include "OpenGL.hpp"
|
|
|
|
|
|
|
|
namespace Outputs {
|
|
|
|
namespace CRT {
|
|
|
|
|
2016-11-17 02:39:30 +00:00
|
|
|
/*!
|
|
|
|
Owns two array buffers, an 'input' and an 'output' and vends pointers to allow an owner to write provisional data into those
|
|
|
|
plus a flush function to lock provisional data into place. Also supplies a submit method to transfer all currently locked
|
|
|
|
data to the GPU and bind_input/output methods to bind the internal buffers.
|
|
|
|
|
|
|
|
It is safe for one thread to communicate via the get_*_storage and flush inputs asynchronously from another that is making
|
|
|
|
use of the bind and submit outputs.
|
|
|
|
*/
|
2016-11-17 01:20:49 +00:00
|
|
|
class ArrayBuilder {
|
|
|
|
public:
|
2016-11-17 02:39:30 +00:00
|
|
|
/// Creates an instance of ArrayBuilder with @c output_size bytes of storage for the output buffer and
|
|
|
|
/// @c input_size bytes of storage for the input buffer.
|
2017-11-11 20:28:40 +00:00
|
|
|
ArrayBuilder(std::size_t input_size, std::size_t output_size);
|
2016-11-17 01:20:49 +00:00
|
|
|
|
2016-11-17 06:06:16 +00:00
|
|
|
/// Creates an instance of ArrayBuilder with @c output_size bytes of storage for the output buffer and
|
|
|
|
/// @c input_size bytes of storage for the input buffer that, rather than using OpenGL, will submit data
|
|
|
|
/// to the @c submission_function. [Teleological: this is provided as a testing hook.]
|
2017-11-11 20:28:40 +00:00
|
|
|
ArrayBuilder(std::size_t input_size, std::size_t output_size, std::function<void(bool is_input, uint8_t *, std::size_t)> submission_function);
|
2016-11-17 06:06:16 +00:00
|
|
|
|
2016-11-20 01:11:24 +00:00
|
|
|
/// Attempts to add @c size bytes to the input set.
|
|
|
|
/// @returns a pointer to the allocated area if allocation was possible; @c nullptr otherwise.
|
2017-11-11 20:28:40 +00:00
|
|
|
uint8_t *get_input_storage(std::size_t size);
|
2016-11-17 04:26:04 +00:00
|
|
|
|
2016-11-20 01:11:24 +00:00
|
|
|
/// Attempts to add @c size bytes to the output set.
|
|
|
|
/// @returns a pointer to the allocated area if allocation was possible; @c nullptr otherwise.
|
2017-11-11 20:28:40 +00:00
|
|
|
uint8_t *get_output_storage(std::size_t size);
|
2016-11-17 04:26:04 +00:00
|
|
|
|
2016-11-20 01:11:24 +00:00
|
|
|
/// @returns @c true if either of the input or output storage areas is currently exhausted; @c false otherwise.
|
2016-11-17 04:26:04 +00:00
|
|
|
bool is_full();
|
2016-11-20 01:11:24 +00:00
|
|
|
|
|
|
|
/// If neither input nor output was exhausted since the last flush, atomically commits both input and output
|
2016-12-03 23:19:12 +00:00
|
|
|
/// up to the currently allocated size for use upon the next @c submit, giving the supplied function a
|
|
|
|
/// chance to perform last-minute processing. Otherwise acts as a no-op.
|
2017-11-11 20:28:40 +00:00
|
|
|
void flush(const std::function<void(uint8_t *input, std::size_t input_size, uint8_t *output, std::size_t output_size)> &);
|
2016-11-17 01:20:49 +00:00
|
|
|
|
2016-11-20 01:11:24 +00:00
|
|
|
/// Binds the input array to GL_ARRAY_BUFFER.
|
2016-11-17 01:20:49 +00:00
|
|
|
void bind_input();
|
2016-11-20 01:11:24 +00:00
|
|
|
|
|
|
|
/// Binds the output array to GL_ARRAY_BUFFER.
|
2016-11-17 01:20:49 +00:00
|
|
|
void bind_output();
|
2016-11-17 02:39:30 +00:00
|
|
|
|
|
|
|
struct Submission {
|
2017-11-11 20:28:40 +00:00
|
|
|
std::size_t input_size, output_size;
|
2016-11-17 02:39:30 +00:00
|
|
|
};
|
2016-11-20 01:11:24 +00:00
|
|
|
|
|
|
|
/// Submits all flushed input and output data to the corresponding arrays.
|
|
|
|
/// @returns A @c Submission record, indicating how much data of each type was submitted.
|
2016-11-17 02:39:30 +00:00
|
|
|
Submission submit();
|
2016-11-17 01:20:49 +00:00
|
|
|
|
|
|
|
private:
|
2016-11-19 12:12:40 +00:00
|
|
|
class Buffer {
|
|
|
|
public:
|
2017-11-11 20:28:40 +00:00
|
|
|
Buffer(std::size_t size, std::function<void(bool is_input, uint8_t *, std::size_t)> submission_function);
|
2016-11-19 12:12:40 +00:00
|
|
|
~Buffer();
|
|
|
|
|
2017-11-11 20:28:40 +00:00
|
|
|
uint8_t *get_storage(std::size_t size);
|
|
|
|
uint8_t *get_unflushed(std::size_t &size);
|
2016-11-19 12:12:40 +00:00
|
|
|
|
|
|
|
void flush();
|
2017-11-11 20:28:40 +00:00
|
|
|
std::size_t submit(bool is_input);
|
2016-11-19 12:12:40 +00:00
|
|
|
void bind();
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
private:
|
2017-10-18 00:50:46 +00:00
|
|
|
bool is_full = false;
|
|
|
|
GLuint buffer = 0;
|
2017-11-11 20:28:40 +00:00
|
|
|
std::function<void(bool is_input, uint8_t *, std::size_t)> submission_function_;
|
2016-11-19 12:12:40 +00:00
|
|
|
std::vector<uint8_t> data;
|
2017-11-11 20:28:40 +00:00
|
|
|
std::size_t allocated_data = 0;
|
|
|
|
std::size_t flushed_data = 0;
|
|
|
|
std::size_t submitted_data = 0;
|
2016-11-17 01:20:49 +00:00
|
|
|
} output_, input_;
|
2017-11-11 20:28:40 +00:00
|
|
|
uint8_t *get_storage(std::size_t size, Buffer &buffer);
|
2016-11-17 04:26:04 +00:00
|
|
|
|
2017-10-18 00:50:46 +00:00
|
|
|
bool is_full_ = false;
|
2016-11-17 01:20:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* ArrayBuilder_hpp */
|