Skip to content

Commit

Permalink
Merge pull request #26 from mortennobel/1.0.3
Browse files Browse the repository at this point in the history
1.0.3 Add RenderPass.blit(). Add depth/stencil as Texture type and add support for depth attachment in Framebuffer.
  • Loading branch information
mortennobel authored Feb 14, 2018
2 parents 0391f98 + afa12b0 commit 17c6d18
Show file tree
Hide file tree
Showing 25 changed files with 440 additions and 50 deletions.
2 changes: 1 addition & 1 deletion examples/render-to-texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class RenderToTextureExample {

texture = Texture::create().withRGBData(nullptr, 1024,1024).build();

framebuffer = Framebuffer::create().withTexture(texture).build();
framebuffer = Framebuffer::create().withColorTexture(texture).build();

materialOffscreen = Shader::getStandardBlinnPhong()->createMaterial();
materialOffscreen->setSpecularity({1,1,1,120});
Expand Down
9 changes: 7 additions & 2 deletions imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ Size=266,600
Collapsed=0

[Window][SRE Renderer]
Pos=7,58
Pos=549,271
Size=744,811
Collapsed=0

[Window][Standard]
Pos=68,309
Pos=199,170
Size=680,544
Collapsed=0

Expand All @@ -23,3 +23,8 @@ Pos=60,60
Size=913,610
Collapsed=0

[Window][StandardBlinnPhong]
Pos=668,175
Size=819,638
Collapsed=0

11 changes: 11 additions & 0 deletions include/sre/Framebuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <string>
#include "glm/glm.hpp"
#include "Texture.hpp"
#include "impl/CPPShim.hpp"


namespace sre {
Expand All @@ -29,11 +30,16 @@ namespace sre {
public:
class FrameBufferBuilder {
public:
DEPRECATED("Use withColorTexture() instead")
FrameBufferBuilder& withTexture(std::shared_ptr<Texture> texture);

FrameBufferBuilder& withColorTexture(std::shared_ptr<Texture> texture);
FrameBufferBuilder& withDepthTexture(std::shared_ptr<Texture> texture);
FrameBufferBuilder& withName(std::string name);
std::shared_ptr<Framebuffer> build();
private:
std::vector<std::shared_ptr<Texture>> textures;
std::shared_ptr<Texture> depthTexture;
glm::uvec2 size;
std::string name;
FrameBufferBuilder() = default;
Expand All @@ -47,7 +53,10 @@ namespace sre {

static int getMaximumColorAttachments();

DEPRECATED("Use setColorTexture() instead")
void setTexture(std::shared_ptr<Texture> tex, int index = 0);
void setColorTexture(std::shared_ptr<Texture> tex, int index = 0);
void setDepthTexture(std::shared_ptr<Texture> tex);

const std::string& getName();

Expand All @@ -56,6 +65,7 @@ namespace sre {
bool dirty = true;
explicit Framebuffer(std::string name);
std::vector<std::shared_ptr<Texture>> textures;
std::shared_ptr<Texture> depthTexture;
unsigned int frameBufferObjectId;
uint32_t renderBufferDepth = 0;
std::string name;
Expand All @@ -64,6 +74,7 @@ namespace sre {
};



}


3 changes: 2 additions & 1 deletion include/sre/Mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace sre {
// primitives
MeshBuilder& withSphere(int stacks = 16, int slices = 32, float radius = 1); // Creates a sphere mesh including UV coordinates, positions and normals
MeshBuilder& withCube(float length = 1); // Creates a cube including UV coordinates, positions and normals
MeshBuilder& withQuad(float size=1); // Creates a quad z,y = [-size;size] and z=0, UV=[0;1], normals=(0,0,1)
MeshBuilder& withQuad(float size=1); // Creates a quad x,y = [-size;size] and z=0, UV=[0;1], normals=(0,0,1)
MeshBuilder& withTorus(int segmentsC = 24, int segmentsA = 24, float radiusC = 1, float radiusA = .25);
// Creates a torus in xy plane. C is in the outer (large) circle, A is the sweeping circle.
// raw data
Expand Down Expand Up @@ -112,6 +112,7 @@ namespace sre {
std::vector<std::string> getAttributeNames(); // Names of the vertex attributes

std::array<glm::vec3,2> getBoundsMinMax(); // get the local axis aligned bounding box (AABB)
void setBoundsMinMax(const std::array<glm::vec3,2>& minMax);// set the local axis aligned bounding box (AABB)

const std::string& getName(); // Return the mesh name

Expand Down
9 changes: 6 additions & 3 deletions include/sre/RenderPass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ namespace sre {
void draw(std::shared_ptr<SpriteBatch>&& spriteBatch, // Draws a spriteBatch using modelTransform
glm::mat4 modelTransform = glm::mat4(1)); // using a model-to-world transformation

void blit(std::shared_ptr<Texture> texture, // Render texture to screen
glm::mat4 transformation = glm::mat4(1.0f));

void blit(std::shared_ptr<Material> material, // Render material to screen
glm::mat4 transformation = glm::mat4(1.0f));

std::vector<Color> readPixels(unsigned int x, // Reads pixel(s) from the current framebuffer
unsigned int y, // The defined rectangle must be within the size of the current framebuffer
unsigned int width = 1, // This function must be called after finish has been explicit called on the renderPass
Expand All @@ -124,7 +130,6 @@ namespace sre {

void drawInstance(RenderQueueObj& rqObj); // perform the actual rendering


RenderPass::RenderPassBuilder builder;
explicit RenderPass(RenderPass::RenderPassBuilder& builder);

Expand All @@ -140,6 +145,4 @@ namespace sre {

friend class Renderer;
};


}
2 changes: 1 addition & 1 deletion include/sre/Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace sre {
~Renderer();
static constexpr int sre_version_major = 1;
static constexpr int sre_version_minor = 0;
static constexpr int sre_version_point = 2;
static constexpr int sre_version_point = 3;

glm::ivec2 getWindowSize(); // Return the current size of the window

Expand Down
4 changes: 4 additions & 0 deletions include/sre/Shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ namespace sre {
// "uv" vec4 (note: xy is lower left corner, z is size and w is rotation in radians)
// Expects a mesh with topology = Points

static std::shared_ptr<Shader> getBlit(); // Shader used for blitting
// Uniforms
// "tex" shared_ptr<Texture> (default white texture)

static ShaderBuilder create();
ShaderBuilder update(); // Update the shader using the builder pattern. (Must end with build()).

Expand Down
24 changes: 21 additions & 3 deletions include/sre/Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include "sre/Framebuffer.hpp"
#include "sre/Shader.hpp"

#ifdef None // Fix Linux compile issue
#undef None
#endif

class SDL_Surface;

namespace sre{
Expand Down Expand Up @@ -47,25 +51,36 @@ class DllExport Texture : public std::enable_shared_from_this<Texture> {
NegativeZ
};


enum class SamplerColorspace {
Linear, // Convert values from gamma space to linear space, when gamma correction is enabled. Default behavior.
Gamma // Sampler performs no gamma convertions. This is useful for e.g. normal textures where no gamma correction must be performed
};

enum class DepthPrecision {
I16, // 16 bit integer
I24, // 24 bit integer
I32, // 32 bit integer
F32, // 32 bit float
I24_STENCIL8, // 24 bit integer 8 bit stencil
F32_STENCIL8, // 32 bit float 8 bit stencil
STENCIL8, // 8 bit stencil
None
};

class DllExport TextureBuilder {
public:
~TextureBuilder();
TextureBuilder& withGenerateMipmaps(bool enable);
TextureBuilder& withFilterSampling(bool enable); // if true texture sampling is filtered (bi-linear or tri-linear sampling) otherwise use point sampling.
TextureBuilder& withWrappedTextureCoordinates(bool enable);
TextureBuilder& withFileCubemap(std::string filename, CubemapSide side); // Must define a cubemap for each side
TextureBuilder& withFileCubemap(std::string filename, CubemapSide side); // Must define a cubemap for each side
TextureBuilder& withFile(std::string filename); // Currently only PNG files supported
TextureBuilder& withRGBData(const char* data, int width, int height); // data may be null (for a uninitialized texture)
TextureBuilder& withRGBAData(const char* data, int width, int height); // data may be null (for a uninitialized texture)
TextureBuilder& withWhiteData(int width=2, int height=2);
TextureBuilder& withSamplerColorspace(SamplerColorspace samplerColorspace);
TextureBuilder& withWhiteCubemapData(int width=2, int height=2);
TextureBuilder& withDepth(int width, int height, DepthPrecision precision=DepthPrecision::I16); // Creates a depth texture.
TextureBuilder& withName(const std::string& name);
TextureBuilder& withDumpDebug(); // Output debug info on build
std::shared_ptr<Texture> build();
Expand All @@ -83,7 +98,7 @@ class DllExport Texture : public std::enable_shared_from_this<Texture> {
std::vector<char> data;
void dumpDebug();
};

DepthPrecision depthPrecision = DepthPrecision::None;
std::string name;
bool transparent;
bool generateMipmaps = false;
Expand Down Expand Up @@ -121,6 +136,8 @@ class DllExport Texture : public std::enable_shared_from_this<Texture> {
const std::string& getName(); // name of the string

int getDataSize(); // get size of the texture in bytes on GPU
bool isDepthTexture();
DepthPrecision getDepthPrecision();
private:
Texture(unsigned int textureId, int width, int height, uint32_t target, std::string string);
void updateTextureSampler(bool filterSampling, bool wrapTextureCoordinates);
Expand All @@ -132,6 +149,7 @@ class DllExport Texture : public std::enable_shared_from_this<Texture> {
uint32_t target;
bool generateMipmap;
bool transparent;
DepthPrecision depthPrecision = DepthPrecision::None;
std::string name;
SamplerColorspace samplerColorspace;
bool filterSampling = true; // true = linear/trilinear sampling, false = point sampling
Expand Down
2 changes: 1 addition & 1 deletion include/sre/impl/GL.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <vector>

// For internal debugging of gl errors
inline void checkGLError();
inline void checkGLError(const char* title = nullptr);

inline bool hasExtension(std::string extensionName);
inline std::vector<std::string> listExtension();
Expand Down
7 changes: 6 additions & 1 deletion include/sre/impl/GL.inl
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
#include <iostream>
#include <sstream>

void checkGLError() {
void checkGLError(const char* title) {
for(GLenum err; (err = glGetError()) != GL_NO_ERROR;)
{
if (err != GL_NONE)
{
if (title) std::cerr << title << std::endl;
}
//Process/log the error.
switch (err){
case GL_INVALID_ENUM:

std::cerr << "GL_INVALID_ENUM"<<std::endl;
break;
case GL_INVALID_VALUE:
Expand Down
33 changes: 32 additions & 1 deletion include/sre/impl/ShaderSource.inl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// autogenerated by
// files_to_cpp shader src/embedded_deps/sre_utils_incl.glsl sre_utils_incl.glsl src/embedded_deps/debug_normal_frag.glsl debug_normal_frag.glsl src/embedded_deps/debug_normal_vert.glsl debug_normal_vert.glsl src/embedded_deps/debug_uv_frag.glsl debug_uv_frag.glsl src/embedded_deps/debug_uv_vert.glsl debug_uv_vert.glsl src/embedded_deps/light_incl.glsl light_incl.glsl src/embedded_deps/particles_frag.glsl particles_frag.glsl src/embedded_deps/particles_vert.glsl particles_vert.glsl src/embedded_deps/sprite_frag.glsl sprite_frag.glsl src/embedded_deps/sprite_vert.glsl sprite_vert.glsl src/embedded_deps/standard_pbr_frag.glsl standard_pbr_frag.glsl src/embedded_deps/standard_pbr_vert.glsl standard_pbr_vert.glsl src/embedded_deps/standard_blinn_phong_frag.glsl standard_blinn_phong_frag.glsl src/embedded_deps/standard_blinn_phong_vert.glsl standard_blinn_phong_vert.glsl src/embedded_deps/standard_phong_frag.glsl standard_phong_frag.glsl src/embedded_deps/standard_phong_vert.glsl standard_phong_vert.glsl src/embedded_deps/unlit_frag.glsl unlit_frag.glsl src/embedded_deps/unlit_vert.glsl unlit_vert.glsl src/embedded_deps/debug_tangent_frag.glsl debug_tangent_frag.glsl src/embedded_deps/debug_tangent_vert.glsl debug_tangent_vert.glsl src/embedded_deps/normalmap_incl.glsl normalmap_incl.glsl include/sre/impl/ShaderSource.inl
// files_to_cpp shader src/embedded_deps/sre_utils_incl.glsl sre_utils_incl.glsl src/embedded_deps/debug_normal_frag.glsl debug_normal_frag.glsl src/embedded_deps/debug_normal_vert.glsl debug_normal_vert.glsl src/embedded_deps/debug_uv_frag.glsl debug_uv_frag.glsl src/embedded_deps/debug_uv_vert.glsl debug_uv_vert.glsl src/embedded_deps/light_incl.glsl light_incl.glsl src/embedded_deps/particles_frag.glsl particles_frag.glsl src/embedded_deps/particles_vert.glsl particles_vert.glsl src/embedded_deps/sprite_frag.glsl sprite_frag.glsl src/embedded_deps/sprite_vert.glsl sprite_vert.glsl src/embedded_deps/standard_pbr_frag.glsl standard_pbr_frag.glsl src/embedded_deps/standard_pbr_vert.glsl standard_pbr_vert.glsl src/embedded_deps/standard_blinn_phong_frag.glsl standard_blinn_phong_frag.glsl src/embedded_deps/standard_blinn_phong_vert.glsl standard_blinn_phong_vert.glsl src/embedded_deps/standard_phong_frag.glsl standard_phong_frag.glsl src/embedded_deps/standard_phong_vert.glsl standard_phong_vert.glsl src/embedded_deps/blit_frag.glsl blit_frag.glsl src/embedded_deps/blit_vert.glsl blit_vert.glsl src/embedded_deps/unlit_frag.glsl unlit_frag.glsl src/embedded_deps/unlit_vert.glsl unlit_vert.glsl src/embedded_deps/debug_tangent_frag.glsl debug_tangent_frag.glsl src/embedded_deps/debug_tangent_vert.glsl debug_tangent_vert.glsl src/embedded_deps/normalmap_incl.glsl normalmap_incl.glsl include/sre/impl/ShaderSource.inl
#include <map>
#include <utility>
#include <string>
Expand Down Expand Up @@ -674,6 +674,37 @@ void main(void) {
vColor = color;
#endif
})"),
std::make_pair<std::string,std::string>("blit_frag.glsl",R"(#version 140
out vec4 fragColor;
in vec2 vUV;
uniform sampler2D tex;
#pragma include "sre_utils_incl.glsl"
void main(void)
{
fragColor = toLinear(texture(tex, vUV));
fragColor = toOutput(fragColor);
})"),
std::make_pair<std::string,std::string>("blit_vert.glsl",R"(#version 140
in vec3 position;
in vec3 normal;
#ifdef S_VERTEX_COLOR
in vec4 color;
out vec4 vColor;
#endif
in vec4 uv;
out vec2 vUV;
uniform mat4 g_model;
uniform mat4 g_view;
uniform mat4 g_projection;
void main(void) {
gl_Position = g_model * vec4(position,1.0);
vUV = uv.xy;
})"),
std::make_pair<std::string,std::string>("unlit_frag.glsl",R"(#version 140
out vec4 fragColor;
in vec2 vUV;
Expand Down
13 changes: 13 additions & 0 deletions src/embedded_deps/blit_frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#version 140
out vec4 fragColor;
in vec2 vUV;

uniform sampler2D tex;

#pragma include "sre_utils_incl.glsl"

void main(void)
{
fragColor = toLinear(texture(tex, vUV));
fragColor = toOutput(fragColor);
}
18 changes: 18 additions & 0 deletions src/embedded_deps/blit_vert.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#version 140
in vec3 position;
in vec3 normal;
#ifdef S_VERTEX_COLOR
in vec4 color;
out vec4 vColor;
#endif
in vec4 uv;
out vec2 vUV;

uniform mat4 g_model;
uniform mat4 g_view;
uniform mat4 g_projection;

void main(void) {
gl_Position = g_model * vec4(position,1.0);
vUV = uv.xy;
}
Loading

0 comments on commit 17c6d18

Please sign in to comment.