Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ add_test(NAME Logging COMMAND FunGame Test Logging)
add_test(NAME ChunkDataTest COMMAND FunGame Test ChunkDataTest)
add_test(NAME LoadManifest COMMAND FunGame Test LoadManifest)
add_test(NAME PathFinderTest COMMAND FunGame Test PathFinderTest)
add_test(NAME ColorImageTest COMMAND FunGame Test ColorImageTest)
add_test(NAME LuaMap COMMAND FunGame Test Lua Map)
add_test(NAME LuaLogging COMMAND FunGame Test Lua Logging)
add_test(NAME LuaLoadTime COMMAND FunGame Test Lua LoadTime)
add_test(NAME LuaLoadScript COMMAND FunGame Test Lua LoadScript)
add_test(NAME LuaTransferScript COMMAND FunGame Test Lua TransferScript)

58 changes: 23 additions & 35 deletions src/gui/render/gpu_data/frame_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ FrameBufferBase::copy_to(
);
}

std::shared_ptr<util::image::Image>
std::expected<util::image::ImageVariant, int>
FrameBufferBase::read_data(int8_t color_component) const {
return read_data(0, 0, width_, height_, color_component);
}

std::shared_ptr<util::image::Image>
std::expected<util::image::ImageVariant, int>
FrameBufferBase::read_data(
screen_size_t start_w, screen_size_t start_h, screen_size_t image_w,
screen_size_t image_h, int8_t color_component
Expand All @@ -72,10 +72,11 @@ FrameBufferBase::read_data(
FrameBufferHandler& fbh = FrameBufferHandler::instance();
fbh.bind_fbo(frame_buffer); // need to crate a locking accessor

// choose which component we are reading from
if (color_component == DEPTH_COMPONENT_ID) {
if (!depth_buffer_) {
LOG_ERROR(logging::opengl_logger, "No depth Component to read from");
return {};
return std::unexpected(1);
}
type = depth_buffer_->get_type();
format = depth_buffer_->get_format();
Expand All @@ -87,15 +88,15 @@ FrameBufferBase::read_data(
logging::opengl_logger, "Color component {} does not exist.",
color_component
);
return {};
return std::unexpected(1);
}
const auto& color_texture = render_texture_.at(color_component);
if (!color_texture) {
LOG_ERROR(
logging::opengl_logger, "Color component {} exist, but has no value.",
color_component
);
return {};
return std::unexpected(1);
}
type = color_texture->get_type();
format = color_texture->get_format();
Expand All @@ -111,46 +112,33 @@ FrameBufferBase::read_data(
read_format = GPUPixelReadFormat::RGBA;
break;
default:
LOG_ERROR(logging::opengl_logger, "Cannot read from depth texture");
return {};
LOG_ERROR(logging::opengl_logger, "No known format.");
return std::unexpected(2);
}

// GLuint attachment = GL_COLOR_ATTACHMENT0 + color_component;
}
// type
// number of
size_t format_size = get_size(read_format);
size_t type_size = get_size(type) / sizeof(char);
std::shared_ptr<char[]> data(new char[image_w * image_h * format_size * type_size]);

glReadPixels(
start_w, start_h, image_w, image_h, static_cast<GLenum>(read_format),
static_cast<GLenum>(type), data.get()
);

if (type != GPUPixelType::FLOAT) {
LOG_ERROR(logging::opengl_logger, "NOT IMPLEMENTED MUST USE FLOAT");
return std::unexpected(3);
}

switch (format) {
case GPUPixelStorageFormat::RED:
case GPUPixelStorageFormat::DEPTH:
// in this case format_size should be 1
return std::make_shared<util::image::FloatMonochromeImage>(
data, image_w, image_h, format_size
);
case GPUPixelStorageFormat::RGB:
return std::make_shared<util::image::FloatPolychromeImage>(
data, image_w, image_h, format_size
);
case GPUPixelStorageFormat::RGBA:
return std::make_shared<util::image::FloatPolychromeAlphaImage>(
data, image_w, image_h, format_size
);
util::image::ImageVariant out = util::image::make_image(GPUPixelType::FLOAT, read_format, image_w, image_h);

default:
return {};
}
const auto visitor = util::image::ImageVisitor(
[this, start_w, start_h, read_format](auto&& image) {

glReadPixels(
start_w, start_h, image.get_width(), image.get_height(), static_cast<GLenum>(read_format),
static_cast<GLenum>(GPUPixelType::FLOAT), image.get_raw_data()
);
}
);

std::visit(visitor, out);

return out;
}

} // namespace gpu_data
Expand Down
5 changes: 3 additions & 2 deletions src/gui/render/gpu_data/frame_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,11 @@ class FrameBufferBase {
std::array<screen_size_t, 8> params
) const;

