Skip to content

Commit

Permalink
Merge pull request #28 from AlexanderVeselov/glfw
Browse files Browse the repository at this point in the history
Add glfw
  • Loading branch information
AlexanderVeselov authored Mar 10, 2022
2 parents 02eb034 + 8dba4dd commit 609985b
Show file tree
Hide file tree
Showing 23 changed files with 7,395 additions and 263 deletions.
6,391 changes: 6,391 additions & 0 deletions 3rdparty/glfw/include/GLFW/glfw3.h

Large diffs are not rendered by default.

594 changes: 594 additions & 0 deletions 3rdparty/glfw/include/GLFW/glfw3native.h

Large diffs are not rendered by default.

Binary file added 3rdparty/glfw/lib/x64/Debug/glfw3.lib
Binary file not shown.
Binary file added 3rdparty/glfw/lib/x64/Debug/glfw3.pdb
Binary file not shown.
Binary file added 3rdparty/glfw/lib/x64/Release/glfw3.lib
Binary file not shown.
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

find_package(glfw3 REQUIRED)
find_package(OpenCL REQUIRED)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
Expand All @@ -24,14 +25,14 @@ add_library(imgui STATIC
3rdparty/imgui/imstb_truetype.h
3rdparty/imgui/backends/imgui_impl_opengl3.cpp
3rdparty/imgui/backends/imgui_impl_opengl3.h
3rdparty/imgui/backends/imgui_impl_win32.cpp
3rdparty/imgui/backends/imgui_impl_win32.h
3rdparty/imgui/backends/imgui_impl_glfw.cpp
3rdparty/imgui/backends/imgui_impl_glfw.h
)

target_compile_features(imgui PRIVATE cxx_std_17)
target_include_directories(imgui PUBLIC 3rdparty/imgui 3rdparty/stb)
target_compile_options(imgui PRIVATE /WX)
target_compile_definitions(imgui PRIVATE IMGUI_IMPL_OPENGL_LOADER_GLEW)
target_link_libraries(imgui GLEW::GLEW)
target_link_libraries(imgui PRIVATE GLEW::GLEW glfw3::glfw3)

add_subdirectory(src)
15 changes: 15 additions & 0 deletions cmake/FindGLFW3.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
include (FindPackageHandleStandardArgs)

set(glfw3_DIR "${CMAKE_SOURCE_DIR}/3rdparty/glfw")

find_library(glfw3_LIBRARY_DEBUG NAMES glfw glfw3 PATHS "${glfw3_DIR}/lib/x64/Debug")
find_library(glfw3_LIBRARY_RELEASE NAMES glfw glfw3 PATHS "${glfw3_DIR}/lib/x64/Release")
find_path(glfw3_INCLUDE_DIR NAMES GLFW/glfw3.h PATHS "${glfw3_DIR}/include")

find_package_handle_standard_args(glfw3 DEFAULT_MSG glfw3_LIBRARY_DEBUG glfw3_INCLUDE_DIR)

