Commit 1befa28c authored by unknown's avatar unknown
Browse files

Changed gl handles to lazy-initializers

parent a5e2a7d3
......@@ -34,11 +34,7 @@ vec3 bsdf__sampleTranslucent(const in Material material, const in vec2 random, c
bool entering = dot(-ray_in.direction, vertex.normal.xyz) > 0.f;
vec3 hemi = randCosineHemisphere(random.x, random.y);
/*if (entering) {
light_influence = vec3(1, 0, 0);
}*/
ray_out.direction.xyz =localToWorldNormal(hemi, (entering ? -1 : 1) * vertex.normal.xyz);
ray_out.direction.xyz =localToWorldNormal(hemi, (entering ? -1 : 1) * vertex.normal.xyz);
pdf = abs(dot(ray_out.direction, vertex.normal.xyz)) * ONE_OVER_PI;
ray_out.origin = vertex.position.xyz + 1e-5f * ray_in.direction;
......@@ -50,6 +46,10 @@ vec3 bsdf__sampleTranslucent(const in Material material, const in vec2 random, c
vec3 diffuse = material__getDiffuse(material, vertex.texcoord).xyz;
if(!entering){
diffuse = (diffuse + material__getSpecular(material, vertex.texcoord).xyz) / 2.f;
}
light_influence = diffuse * ONE_OVER_PI;
return diffuse * ONE_OVER_PI;
......
......@@ -4,43 +4,96 @@
using namespace glare;
int main(int argc, char* argv[])
template<typename ValueType, typename ConstructorFunc, ConstructorFunc constructor, void(*destructor)(ValueType), typename ...ConstructParams>
struct late_initializer
{
late_initializer(ConstructParams&&... args)
: m_function([=]() mutable { generate(std::forward<ConstructParams>(args)...); }) {}
late_initializer(late_initializer &&other)
: m_value(other.m_value) {}
~late_initializer() { destructor(m_value); }
late_initializer(late_initializer &other) = delete;
void reset(ValueType&& type) {
if (m_initialized) {
destructor(m_value);
m_value = std::forward<ValueType>(type);
}
}
void reset() {
m_initialized = false;
destructor(m_value);
}
std::vector<float> data2
bool valid() const
{
1, 24, 15,
15, 592, 623 ,
10,41,44,
1141,419,24,
29, 293, 24 ,
};
for (int i = 0; i < 1000000; i++)
return m_initialized;
}
operator ValueType() const
{
data2.push_back(float(rand()));
if (!m_initialized) {
m_function();
m_initialized = true;
}
return m_value;
}
std::vector<float> data3(data2.begin(), data2.end());
core::ClockMicros clock;
private:
void generate(ConstructParams&&... args) {
m_value = constructor(args...);
}
std::sort(data3.begin(), data3.end());
Log_Info << "Sort took " << clock.restart() << " microseconds";
std::function<void()> m_function;
mutable bool m_initialized = false;
mutable ValueType m_value;
};
core::radix::simpleSort<float>(data2);
Log_Info << "Sort took " << clock.restart() << " microseconds";
template<void(*constr)(unsigned, unsigned*)>
inline unsigned constructor()
{
unsigned val;
constr(1, &val);
return val;
}
std::vector<glm::vec3> datatatat
{
{1,425,235236},
{2566,235,252},
{13502, 20, 11}
};
template<void(*destr)(unsigned, unsigned*)>
inline void destructor(unsigned val)
{
destr(1, &val);
}
template<void(*construct)(unsigned, unsigned*), void(*destruct)(unsigned, unsigned*)>
using handle_t = late_initializer<unsigned, unsigned(), constructor<construct>, destructor<destruct>>;
void genBla(unsigned, unsigned* vals) {
vals[0] = 30120;
Log_Info << "Generated a Bla";
}
core::radix::advancedSort<glm::vec3, float>(datatatat, [](const glm::vec3 &data) {
return data.x;
});
void delBla(unsigned, unsigned* vals) {
vals[0] = 0;
Log_Info << "Deleted a Bla";
}
using bla_handle = handle_t<genBla, delBla>;
int main(int argc, char* argv[])
{
{
bla_handle handle;
Log_Info << "Grappling Hook";
Log_Info << "Bla's value is " << unsigned(handle);
Log_Info << "Bla's value is " << unsigned(handle);
Log_Info << "Bla's value is " << unsigned(handle);
handle.reset();
Log_Info << "Bla's value is " << unsigned(handle);
Log_Info << "Bla's value is " << unsigned(handle);
}
quitPromptDefault(0);
return 0;
......
......@@ -48,7 +48,7 @@ namespace glare
//Create a skybox from cube map.
m_skybox = std::make_shared<core::Skybox>(core::Skybox::collectFilesFrom(core::asset("/textures/ryfjallet/")));
initializeScene(m_current_scene_root / "bunny.dae", 1.f);
initializeScene(m_current_scene_root / "mesh.dae", 1.f);
initializeRenderRequirements();
initializeAdvanced();
initializeGUI();
......
......@@ -144,8 +144,8 @@ namespace glare
class Query {
public:
Query() { m_handle.generate(); };
explicit Query(GLuint id) { m_handle.reset(id); };
Query() { };
explicit Query(GLuint id) { m_handle.reset(std::move(id)); };
~Query() {};
Query(Query&&) = delete;
......
......@@ -281,7 +281,7 @@ namespace glare
template<gl::TextureFormat Format, gl::TextureInternalFormat InternalFormat, gl::Type T>
Texture<Format, InternalFormat, T>::Texture(uint32_t width, uint32_t height, tex_type* data, gl::TextureFilterMin min, gl::TextureFilterMag mag)
{
m_handle.generate();
//m_handle.generate();
m_filter_min = min;
m_filter_mag = mag;
resize(width, height, data);
......
......@@ -68,7 +68,6 @@ namespace glare
template<gl::TextureInternalFormat InternalFormat, gl::Type T>
TextureMultisampled<InternalFormat, T>::TextureMultisampled(unsigned width, unsigned height, unsigned samples)
{
m_texture_handle.generate();
resize(width, height, samples);
gl::bindTexture(gl::TextureTarget::e2DMultisample, m_texture_handle);
......
......@@ -9,7 +9,7 @@ namespace glare
Framebuffer::Framebuffer(unsigned int width, unsigned int height)
: m_width(width), m_height(height)
{
m_handle.generate();
//m_handle.generate();
}
void Framebuffer::resize(unsigned int width, unsigned int height)
......@@ -129,7 +129,7 @@ namespace glare
FramebufferMultisample::FramebufferMultisample(unsigned int width, unsigned int height, unsigned samples)
: m_width(width), m_height(height), m_samples(samples)
{
m_handle.generate();
//m_handle.generate();
}
void FramebufferMultisample::resize(unsigned int width, unsigned int height, unsigned samples)
......
......@@ -418,8 +418,9 @@ namespace glare
}
Shader::Shader(gl::ShaderType type)
: m_shader_handle(std::move(type))
{
m_shader_handle.generate(type);
}
Shader::Shader(gl::ShaderType shader_type, fs::path path)
......@@ -478,7 +479,6 @@ namespace glare
VertexArray::VertexArray()
{
m_handle.generate();
}
VertexArray::~VertexArray()
......
......@@ -196,7 +196,7 @@ namespace glare
template<gl::BufferType Type>
Buffer<Type>::Buffer()
{
m_handle.generate();
//m_handle.generate();
}
template <gl::BufferType Type>
......
......@@ -7,8 +7,6 @@ namespace glare
{
ShaderProgram::ShaderProgram(std::vector<std::shared_ptr<Shader>> shaders)
{
m_handle.generate();
for (auto && shader : shaders)
{
gl::attachShader(m_handle, shader->getID());
......
......@@ -17,6 +17,11 @@ namespace glare
{
return size_t((((-0.5 * unsigned(in)) + 4.5)*unsigned(in)) - 1 + unsigned(out));
}
unsigned goodSubdivision(size_t polygon_count, glm::vec3 bounds_size)
{
return unsigned(glm::ceil(glm::log2(glm::max(size_t(2), polygon_count)) * glm::log(log2((glm::compMax(bounds_size) / glm::compMin(bounds_size)) + 2 * glm::e<float>()))));
}
}
LineSpace::LineSpace()
......@@ -24,19 +29,20 @@ namespace glare
}
LineSpace::LineSpace(const advanced::LocalCollector &collector, unsigned max_subdivisions)
LineSpace::LineSpace(const advanced::LocalCollector &collector, int max_subdivisions)
: LineSpace()
{
generate(collector, max_subdivisions);
}
LineSpace::~LineSpace()
{
LineSpace::~LineSpace() {}
}
void LineSpace::generate(const advanced::LocalCollector &collector, unsigned max_subdivisions)
void LineSpace::generate(const advanced::LocalCollector &collector, int max_subdivisions)
{
if (max_subdivisions <= 0) {
max_subdivisions = utilities::goodSubdivision(collector.triangles().size(), collector.bounds().size().xyz);
}
m_subdivision = math::UniformBoundsSubdivision(collector.bounds(), max_subdivisions);
m_bounds = collector.bounds();
m_bounds.max = m_bounds.min + glm::vec4(m_subdivision.size * glm::vec3(m_subdivision.subdivisions), 0);
......
......@@ -86,6 +86,7 @@ namespace glare
{
unsigned faceAxis(Face face);
size_t faceRangeIndex(Face in, Face out);
unsigned goodSubdivision(size_t polygon_count, glm::vec3 bounds_size);
}
class LineSpaceRenderer;
......@@ -96,8 +97,8 @@ namespace glare
public:
LineSpace();
~LineSpace();
LineSpace(const advanced::LocalCollector &collector, unsigned max_subdivisions);
void generate(const advanced::LocalCollector &collector, unsigned max_subdivisions);
LineSpace(const advanced::LocalCollector &collector, int max_subdivisions);
void generate(const advanced::LocalCollector &collector, int max_subdivisions);
const core::Buffer<gl::BufferType::eShaderStorage> &lineBuffer() const;
const std::vector<Line> &lines() const;
......
......@@ -6,6 +6,7 @@
#include <glm/glm.hpp>
#include <string>
#include <vector>
#include <functional>
namespace gl
{
......@@ -774,56 +775,89 @@ namespace gl
namespace handle
{
template<typename Constructor, Constructor construct, void(*destruct)(unsigned), typename ...Args>
struct handle_t
template<typename ValueType, typename ConstructorFunc, ConstructorFunc constructor, void(*destructor)(ValueType), typename ...ConstructParams>
struct late_initializer
{
handle_t() {}
explicit handle_t(unsigned id) { this->id = id; }
handle_t(handle_t &other) = delete;
handle_t(handle_t &&other) { id = other.id; }
~handle_t() { destruct(id); }
late_initializer(ConstructParams&&... args)
: m_function([=]() mutable { generate(std::forward<ConstructParams>(args)...); }) {}
late_initializer(late_initializer &&other)
: m_value(other.m_value) {}
~late_initializer() { destructor(m_value); }
late_initializer(late_initializer &other) = delete;
void regenerate(ConstructParams&&... args) {
if(m_initialized)
destructor(m_value);
m_initialized = false;
m_function = [=]() mutable { generate(std::forward<ConstructParams>(args)...); };
}
void reset(ValueType&& type) {
if (m_initialized) {
destructor(m_value);
m_value = std::forward<ValueType>(type);
}
}
void reset() {
m_initialized = false;
destructor(m_value);
}
bool valid() const
{
return m_initialized;
}
void reset(unsigned new_id = 0) { destruct(id); id = new_id; };
void generate(Args... args) { id = construct(args...); }
operator ValueType() const
{
if (!m_initialized) {
m_function();
m_initialized = true;
}
operator unsigned() const {
return id;
return m_value;
}
friend bool operator == (handle_t x, handle_t y) { return x.id == y.id; }
friend bool operator != (handle_t x, handle_t y) { return x.id != y.id; }
private:
unsigned id = 0;
void generate(ConstructParams&&... args) {
m_value = constructor(args...);
}
std::function<void()> m_function;
mutable bool m_initialized = false;
mutable ValueType m_value;
};
template<void(*destruct)(unsigned, unsigned*)>
inline void destructor(unsigned id)
template<void(*constr)(unsigned, unsigned*)>
inline unsigned constructor()
{
destruct(1, &id);
unsigned val;
constr(1, &val);
return val;
}
template<void(*construct)(unsigned, unsigned*)>
inline unsigned constructor()
template<void(*destr)(unsigned, unsigned*)>
inline void destructor(unsigned val)
{
unsigned id;
construct(1, &id);
return id;
destr(1, &val);
}
template<unsigned(*construct)(), void(*destruct)(unsigned)>
using default_handle_t = handle_t<unsigned(*)(), construct, destruct>;
using simple_handle_t = late_initializer<unsigned, unsigned(), construct, destruct>;
template<void(*construct)(unsigned, unsigned*), void(*destruct)(unsigned, unsigned*)>
using indirect_handle_t = handle_t<unsigned(*)(), constructor<construct>, destructor<destruct>>;
using ext_handle_t = simple_handle_t<constructor<construct>, destructor<destruct>>;
using texture = indirect_handle_t<genTextures, deleteTextures>;
using buffer = indirect_handle_t<genBuffers, deleteBuffers>;
using framebuffer = indirect_handle_t<genFramebuffers, deleteFramebuffers>;
using query = indirect_handle_t<genQueries, deleteQueries>;
using vertex_array = indirect_handle_t<genVertexArrays, deleteVertexArrays>;
using texture = ext_handle_t<genTextures, deleteTextures>;
using buffer = ext_handle_t<genBuffers, deleteBuffers>;
using framebuffer = ext_handle_t<genFramebuffers, deleteFramebuffers>;
using query = ext_handle_t<genQueries, deleteQueries>;
using vertex_array = ext_handle_t<genVertexArrays, deleteVertexArrays>;
using shader = handle_t<decltype(&createShader), createShader, deleteShader, gl::ShaderType>;
using shader_program = default_handle_t<createProgram, deleteProgram>;
using shader = late_initializer<unsigned, decltype(&createShader), createShader, deleteShader, gl::ShaderType>;
using shader_program = simple_handle_t<createProgram, deleteProgram>;
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment