simplified texture and framebuffer create methods

This commit is contained in:
Noel Berry 2021-03-24 00:56:43 -07:00
parent 1570e9becf
commit bd9736df03
5 changed files with 25 additions and 55 deletions

View File

@ -17,6 +17,7 @@ namespace Blah
// Up to 4 color attachments + 1 depth/stencil // Up to 4 color attachments + 1 depth/stencil
using Attachments = StackVector<TextureRef, 5>; using Attachments = StackVector<TextureRef, 5>;
using AttachmentFormats = StackVector<TextureFormat, 5>;
class FrameBuffer; class FrameBuffer;
using FrameBufferRef = std::shared_ptr<FrameBuffer>; using FrameBufferRef = std::shared_ptr<FrameBuffer>;
@ -44,7 +45,7 @@ namespace Blah
// Creates a new FrameBuffer with the given Texture Attachments. You must provide at least one Attachment. // Creates a new FrameBuffer with the given Texture Attachments. You must provide at least one Attachment.
// If the FrameBuffer creation fails, it will return an invalid FrameBufferRef. // If the FrameBuffer creation fails, it will return an invalid FrameBufferRef.
static FrameBufferRef create(int width, int height, const TextureFormat* attachments, int attachmentCount); static FrameBufferRef create(int width, int height, const AttachmentFormats& attachments);
// Gets the list of Attachments from the FrameBuffer // Gets the list of Attachments from the FrameBuffer
virtual Attachments& attachments() = 0; virtual Attachments& attachments() = 0;

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <memory> #include <memory>
#include <blah/core/filesystem.h>
namespace Blah namespace Blah
{ {
@ -50,12 +51,9 @@ namespace Blah
static TextureRef create(const Image& image); static TextureRef create(const Image& image);
// Creates a new Texture. // Creates a new Texture.
// If image data is provided, it should be the full size of the texture.
// If the Texture creation fails, it will return an invalid TextureRef. // If the Texture creation fails, it will return an invalid TextureRef.
static TextureRef create(int width, int height, unsigned char* rgba); static TextureRef create(int width, int height, TextureFormat format, unsigned char* data = nullptr);
// Creates a new Texture.
// If the Texture creation fails, it will return an invalid TextureRef.
static TextureRef create(int width, int height, TextureFormat format);
// Creates a new Texture from a Stream. // Creates a new Texture from a Stream.
// If the Texture creation fails, it will return an invalid TextureRef. // If the Texture creation fails, it will return an invalid TextureRef.
@ -63,7 +61,7 @@ namespace Blah
// Creates a new Texture from a File. // Creates a new Texture from a File.
// If the Texture creation fails, it will return an invalid TextureRef. // If the Texture creation fails, it will return an invalid TextureRef.
static TextureRef create(const char* file); static TextureRef create(const FilePath& file);
// gets the width of the texture // gets the width of the texture
virtual int width() const = 0; virtual int width() const = 0;
@ -75,12 +73,11 @@ namespace Blah
virtual TextureFormat format() const = 0; virtual TextureFormat format() const = 0;
// Sets the data of the Texture. // Sets the data of the Texture.
// Note that the pixel buffer should be in the same format as the Texture. There is no row padding. // Note that the data should be the same format and size as the Texture. There is no row padding.
// If the pixel buffer isn't the same size as the texture, it will set the minimum available amount of data.
virtual void set_data(unsigned char* data) = 0; virtual void set_data(unsigned char* data) = 0;
// Gets the data of the Texture. // Gets the data of the Texture.
// Note that the pixel buffer will be written to in the same format as the Texture, // Note that the data will be written to in the same format as the Texture,
// and you should allocate enough space for the full texture. There is no row padding. // and you should allocate enough space for the full texture. There is no row padding.
virtual void get_data(unsigned char* data) = 0; virtual void get_data(unsigned char* data) = 0;

View File

@ -5,20 +5,18 @@ using namespace Blah;
FrameBufferRef FrameBuffer::create(int width, int height) FrameBufferRef FrameBuffer::create(int width, int height)
{ {
static const TextureFormat attachment = TextureFormat::RGBA; return create(width, height, { TextureFormat::RGBA });
return create(width, height, &attachment, 1);
} }
FrameBufferRef FrameBuffer::create(int width, int height, const TextureFormat* attachments, int attachment_count) FrameBufferRef FrameBuffer::create(int width, int height, const AttachmentFormats& attachments)
{ {
BLAH_ASSERT(width > 0 && height > 0, "FrameBuffer width and height must be larger than 0"); BLAH_ASSERT(width > 0 && height > 0, "FrameBuffer width and height must be larger than 0");
BLAH_ASSERT(attachment_count <= Attachments::MaxCapacity, "Exceeded maximum attachment count"); BLAH_ASSERT(attachments.size() > 0, "At least one attachment must be provided");
BLAH_ASSERT(attachment_count > 0, "At least one attachment must be provided");
int color_count = 0; int color_count = 0;
int depth_count = 0; int depth_count = 0;
for (int i = 0; i < attachment_count; i++) for (int i = 0; i < attachments.size(); i++)
{ {
BLAH_ASSERT((int)attachments[i] > (int)TextureFormat::None && (int)attachments[i] < (int)TextureFormat::Count, "Invalid texture format"); BLAH_ASSERT((int)attachments[i] > (int)TextureFormat::None && (int)attachments[i] < (int)TextureFormat::Count, "Invalid texture format");
@ -31,5 +29,5 @@ FrameBufferRef FrameBuffer::create(int width, int height, const TextureFormat* a
BLAH_ASSERT(depth_count <= 1, "FrameBuffer can only have 1 Depth/Stencil Texture"); BLAH_ASSERT(depth_count <= 1, "FrameBuffer can only have 1 Depth/Stencil Texture");
BLAH_ASSERT(color_count <= Attachments::MaxCapacity - 1, "Exceeded maximum Color attachment count"); BLAH_ASSERT(color_count <= Attachments::MaxCapacity - 1, "Exceeded maximum Color attachment count");
return GraphicsBackend::create_framebuffer(width, height, attachments, attachment_count); return GraphicsBackend::create_framebuffer(width, height, attachments.data(), attachments.size());
} }

View File

@ -335,9 +335,9 @@ const float* Material::get_value(const char* name, i64* length) const
offset += calc_uniform_size(uniform); offset += calc_uniform_size(uniform);
} }
Log::warn("No Uniform '%s' exists", name);
*length = 0; *length = 0;
return nullptr; return nullptr;
Log::warn("No Uniform '%s' exists", name);
} }
const Vector<TextureRef>& Material::textures() const const Vector<TextureRef>& Material::textures() const

View File

@ -8,54 +8,28 @@ using namespace Blah;
TextureRef Texture::create(const Image& image) TextureRef Texture::create(const Image& image)
{ {
auto tex = create(image.width, image.height, TextureFormat::RGBA); return create(image.width, image.height, TextureFormat::RGBA, (unsigned char*)image.pixels);
if (tex)
tex->set_data((unsigned char*)image.pixels);
return tex;
} }
TextureRef Texture::create(int width, int height, unsigned char* rgba) TextureRef Texture::create(int width, int height, TextureFormat format, unsigned char* data)
{
auto tex = create(width, height, TextureFormat::RGBA);
if (tex)
tex->set_data(rgba);
return tex;
}
TextureRef Texture::create(int width, int height, TextureFormat format)
{ {
BLAH_ASSERT(width > 0 && height > 0, "Texture width and height must be larger than 0"); BLAH_ASSERT(width > 0 && height > 0, "Texture width and height must be larger than 0");
BLAH_ASSERT((int)format > (int)TextureFormat::None && (int)format < (int)TextureFormat::Count, "Invalid texture format"); BLAH_ASSERT((int)format > (int)TextureFormat::None && (int)format < (int)TextureFormat::Count, "Invalid texture format");
return GraphicsBackend::create_texture(width, height, format); auto tex = GraphicsBackend::create_texture(width, height, format);
if (tex && data != nullptr)
tex->set_data(data);
return tex;
} }
TextureRef Texture::create(Stream& stream) TextureRef Texture::create(Stream& stream)
{ {
Image img = Image(stream); return create(Image(stream));
if (img.pixels && img.width > 0 && img.height > 0)
{
auto tex = create(img.width, img.height, TextureFormat::RGBA);
if (tex)
tex->set_data((unsigned char*)img.pixels);
return tex;
}
return TextureRef();
} }
TextureRef Texture::create(const char* file) TextureRef Texture::create(const FilePath& file)
{ {
Image img = Image(file); return create(Image(file));
if (img.pixels)
{
auto tex = create(img.width, img.height, TextureFormat::RGBA);
if (tex)
tex->set_data((unsigned char*)img.pixels);
return tex;
}
return TextureRef();
} }