std::shared_ptr<util::image::Image>
std::expected<util::image::ImageVariant, int>
read_data(int8_t color_component = DEPTH_COMPONENT_ID) const;

std::shared_ptr<util::image::Image> read_data(
std::expected<util::image::ImageVariant, int>
read_data(
screen_size_t start_w, screen_size_t start_h, screen_size_t image_w,
screen_size_t image_h, int8_t color_component = DEPTH_COMPONENT_ID
) const;
Expand Down
168 changes: 47 additions & 121 deletions src/gui/render/gpu_data/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Texture2D::connect_depth_texture(GLuint framebuffer_ID) {
}

void
Texture2D::setup(std::shared_ptr<util::image::Image> image) {
Texture2D::load_settings() {
#if DEBUG()
GlobalContext& context = GlobalContext::instance();
if (!context.is_main_thread()) {
Expand All @@ -98,22 +98,22 @@ Texture2D::setup(std::shared_ptr<util::image::Image> image) {
glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, settings_.compare_mode);
}
// set other paremeters
}

if (image) {
load_data(image);
void
Texture2D::setup() {
// bind
if (settings_.multisample) {
glTexImage2DMultisample(
GL_TEXTURE_2D_MULTISAMPLE, settings_.samples,
static_cast<GLenum>(settings_.internal_format), width_, height_, GL_TRUE
);
} else {
if (settings_.multisample) {
glTexImage2DMultisample(
GL_TEXTURE_2D_MULTISAMPLE, settings_.samples,
static_cast<GLenum>(settings_.internal_format), width_, height_, GL_TRUE
);
} else {
glTexImage2D(
GL_TEXTURE_2D, 0, static_cast<GLenum>(settings_.internal_format),
width_, height_, 0, static_cast<GLenum>(settings_.read_format),
static_cast<GLenum>(settings_.type), nullptr
);
}
glTexImage2D(
GL_TEXTURE_2D, 0, static_cast<GLenum>(settings_.internal_format),
width_, height_, 0, static_cast<GLenum>(settings_.read_format),
static_cast<GLenum>(settings_.type), nullptr
);
}
}

Expand All @@ -122,65 +122,50 @@ Texture2D::Texture2D(
) : width_(width), height_(height), settings_(settings) {
if (differed) {
GlobalContext& context = GlobalContext::instance();
context.push_opengl_task([this]() { setup(nullptr); });
context.push_opengl_task([this]() { load_settings(); setup(); });
} else {
setup(nullptr);
load_settings();
setup();
}
}

Texture2D::Texture2D(
std::shared_ptr<util::image::Image> image, TextureSettings settings, bool differed
) : settings_(settings) {
if (!image) {
return;
}
width_ = image->get_width();
height_ = image->get_height();
util::image::ImageVariant image, TextureSettings settings, bool differed
) :
settings_(settings) {
// width_ = image->get_width();
// height_ = image->get_height();

if (differed) {
GlobalContext& context = GlobalContext::instance();
context.push_opengl_task([this, image]() { setup(image); });
context.push_opengl_task([this, image]() { load_settings(); load_image(image); });
} else {
setup(image);
load_settings();
load_image(image);
}
}

void
Texture2D::load_data(std::shared_ptr<util::image::Image> image) {
// TODO lots of checks
if (settings_.multisample) {
LOG_ERROR(logging::opengl_logger, "Cannot write data to multisample texture");
return;
}
if (settings_.internal_format == gpu_data::GPUPixelStorageFormat::DEPTH) {
//
}
width_ = image->get_width();
height_ = image->get_height();
glTexImage2D(
GL_TEXTURE_2D, 0, static_cast<GLenum>(settings_.internal_format),
image->get_width(), image->get_height(), 0,
static_cast<GLenum>(settings_.read_format), static_cast<GLenum>(settings_.type),
image->data()
Texture2D::load_image(util::image::ImageVariant image) {
const auto visitor = util::image::ImageVisitor(
[this](auto&& image) {this->load_data(image);}
);
if (settings_.type == GPUPixelType::FLOAT
|| settings_.type == GPUPixelType::HALF_FLOAT) {
glGenerateMipmap(GL_TEXTURE_2D);
}

std::visit(visitor, image);
}

std::shared_ptr<util::image::Image>
std::expected<util::image::ImageVariant, int>
Texture2D::get_image() const {
// multiplying by 2 fixes the memory error, but that can't possibly be correct.
// TODO GL_PACK_ALIGNMENT
size_t data_size = width_ * height_ * get_size(settings_.type)
* get_size(settings_.read_format) * 2;

std::shared_ptr<char[]> data = std::make_shared<char[]>(data_size);
// std::shared_ptr<char[]> data = std::make_shared<char[]>(data_size);

if (settings_.multisample) {
LOG_ERROR(logging::opengl_logger, "Cannot load multisample texture to image.");
return nullptr;
return std::unexpected(1);
}
glBindTexture(GL_TEXTURE_2D, texture_ID_);

Expand Down Expand Up @@ -214,81 +199,22 @@ Texture2D::get_image() const {

#endif

glGetTexImage(
GL_TEXTURE_2D, 0, static_cast<GLenum>(settings_.read_format),
static_cast<GLenum>(settings_.type), data.get()
);
util::image::ImageVariant out = util::image::make_image(settings_.type, settings_.read_format, width_, height_);

switch (settings_.type) {
case GPUPixelType::FLOAT:
case GPUPixelType::HALF_FLOAT:
switch (settings_.read_format) {
case GPUPixelReadFormat::DEPTH_COMPONENT:
case GPUPixelReadFormat::DEPTH_STENCIL:
case GPUPixelReadFormat::RED:
case GPUPixelReadFormat::GREEN:
case GPUPixelReadFormat::BLUE:
return std::make_shared<util::image::FloatMonochromeImage>(
data, width_, height_, get_size(settings_.type)
);
case GPUPixelReadFormat::RGB:
case GPUPixelReadFormat::BGR:
return std::make_shared<util::image::FloatPolychromeImage>(
data, width_, height_, get_size(settings_.type)
);

case GPUPixelReadFormat::RGBA:
case GPUPixelReadFormat::BGRA:
return std::make_shared<util::image::FloatPolychromeAlphaImage>(
data, width_, height_, get_size(settings_.type)
);

default:
LOG_ERROR(
logging::opengl_logger,
"Cannot load image. Unknown read_format {}.",
static_cast<GLenum>(settings_.read_format)
);
return nullptr;
}
case GPUPixelType::UNSIGNED_BYTE:
switch (settings_.read_format) {
case GPUPixelReadFormat::DEPTH_COMPONENT:
case GPUPixelReadFormat::DEPTH_STENCIL:
case GPUPixelReadFormat::RED:
case GPUPixelReadFormat::GREEN:
case GPUPixelReadFormat::BLUE:
return std::make_shared<util::image::ByteMonochromeImage>(
data, width_, height_, get_size(settings_.type)
);
case GPUPixelReadFormat::RGB:
case GPUPixelReadFormat::BGR:
return std::make_shared<util::image::BytePolychromeImage>(
data, width_, height_, get_size(settings_.type)
);

case GPUPixelReadFormat::RGBA:
case GPUPixelReadFormat::BGRA:
return std::make_shared<util::image::BytePolychromeAlphaImage>(
data, width_, height_, get_size(settings_.type)
);

default:
LOG_ERROR(
logging::opengl_logger,
"Cannot load image. Unknown read_format {}.",
static_cast<GLenum>(settings_.read_format)
);
return nullptr;
}
default:
LOG_ERROR(
logging::opengl_logger, "Cannot load image. Unknown type {}.",
static_cast<GLenum>(settings_.type)
const auto visitor = util::image::ImageVisitor(
[this](auto&& image) {
glGetTexImage(
GL_TEXTURE_2D, 0, static_cast<GLenum>(this->settings_.read_format),
static_cast<GLenum>(this->settings_.type), static_cast<void*>(image.get_raw_data())
);
return nullptr;
}
);

std::visit(visitor, out);

return out;

}
}

} // namespace gpu_data

Expand Down
Loading