2016-11-17 01:20:49 +00:00
|
|
|
//
|
|
|
|
// ArrayBuilder.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 17/11/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "ArrayBuilder.hpp"
|
|
|
|
|
2016-11-17 02:39:30 +00:00
|
|
|
using namespace Outputs::CRT;
|
|
|
|
|
|
|
|
ArrayBuilder::ArrayBuilder(size_t input_size, size_t output_size) :
|
2017-03-26 18:34:47 +00:00
|
|
|
output_(output_size, nullptr),
|
|
|
|
input_(input_size, nullptr) {}
|
2016-11-17 06:06:16 +00:00
|
|
|
|
|
|
|
ArrayBuilder::ArrayBuilder(size_t input_size, size_t output_size, std::function<void(bool is_input, uint8_t *, size_t)> submission_function) :
|
2017-03-26 18:34:47 +00:00
|
|
|
output_(output_size, submission_function),
|
|
|
|
input_(input_size, submission_function) {}
|
2016-11-17 02:39:30 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
bool ArrayBuilder::is_full() {
|
2016-11-17 04:26:04 +00:00
|
|
|
bool was_full;
|
|
|
|
was_full = is_full_;
|
|
|
|
return was_full;
|
2016-11-17 02:39:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
uint8_t *ArrayBuilder::get_input_storage(size_t size) {
|
2016-11-17 04:26:04 +00:00
|
|
|
return get_storage(size, input_);
|
2016-11-17 02:39:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
uint8_t *ArrayBuilder::get_output_storage(size_t size) {
|
2016-11-17 04:26:04 +00:00
|
|
|
return get_storage(size, output_);
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void ArrayBuilder::flush(const std::function<void(uint8_t *input, size_t input_size, uint8_t *output, size_t output_size)> &function) {
|
|
|
|
if(!is_full_) {
|
2017-02-11 18:36:09 +00:00
|
|
|
size_t input_size = 0, output_size = 0;
|
2016-12-03 23:19:12 +00:00
|
|
|
uint8_t *input = input_.get_unflushed(input_size);
|
|
|
|
uint8_t *output = output_.get_unflushed(output_size);
|
|
|
|
function(input, input_size, output, output_size);
|
|
|
|
|
2016-11-17 04:26:04 +00:00
|
|
|
input_.flush();
|
|
|
|
output_.flush();
|
|
|
|
}
|
2016-11-17 02:39:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void ArrayBuilder::bind_input() {
|
2016-11-17 02:39:30 +00:00
|
|
|
input_.bind();
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void ArrayBuilder::bind_output() {
|
2016-11-17 02:39:30 +00:00
|
|
|
output_.bind();
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
ArrayBuilder::Submission ArrayBuilder::submit() {
|
2016-11-17 02:39:30 +00:00
|
|
|
ArrayBuilder::Submission submission;
|
|
|
|
|
2016-11-17 06:06:16 +00:00
|
|
|
submission.input_size = input_.submit(true);
|
|
|
|
submission.output_size = output_.submit(false);
|
2017-03-26 18:34:47 +00:00
|
|
|
if(is_full_) {
|
2016-11-17 04:26:04 +00:00
|
|
|
is_full_ = false;
|
|
|
|
input_.reset();
|
|
|
|
output_.reset();
|
|
|
|
}
|
2016-11-17 02:39:30 +00:00
|
|
|
|
|
|
|
return submission;
|
|
|
|
}
|
|
|
|
|
2016-11-17 06:06:16 +00:00
|
|
|
ArrayBuilder::Buffer::Buffer(size_t size, std::function<void(bool is_input, uint8_t *, size_t)> submission_function) :
|
2017-03-26 18:34:47 +00:00
|
|
|
is_full(false),
|
|
|
|
submission_function_(submission_function),
|
|
|
|
allocated_data(0), flushed_data(0), submitted_data(0) {
|
|
|
|
if(!submission_function_) {
|
2016-11-17 06:06:16 +00:00
|
|
|
glGenBuffers(1, &buffer);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)size, NULL, GL_STREAM_DRAW);
|
|
|
|
}
|
2016-11-17 04:26:04 +00:00
|
|
|
data.resize(size);
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
ArrayBuilder::Buffer::~Buffer() {
|
2016-11-17 06:06:16 +00:00
|
|
|
if(!submission_function_)
|
|
|
|
glDeleteBuffers(1, &buffer);
|
2016-11-17 04:26:04 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
uint8_t *ArrayBuilder::get_storage(size_t size, Buffer &buffer) {
|
2016-11-17 04:26:04 +00:00
|
|
|
uint8_t *pointer = buffer.get_storage(size);
|
|
|
|
if(!pointer) is_full_ = true;
|
|
|
|
return pointer;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
uint8_t *ArrayBuilder::Buffer::get_storage(size_t size) {
|
|
|
|
if(is_full || allocated_data + size > data.size()) {
|
2016-11-17 04:26:04 +00:00
|
|
|
is_full = true;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-11-17 02:39:30 +00:00
|
|
|
uint8_t *pointer = &data[allocated_data];
|
|
|
|
allocated_data += size;
|
|
|
|
return pointer;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
uint8_t *ArrayBuilder::Buffer::get_unflushed(size_t &size) {
|
|
|
|
if(is_full) {
|
2016-11-17 04:26:04 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
size = allocated_data - flushed_data;
|
|
|
|
return &data[flushed_data];
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void ArrayBuilder::Buffer::flush() {
|
|
|
|
if(submitted_data) {
|
2017-01-29 21:12:48 +00:00
|
|
|
memmove(data.data(), &data[submitted_data], allocated_data - submitted_data);
|
2016-11-19 11:55:30 +00:00
|
|
|
allocated_data -= submitted_data;
|
|
|
|
flushed_data -= submitted_data;
|
2016-11-17 04:26:04 +00:00
|
|
|
submitted_data = 0;
|
2016-11-17 02:39:30 +00:00
|
|
|
}
|
2016-11-19 11:55:30 +00:00
|
|
|
|
|
|
|
flushed_data = allocated_data;
|
2016-11-17 02:39:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
size_t ArrayBuilder::Buffer::submit(bool is_input) {
|
2016-11-17 04:26:04 +00:00
|
|
|
size_t length = flushed_data;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(submission_function_) {
|
2016-11-17 06:06:16 +00:00
|
|
|
submission_function_(is_input, data.data(), length);
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-11-17 06:06:16 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
|
|
|
uint8_t *destination = (uint8_t *)glMapBufferRange(GL_ARRAY_BUFFER, 0, (GLsizeiptr)length, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT | GL_MAP_FLUSH_EXPLICIT_BIT);
|
|
|
|
memcpy(destination, data.data(), length);
|
|
|
|
glFlushMappedBufferRange(GL_ARRAY_BUFFER, 0, (GLsizeiptr)length);
|
|
|
|
glUnmapBuffer(GL_ARRAY_BUFFER);
|
|
|
|
}
|
2016-11-17 04:26:04 +00:00
|
|
|
submitted_data = flushed_data;
|
2016-11-17 02:39:30 +00:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void ArrayBuilder::Buffer::bind() {
|
2016-11-17 02:39:30 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, buffer);
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void ArrayBuilder::Buffer::reset() {
|
2016-11-17 04:26:04 +00:00
|
|
|
is_full = false;
|
2016-12-10 19:17:46 +00:00
|
|
|
allocated_data = 0;
|
|
|
|
flushed_data = 0;
|
|
|
|
submitted_data = 0;
|
2016-11-17 02:39:30 +00:00
|
|
|
}
|