2016-02-14 01:52:23 +00:00
|
|
|
//
|
2016-11-16 14:57:17 +00:00
|
|
|
// TextureBuilder.cpp
|
2016-02-14 01:52:23 +00:00
|
|
|
// Clock Signal
|
|
|
|
//
|
2016-03-09 01:59:16 +00:00
|
|
|
// Created by Thomas Harte on 08/03/2016.
|
2016-02-14 01:52:23 +00:00
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2016-11-16 14:57:17 +00:00
|
|
|
#include "TextureBuilder.hpp"
|
2016-02-14 01:52:23 +00:00
|
|
|
#include "CRTOpenGL.hpp"
|
2016-11-16 15:13:06 +00:00
|
|
|
#include "OpenGL.hpp"
|
2016-03-09 01:59:16 +00:00
|
|
|
#include <string.h>
|
2016-02-14 01:52:23 +00:00
|
|
|
|
2016-03-09 01:59:16 +00:00
|
|
|
using namespace Outputs::CRT;
|
2016-02-14 01:52:23 +00:00
|
|
|
|
2016-11-16 15:13:06 +00:00
|
|
|
static const GLint internalFormatForDepth(size_t depth)
|
|
|
|
{
|
|
|
|
switch(depth)
|
|
|
|
{
|
|
|
|
default: return GL_FALSE;
|
|
|
|
case 1: return GL_R8UI;
|
|
|
|
case 2: return GL_RG8UI;
|
|
|
|
case 3: return GL_RGB8UI;
|
|
|
|
case 4: return GL_RGBA8UI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const GLenum formatForDepth(size_t depth)
|
|
|
|
{
|
|
|
|
switch(depth)
|
|
|
|
{
|
|
|
|
default: return GL_FALSE;
|
|
|
|
case 1: return GL_RED_INTEGER;
|
|
|
|
case 2: return GL_RG_INTEGER;
|
|
|
|
case 3: return GL_RGB_INTEGER;
|
|
|
|
case 4: return GL_RGBA_INTEGER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-17 04:26:04 +00:00
|
|
|
TextureBuilder::TextureBuilder(size_t bytes_per_pixel, GLenum texture_unit) :
|
2016-11-16 15:13:06 +00:00
|
|
|
bytes_per_pixel_(bytes_per_pixel),
|
2016-12-06 12:24:07 +00:00
|
|
|
write_areas_start_x_(0),
|
|
|
|
write_areas_start_y_(0),
|
|
|
|
is_full_(false),
|
2016-12-06 12:26:23 +00:00
|
|
|
did_submit_(false),
|
|
|
|
number_of_write_areas_(0)
|
2016-11-16 04:31:32 +00:00
|
|
|
{
|
2016-11-16 15:13:06 +00:00
|
|
|
image_.resize(bytes_per_pixel * InputBufferBuilderWidth * InputBufferBuilderHeight);
|
|
|
|
glGenTextures(1, &texture_name_);
|
2016-11-17 04:26:04 +00:00
|
|
|
|
|
|
|
glActiveTexture(texture_unit);
|
2016-11-16 15:13:06 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, texture_name_);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, internalFormatForDepth(bytes_per_pixel), InputBufferBuilderWidth, InputBufferBuilderHeight, 0, formatForDepth(bytes_per_pixel), GL_UNSIGNED_BYTE, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
TextureBuilder::~TextureBuilder()
|
|
|
|
{
|
|
|
|
glDeleteTextures(1, &texture_name_);
|
2016-11-16 04:31:32 +00:00
|
|
|
}
|
2016-03-20 02:46:17 +00:00
|
|
|
|
2016-12-06 12:24:07 +00:00
|
|
|
inline uint8_t *TextureBuilder::pointer_to_location(uint16_t x, uint16_t y)
|
|
|
|
{
|
|
|
|
return &image_[((y * InputBufferBuilderWidth) + x) * bytes_per_pixel_];
|
|
|
|
}
|
|
|
|
|
2016-11-16 14:57:17 +00:00
|
|
|
uint8_t *TextureBuilder::allocate_write_area(size_t required_length)
|
2016-02-14 01:52:23 +00:00
|
|
|
{
|
2016-12-06 12:24:07 +00:00
|
|
|
if(is_full_) return nullptr;
|
|
|
|
|
|
|
|
uint16_t starting_x, starting_y;
|
|
|
|
|
|
|
|
if(!number_of_write_areas_)
|
2016-02-14 01:52:23 +00:00
|
|
|
{
|
2016-12-06 12:24:07 +00:00
|
|
|
starting_x = write_areas_start_x_;
|
|
|
|
starting_y = write_areas_start_y_;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
starting_x = write_areas_[number_of_write_areas_ - 1].x + write_areas_[number_of_write_areas_ - 1].length + 1;
|
|
|
|
starting_y = write_areas_[number_of_write_areas_ - 1].y;
|
|
|
|
}
|
2016-02-14 01:52:23 +00:00
|
|
|
|
2016-12-06 12:24:07 +00:00
|
|
|
WriteArea next_write_area;
|
|
|
|
if(starting_x + required_length + 2 > InputBufferBuilderWidth)
|
|
|
|
{
|
|
|
|
starting_x = 0;
|
|
|
|
starting_y++;
|
2016-05-04 11:39:45 +00:00
|
|
|
|
2016-12-06 12:24:07 +00:00
|
|
|
if(starting_y == InputBufferBuilderHeight)
|
|
|
|
{
|
|
|
|
is_full_ = true;
|
|
|
|
return nullptr;
|
2016-05-04 11:39:45 +00:00
|
|
|
}
|
2016-05-07 22:37:18 +00:00
|
|
|
}
|
2016-11-16 05:15:50 +00:00
|
|
|
|
2016-12-06 12:24:07 +00:00
|
|
|
next_write_area.x = starting_x + 1;
|
|
|
|
next_write_area.y = starting_y;
|
|
|
|
next_write_area.length = (uint16_t)required_length;
|
|
|
|
if(number_of_write_areas_ < write_areas_.size())
|
|
|
|
write_areas_[number_of_write_areas_] = next_write_area;
|
|
|
|
else
|
|
|
|
write_areas_.push_back(next_write_area);
|
|
|
|
number_of_write_areas_++;
|
|
|
|
|
|
|
|
return pointer_to_location(next_write_area.x, next_write_area.y);
|
2016-05-07 22:37:18 +00:00
|
|
|
}
|
2016-05-05 11:22:49 +00:00
|
|
|
|
2016-11-16 14:57:17 +00:00
|
|
|
bool TextureBuilder::is_full()
|
2016-02-14 01:52:23 +00:00
|
|
|
{
|
2016-12-06 12:24:07 +00:00
|
|
|
return is_full_;
|
2016-05-09 11:02:12 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 14:57:17 +00:00
|
|
|
void TextureBuilder::reduce_previous_allocation_to(size_t actual_length)
|
2016-05-09 11:02:12 +00:00
|
|
|
{
|
2016-12-06 12:24:07 +00:00
|
|
|
if(is_full_) return;
|
2016-05-04 11:39:45 +00:00
|
|
|
|
2016-12-06 12:24:07 +00:00
|
|
|
WriteArea &write_area = write_areas_[number_of_write_areas_-1];
|
|
|
|
write_area.length = (uint16_t)actual_length;
|
2016-05-08 20:43:11 +00:00
|
|
|
|
2016-03-19 21:07:05 +00:00
|
|
|
// book end the allocation with duplicates of the first and last pixel, to protect
|
|
|
|
// against rounding errors when this run is drawn
|
2016-12-06 12:24:07 +00:00
|
|
|
uint8_t *start_pointer = pointer_to_location(write_area.x, write_area.y);
|
|
|
|
memcpy( &start_pointer[-bytes_per_pixel_],
|
|
|
|
start_pointer,
|
2016-11-16 15:13:06 +00:00
|
|
|
bytes_per_pixel_);
|
2016-02-14 01:52:23 +00:00
|
|
|
|
2016-12-06 12:24:07 +00:00
|
|
|
memcpy( &start_pointer[actual_length * bytes_per_pixel_],
|
|
|
|
&start_pointer[(actual_length - 1) * bytes_per_pixel_],
|
2016-11-16 15:13:06 +00:00
|
|
|
bytes_per_pixel_);
|
2016-05-05 01:27:10 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 15:13:06 +00:00
|
|
|
void TextureBuilder::submit()
|
2016-05-05 01:27:10 +00:00
|
|
|
{
|
2016-12-06 12:24:07 +00:00
|
|
|
uint16_t height = write_areas_start_y_ + (write_areas_start_x_ ? 1 : 0);
|
|
|
|
did_submit_ = true;
|
2016-11-16 15:13:06 +00:00
|
|
|
|
|
|
|
glTexSubImage2D( GL_TEXTURE_2D, 0,
|
|
|
|
0, 0,
|
|
|
|
InputBufferBuilderWidth, height,
|
|
|
|
formatForDepth(bytes_per_pixel_), GL_UNSIGNED_BYTE,
|
|
|
|
image_.data());
|
2016-05-05 01:27:10 +00:00
|
|
|
}
|
2016-12-06 12:24:07 +00:00
|
|
|
|
|
|
|
void TextureBuilder::flush(const std::function<void(const std::vector<WriteArea> &write_areas, size_t count)> &function)
|
|
|
|
{
|
|
|
|
bool was_full = is_full_;
|
|
|
|
if(did_submit_)
|
|
|
|
{
|
|
|
|
write_areas_start_y_ = write_areas_start_x_ = 0;
|
|
|
|
is_full_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(number_of_write_areas_ && !was_full)
|
|
|
|
{
|
2016-12-06 13:08:57 +00:00
|
|
|
if(write_areas_[0].x != write_areas_start_x_+1 || write_areas_[0].y != write_areas_start_y_)
|
2016-12-06 12:24:07 +00:00
|
|
|
{
|
2016-12-07 00:02:18 +00:00
|
|
|
for(size_t area = 0; area < number_of_write_areas_; area++)
|
2016-12-06 12:24:07 +00:00
|
|
|
{
|
2016-12-07 00:02:18 +00:00
|
|
|
WriteArea &write_area = write_areas_[area];
|
|
|
|
|
2016-12-06 12:24:07 +00:00
|
|
|
if(write_areas_start_x_ + write_area.length + 2 > InputBufferBuilderWidth)
|
|
|
|
{
|
|
|
|
write_areas_start_x_ = 0;
|
|
|
|
write_areas_start_y_ ++;
|
|
|
|
|
|
|
|
if(write_areas_start_y_ == InputBufferBuilderHeight)
|
|
|
|
{
|
|
|
|
is_full_ = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memmove(
|
|
|
|
pointer_to_location(write_areas_start_x_, write_areas_start_y_),
|
|
|
|
pointer_to_location(write_area.x - 1, write_area.y),
|
|
|
|
(write_area.length + 2) * bytes_per_pixel_);
|
|
|
|
write_area.x = write_areas_start_x_ + 1;
|
|
|
|
write_area.y = write_areas_start_y_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!is_full_)
|
2016-12-06 13:08:57 +00:00
|
|
|
{
|
2016-12-06 12:24:07 +00:00
|
|
|
function(write_areas_, number_of_write_areas_);
|
2016-12-06 13:08:57 +00:00
|
|
|
|
|
|
|
write_areas_start_x_ = write_areas_[number_of_write_areas_-1].x + write_areas_[number_of_write_areas_-1].length + 1;
|
|
|
|
write_areas_start_y_ = write_areas_[number_of_write_areas_-1].y;
|
|
|
|
}
|
2016-12-06 12:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
did_submit_ = false;
|
|
|
|
number_of_write_areas_ = 0;
|
|
|
|
}
|