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
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <mutex>
|
2016-11-17 05:37:53 +00:00
|
|
|
#include <memory>
|
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.
|
|
|
|
ArrayBuilder(size_t input_size, 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.]
|
|
|
|
ArrayBuilder(size_t input_size, size_t output_size, std::function<void(bool is_input, uint8_t *, size_t)> submission_function);
|
|
|
|
|
2016-11-17 02:39:30 +00:00
|
|
|
/// Attempts to add @c size bytes
|
2016-11-17 01:20:49 +00:00
|
|
|
uint8_t *get_input_storage(size_t size);
|
2016-11-17 04:26:04 +00:00
|
|
|
uint8_t *reget_input_storage(size_t &size);
|
|
|
|
|
2016-11-17 01:20:49 +00:00
|
|
|
uint8_t *get_output_storage(size_t size);
|
2016-11-17 04:26:04 +00:00
|
|
|
uint8_t *reget_output_storage(size_t &size);
|
|
|
|
|
|
|
|
bool is_full();
|
2016-11-17 02:39:30 +00:00
|
|
|
void flush();
|
2016-11-17 01:20:49 +00:00
|
|
|
|
|
|
|
void bind_input();
|
|
|
|
void bind_output();
|
2016-11-17 02:39:30 +00:00
|
|
|
|
|
|
|
struct Submission {
|
|
|
|
size_t input_size, output_size;
|
|
|
|
};
|
|
|
|
Submission submit();
|
2016-11-17 01:20:49 +00:00
|
|
|
|
|
|
|
private:
|
2016-11-19 12:12:40 +00:00
|
|
|
class Buffer {
|
|
|
|
public:
|
|
|
|
Buffer(size_t size, std::function<void(bool is_input, uint8_t *, size_t)> submission_function);
|
|
|
|
~Buffer();
|
|
|
|
|
|
|
|
uint8_t *get_storage(size_t size);
|
|
|
|
uint8_t *reget_storage(size_t &size);
|
|
|
|
|
|
|
|
void flush();
|
|
|
|
size_t submit(bool is_input);
|
|
|
|
void bind();
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool is_full, was_reset;
|
|
|
|
GLuint buffer;
|
|
|
|
std::function<void(bool is_input, uint8_t *, size_t)> submission_function_;
|
|
|
|
std::vector<uint8_t> data;
|
|
|
|
size_t allocated_data;
|
|
|
|
size_t flushed_data;
|
|
|
|
size_t submitted_data;
|
2016-11-17 01:20:49 +00:00
|
|
|
} output_, input_;
|
2016-11-17 04:26:04 +00:00
|
|
|
uint8_t *get_storage(size_t size, Buffer &buffer);
|
|
|
|
|
2016-11-17 01:20:49 +00:00
|
|
|
std::mutex buffer_mutex_;
|
2016-11-17 04:26:04 +00:00
|
|
|
bool is_full_;
|
2016-11-17 06:06:16 +00:00
|
|
|
;
|
2016-11-17 01:20:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* ArrayBuilder_hpp */
|