add_library(glfw3::glfw3 UNKNOWN IMPORTED)
set_target_properties(glfw3::glfw3 PROPERTIES IMPORTED_LOCATION_DEBUG "${glfw3_LIBRARY_DEBUG}")
set_target_properties(glfw3::glfw3 PROPERTIES IMPORTED_LOCATION_RELEASE "${glfw3_LIBRARY_RELEASE}")
set_target_properties(glfw3::glfw3 PROPERTIES IMPORTED_LOCATION_RELWITHDEBINFO "${glfw3_LIBRARY_RELEASE}")
set_target_properties(glfw3::glfw3 PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${glfw3_INCLUDE_DIR}")
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ add_executable(RayTracingApp ${SOURCES})
target_include_directories(RayTracingApp PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(RayTracingApp PRIVATE ${PROJECT_SOURCE_DIR}/3rdparty/tinyobjloader ${CMAKE_SOURCE_DIR}/3rdparty/glm)

target_link_libraries(RayTracingApp PUBLIC OpenCL::OpenCL OpenGL::GL GLEW::GLEW OpenGL::GLU imgui)
target_link_libraries(RayTracingApp PUBLIC glfw3::glfw3 OpenCL::OpenCL OpenGL::GL GLEW::GLEW OpenGL::GLU imgui)
set_target_properties(RayTracingApp PROPERTIES
VS_DEBUGGER_WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
)
Expand Down
8 changes: 3 additions & 5 deletions src/bvh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
*****************************************************************************/

#include "bvh.hpp"
#include "gpu_wrappers/cl_context.hpp"
#include "Utils/cl_exception.hpp"

namespace
{
Expand Down Expand Up @@ -232,13 +230,13 @@ unsigned int Bvh::FlattenBVHTree(BVHBuildNode* node, unsigned int* offset)
assert(!node->children[0] && !node->children[1]);
assert(node->nPrimitives < 65536);
linearNode->offset = node->firstPrimOffset;
linearNode->nPrimitives = node->nPrimitives;
linearNode->num_primitives_axis = node->nPrimitives << 16;
}
else
{
// Create interior flattened BVH node
linearNode->axis = node->splitAxis;
linearNode->nPrimitives = 0;
linearNode->num_primitives_axis = node->splitAxis;
//linearNode->nPrimitives = 0;
FlattenBVHTree(node->children[0], offset);
linearNode->offset = FlattenBVHTree(node->children[1], offset);
}
Expand Down
14 changes: 10 additions & 4 deletions src/gpu_wrappers/cl_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@
#include <string>
#include <fstream>

CLContext::CLContext(const cl::Platform& platform, void* display_context, void* gl_context)
#ifdef WIN32
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif

CLContext::CLContext(const cl::Platform& platform)
: platform_(platform)
{
std::cout << "Platform: " << platform.getInfo<CL_PLATFORM_NAME>() << std::endl;
Expand All @@ -40,9 +46,9 @@ CLContext::CLContext(const cl::Platform& platform, void* display_context, void*
// OpenCL platform
CL_CONTEXT_PLATFORM, (cl_context_properties)platform(),
// OpenGL context
CL_GL_CONTEXT_KHR, (cl_context_properties)gl_context,
CL_GL_CONTEXT_KHR, (cl_context_properties)wglGetCurrentContext(),
// HDC used to create the OpenGL context
CL_WGL_HDC_KHR, (cl_context_properties)display_context,
CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC(),
0
};

Expand Down Expand Up @@ -171,7 +177,7 @@ void CLKernel::Reload()
cl::Program program(context_.GetContext(), source, false, &status);
ThrowIfFailed(status, ("Failed to create program from file " + std::string(filename_)).c_str());

std::string build_options = "-I src/kernels/cl/";
std::string build_options = "-I . -I src/kernels/cl/";

for (auto const& definition : definitions_)
{
Expand Down
4 changes: 2 additions & 2 deletions src/gpu_wrappers/cl_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#pragma once

#include "scene/scene.hpp"
#include <GL/glew.h>
#include <CL/cl.hpp>
#include <memory>
#include <vector>
Expand All @@ -36,7 +37,7 @@ class CLKernel;
class CLContext
{
public:
CLContext(const cl::Platform& platform, void* display_context, void* gl_context);
CLContext(const cl::Platform& platform);
std::shared_ptr<CLKernel> CreateKernel(const char* filename, char const* kernel_name,
std::vector<std::string> const& definitions = std::vector<std::string>());

Expand All @@ -53,7 +54,6 @@ class CLContext
std::vector<cl::Device> const& GetDevices() const { return devices_; }
void ReloadKernels();


private:
cl::Platform platform_;
std::vector<cl::Device> devices_;
Expand Down
3 changes: 2 additions & 1 deletion src/integrator/cl_pt_integrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
SOFTWARE.
*****************************************************************************/

#include <GL/glew.h>

#include "cl_pt_integrator.hpp"
#include "utils/cl_exception.hpp"
#include "Scene/scene.hpp"
#include "acceleration_structure.hpp"
#include "Utils/blue_noise_sampler.hpp"
#include <GL/glew.h>

namespace args
{
Expand Down
25 changes: 13 additions & 12 deletions src/kernels/cl/trace_bvh.cl
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ float min3(float3 val)
return min(min(val.x, val.y), val.z);
}

bool RayBounds(const __global Bounds3* bounds, float3 ray_origin, float3 ray_inv_dir, float t_min, float t_max)
bool RayBounds(Bounds3 bounds, float3 ray_origin, float3 ray_inv_dir, float t_min, float t_max)
{
float3 aabb_min = bounds->pos[0];
float3 aabb_max = bounds->pos[1];
float3 aabb_min = bounds.pos[0];
float3 aabb_max = bounds.pos[1];

float3 t0 = (aabb_min - ray_origin) * ray_inv_dir;
float3 t1 = (aabb_max - ray_origin) * ray_inv_dir;
Expand Down Expand Up @@ -143,19 +143,20 @@ __kernel void TraceBvh

while (true)
{
__global LinearBVHNode* node = &nodes[currentNodeIndex];
LinearBVHNode node = nodes[currentNodeIndex];

if (RayBounds(&node->bounds, ray.origin.xyz, ray_inv_dir, ray.origin.w, ray.direction.w))
if (RayBounds(node.bounds, ray.origin.xyz, ray_inv_dir, ray.origin.w, ray.direction.w))
{
int num_primitives = node.num_primitives_axis >> 16;
// Leaf node
if (node->nPrimitives > 0)
if (num_primitives > 0)
{
// Intersect ray with primitives in leaf BVH node
for (int i = 0; i < node->nPrimitives; ++i)
for (int i = 0; i < num_primitives; ++i)
{
if (RayTriangle(ray, &triangles[node->offset + i], &hit.bc, &hit.t))
if (RayTriangle(ray, &triangles[node.offset + i], &hit.bc, &hit.t))
{
hit.primitive_id = node->offset + i;
hit.primitive_id = node.offset + i;
// Set ray t_max
// TODO: remove t from hit structure
ray.direction.w = hit.t;
Expand All @@ -177,14 +178,14 @@ __kernel void TraceBvh
else
{
// Put far BVH node on _nodesToVisit_ stack, advance to near node
if (ray_sign[node->axis])
if (ray_sign[node.num_primitives_axis & 0xFFFF])
{
nodesToVisit[toVisitOffset++] = currentNodeIndex + 1;
currentNodeIndex = node->offset;
currentNodeIndex = node.offset;
}
else
{
nodesToVisit[toVisitOffset++] = node->offset;
nodesToVisit[toVisitOffset++] = node.offset;
currentNodeIndex = currentNodeIndex + 1;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/kernels/glsl/hit_surface.comp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ layout(std140, binding = 0) uniform SampleCounter

layout(binding = 1) uniform TextureHandles
{
uint64_t texture_handles[MAX_TEXTURES];
uvec2 texture_handles[MAX_TEXTURES];
};

layout(std430, binding = 0) buffer IncomingRays
Expand Down
6 changes: 3 additions & 3 deletions src/kernels/glsl/trace_bvh.comp
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ void main()
if (RayBounds(node.bounds, ray.origin.xyz, ray_inv_dir, ray.origin.w, ray.direction.w))
{
// Leaf node
if (int(node.nPrimitives) > 0)
if (int(node.num_primitives_axis >> 16) > 0)
{
// Intersect ray with primitives in leaf BVH node
for (int i = 0; i < int(node.nPrimitives); ++i)
for (int i = 0; i < int(node.num_primitives_axis >> 16); ++i)
{
if (RayTriangle(ray, triangles[node.offset + i], hit.bc, hit.t))
{
Expand All @@ -199,7 +199,7 @@ void main()
else
{
// Put far BVH node on _nodesToVisit_ stack, advance to near node
if (ray_sign[node.axis] != 0)
if (ray_sign[node.num_primitives_axis & 0xFFFF] != 0)
{
nodesToVisit[toVisitOffset++] = currentNodeIndex + 1;
currentNodeIndex = int(node.offset);
Expand Down
8 changes: 3 additions & 5 deletions src/kernels/shared_structures.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,9 @@ STRUCT_BEGIN(LinearBVHNode)
Bounds3 bounds;
// 4 bytes
unsigned int offset; // primitives (leaf) or second child (interior) offset
// 2 bytes
unsigned short nPrimitives; // 0 -> interior node
// 1 byte
unsigned char axis; // interior node: xyz
unsigned char pad[9]; // ensure 48 byte total size
// 4 bytes
unsigned int num_primitives_axis; // 0 -> interior node
unsigned int padding[2]; // ensure 48 byte total size
STRUCT_END(LinearBVHNode)

STRUCT_BEGIN(Camera)
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
SOFTWARE.
*****************************************************************************/

#include "Utils/window.hpp"
#include "render.hpp"
#include "utils/window.hpp"

int main()
{
try
{
Window window("RayTracing", 1280, 720);
Window window(1920, 1080, "RayTracing");
Render::RenderBackend backend = Render::RenderBackend::kOpenCL;
//backend = Render::RenderBackend::kOpenGL;
Render render(window, backend);
Expand Down
23 changes: 2 additions & 21 deletions src/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ Render::Render(Window& window, RenderBackend backend)
, width_(window.GetWidth())
, height_(window.GetHeight())
{
ImGui::CreateContext();
ImGui::StyleColorsDark();
ImGui_ImplOpenGL3_Init();
ImGui_ImplWin32_Init(window_.GetNativeHandle());

if (render_backend_ == RenderBackend::kOpenCL)
{
std::vector<cl::Platform> all_platforms;
Expand All @@ -55,8 +50,7 @@ Render::Render(Window& window, RenderBackend backend)
throw std::runtime_error("No OpenCL platforms found");
}

cl_context_ = std::make_shared<CLContext>(all_platforms[0],
window_.GetDisplayContext(), window_.GetGLContext());
cl_context_ = std::make_shared<CLContext>(all_platforms[0]);
}

#ifndef NDEBUG
Expand Down Expand Up @@ -118,13 +112,6 @@ Render::Render(Window& window, RenderBackend backend)
integrator_->UploadGPUData(*scene_, *acc_structure_);
}

Render::~Render()
{
ImGui_ImplWin32_Shutdown();
ImGui_ImplOpenGL3_Shutdown();
ImGui::DestroyContext();
}

double Render::GetCurtime() const
{
return (double)clock() / (double)CLOCKS_PER_SEC;
Expand All @@ -138,9 +125,6 @@ double Render::GetDeltaTime() const
void Render::FrameBegin()
{
start_frame_time_ = GetCurtime();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
}

void Render::FrameEnd()
Expand Down Expand Up @@ -213,9 +197,6 @@ void Render::DrawGUI()
}
}
ImGui::End();

ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}

void Render::RenderFrame()
Expand All @@ -227,7 +208,7 @@ void Render::RenderFrame()

bool need_to_reset = false;

if (window_.IsKeyPressed('R'))
if (window_.GetKey(KeyCode::kR))
{
ReloadKernels();
need_to_reset = true;
Expand Down
4 changes: 2 additions & 2 deletions src/render.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
#include "integrator/integrator.hpp"
#include "acceleration_structure.hpp"
#include "scene/scene.hpp"
#include "gpu_wrappers/cl_context.hpp"
#include "utils/camera_controller.hpp"
#include "utils/framebuffer.hpp"
#include "gpu_wrappers/cl_context.hpp"
#include <memory>
#include <ctime>

Expand All @@ -44,7 +44,7 @@ class Render
};

Render(Window& window, RenderBackend backend);
~Render();
~Render() = default;

void RenderFrame();
double GetCurtime() const;
Expand Down
3 changes: 2 additions & 1 deletion src/scene/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
SOFTWARE.
*****************************************************************************/

#include <GL/glew.h>

#define TINYOBJLOADER_IMPLEMENTATION
#include "tiny_obj_loader.h"

Expand All @@ -37,7 +39,6 @@
#include <sstream>
#include <ctime>
#include <cctype>
#include <GL/glew.h>

Scene::Scene(const char* filename, float scale, bool flip_yz)
{
Expand Down
Loading

0 comments on commit 609985b

Please sign in to comment.