Skip to content

Commit

Permalink
Merge pull request #68 from gameknife/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
gameknife authored Aug 31, 2024
2 parents 8f7e80a + 7e7afcf commit 7880e63
Show file tree
Hide file tree
Showing 65 changed files with 4,531 additions and 118 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ ASALocalRun/
build

assets/shaders/Platform.glsl

assets/package
android/.gradle/
android/app/src/main/assets/
android/local.properties
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,19 @@ vcpkg_windows.bat
build_windows.bat
```

**Windows (MinGW)**
init the MSYS2 MINGW64 shell with following packages
```
pacman -S --needed git base-devel mingw-w64-x86_64-toolchain cmake ninja
```
cmake's module FindVulkan has a little bug on MingGW, try modified FindVulkan.cmake as below
set(_Vulkan_library_name vulkan) -> set(_Vulkan_library_name vulkan-1)
then, execute scripts bellow
```
vcpkg_mingw.sh
build_mings.sh
```

**Android On Windows**
JAVA SDK should be JAVA17 or higher
Install Android Studio or Android SDK Tool, with NDK 25.1.8937393 installed
Expand Down
29 changes: 29 additions & 0 deletions assets/shaders/Accumulate.comp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ layout(binding = 8, r8ui) uniform uimage2D AdaptiveSampleBuffer;

layout(local_size_x = 8, local_size_y = 4, local_size_z = 1) in;

uint FetchPrimitiveIndex(in uint InstanceID)
{
return (InstanceID >> 16) - 1;
}

bool EdgeDetect(in uint current_primitive_index, in bool isEvenFrame, in ivec2 ipos)
{
uint center = FetchPrimitiveIndex(current_primitive_index);

uint prev_primitive_index0 = FetchPrimitiveIndex(isEvenFrame ? imageLoad(VisibilityBuffer, ipos + ivec2(2,0)).r : imageLoad(Visibility1Buffer, ipos + ivec2(2,0)).r);
uint prev_primitive_index1 = FetchPrimitiveIndex(isEvenFrame ? imageLoad(VisibilityBuffer, ipos + ivec2(-2,0)).r : imageLoad(Visibility1Buffer, ipos + ivec2(-2,0)).r);
uint prev_primitive_index2 = FetchPrimitiveIndex(isEvenFrame ? imageLoad(VisibilityBuffer, ipos + ivec2(0, 2)).r : imageLoad(Visibility1Buffer, ipos + ivec2(0, 2)).r);
uint prev_primitive_index3 = FetchPrimitiveIndex(isEvenFrame ? imageLoad(VisibilityBuffer, ipos + ivec2(0, -2)).r : imageLoad(Visibility1Buffer, ipos + ivec2(0, -2)).r);

bool edge0 = any( notEqual( uvec4(prev_primitive_index0, prev_primitive_index1, prev_primitive_index2, prev_primitive_index3), uvec4(center) ));
bool edge1 = any( equal( uvec4(prev_primitive_index0, prev_primitive_index1, prev_primitive_index2, prev_primitive_index3), uvec4(center) ));

return edge0 && edge1;
}

// a simple accumulation shader, reproject can impl here later.
void main() {
ivec2 ipos = ivec2(gl_GlobalInvocationID.xy) + ivec2(Camera.ViewportRect.x, Camera.ViewportRect.y);
Expand Down Expand Up @@ -97,5 +117,14 @@ void main() {
imageStore(AccumulateImage, ipos, final);
}

uint realInstanceId = FetchPrimitiveIndex(current_primitive_index);
if(realInstanceId == Camera.SelectedId)
{
if( EdgeDetect(current_primitive_index, isEvenFrame, ipos ) )
{
final.rgb = vec3(1,0.05,0) * Camera.PaperWhiteNit;
}
}

imageStore(OutImage, ipos, vec4( final.rgb, 1.0 ));
}
2 changes: 1 addition & 1 deletion assets/shaders/common/RTCommon.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void ProcessMiss(const vec3 RayDirection)
{
Ray.GBuffer = vec4(0,1,0,0);
Ray.Albedo = vec4(1,1,1,1);
Ray.primitiveId = 0;
Ray.primitiveId = 65535 << 16 | 0;
Ray.Exit = true;
Ray.Distance = 1000.0;
Ray.pdf = 1.0;
Expand Down
2 changes: 2 additions & 0 deletions assets/shaders/common/UniformBufferObject.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ struct UniformBufferObject
float AdaptiveVariance;
uint AdaptiveSteps;
bool TAA;

uint SelectedId;
};
Binary file added assets/textures/std_env.hdr
Binary file not shown.
7 changes: 7 additions & 0 deletions build_mingw.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh
set -e

mkdir --parents build/mingw
cd build/mingw
cmake -G Ninja -D CMAKE_BUILD_TYPE=Release -D VCPKG_TARGET_TRIPLET=x64-mingw-static -D CMAKE_TOOLCHAIN_FILE=../vcpkg.mingw/scripts/buildsystems/vcpkg.cmake ../..
cmake --build . --config Release
76 changes: 65 additions & 11 deletions src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ NextRendererApplication<Renderer>::NextRendererApplication(const UserSettings& u
userSettings_.SceneIndex = SceneList::AddExternalScene(args);
return true;
});
EditorCommand::RegisterEdtiorCommand( EEditorCommand::ECmdIO_LoadHDRI, [this](std::string& args)->bool {
Assets::GlobalTexturePool::UpdateHDRTexture(0, args.c_str(), Vulkan::SamplerConfig());
userSettings_.SkyIdx = 0;
return true;
});
#endif
}

Expand Down Expand Up @@ -138,14 +143,34 @@ Assets::UniformBufferObject NextRendererApplication<Renderer>::GetUniformBufferO
ubo.PrevViewProjection = prevUBO_.TotalFrames != 0 ? prevUBO_.ViewProjection : ubo.ViewProjection;

ubo.ViewportRect = glm::vec4(offset.x, offset.y, extent.width, extent.height);

glm::vec2 pixel = mousePos_ - glm::vec2(offset.x, offset.y);
glm::vec2 uv = pixel / glm::vec2(extent.width, extent.height) * glm::vec2(2.0,2.0) - glm::vec2(1.0,1.0);
glm::vec4 origin = ubo.ModelViewInverse * glm::vec4(0, 0, 0, 1);
glm::vec4 target = ubo.ProjectionInverse * (glm::vec4(uv.x, uv.y, 1, 1));

glm::vec3 raydir = ubo.ModelViewInverse * glm::vec4(normalize((glm::vec3(target) - glm::vec3(0.0,0.0,0.0))), 0.0);
glm::vec3 rayorg = glm::vec3(origin);

Renderer::SetRaycastRay(rayorg, raydir);

Assets::RayCastResult rayResult;
Renderer::GetLastRaycastResult(rayResult);

if( userSettings_.AutoFocus && rayResult.Hitted )
if( userSettings_.AutoFocus )
{
userSettings_.FocusDistance = rayResult.T;
if(rayResult.Hitted )
{
userSettings_.FocusDistance = rayResult.T;
scene_->SetSelectedId(rayResult.InstanceId);
}
else
{
scene_->SetSelectedId(-1);
}
}

ubo.SelectedId = scene_->GetSelectedId();

userSettings_.HitResult = rayResult;

Expand Down Expand Up @@ -225,7 +250,9 @@ void NextRendererApplication<Renderer>::OnDeviceSet()
Renderer::OnDeviceSet();

// global textures
// texture id 0: global sky
// texture id 0: dynamic hdri sky
Assets::GlobalTexturePool::LoadHDRTexture(Utilities::FileHelper::GetPlatformFilePath("assets/textures/std_env.hdr"), Vulkan::SamplerConfig());

Assets::GlobalTexturePool::LoadHDRTexture(Utilities::FileHelper::GetPlatformFilePath("assets/textures/canary_wharf_1k.hdr"), Vulkan::SamplerConfig());
Assets::GlobalTexturePool::LoadHDRTexture(Utilities::FileHelper::GetPlatformFilePath("assets/textures/kloppenheim_01_puresky_1k.hdr"), Vulkan::SamplerConfig());
Assets::GlobalTexturePool::LoadHDRTexture(Utilities::FileHelper::GetPlatformFilePath("assets/textures/kloppenheim_07_1k.hdr"), Vulkan::SamplerConfig());
Expand All @@ -238,8 +265,7 @@ void NextRendererApplication<Renderer>::OnDeviceSet()
Assets::GlobalTexturePool::LoadHDRTexture(Utilities::FileHelper::GetPlatformFilePath("assets/textures/umhlanga_sunrise_1k.hdr"), Vulkan::SamplerConfig());
Assets::GlobalTexturePool::LoadHDRTexture(Utilities::FileHelper::GetPlatformFilePath("assets/textures/shanghai_bund_1k.hdr"), Vulkan::SamplerConfig());

if(userSettings_.HDRIfile != "") Assets::GlobalTexturePool::LoadHDRTexture(userSettings_.HDRIfile.c_str(), Vulkan::SamplerConfig());
userSettings_.HDRIsLoaded = Assets::GlobalTexturePool::GetInstance()->TotalTextures() - 1;
if(GOption->HDRIfile != "") Assets::GlobalTexturePool::UpdateHDRTexture(0, GOption->HDRIfile.c_str(), Vulkan::SamplerConfig());

std::vector<Assets::Model> models;
std::vector<Assets::Node> nodes;
Expand Down Expand Up @@ -277,8 +303,6 @@ void NextRendererApplication<Renderer>::DeleteSwapChain()
template <typename Renderer>
void NextRendererApplication<Renderer>::DrawFrame()
{
TaskCoordinator::GetInstance()->Tick();

// Check if the scene has been changed by the user.
if (status_ == NextRenderer::EApplicationStatus::Running && sceneIndex_ != static_cast<uint32_t>(userSettings_.SceneIndex))
{
Expand Down Expand Up @@ -324,23 +348,28 @@ void NextRendererApplication<Renderer>::RenderUI(VkCommandBuffer commandBuffer,
{
static float frameRate = 0.0;
static double lastTime = 0.0;
static double lastTimestamp = 0.0;
double now = Renderer::Window().GetTime();

// Record delta time between calls to Render.
if(totalFrames_ % 30 == 0)
{
double now = Renderer::Window().GetTime();
const auto timeDelta = now - lastTime;
lastTime = now;
frameRate = static_cast<float>(30 / timeDelta);
}

// Render the UI
Statistics stats = {};


stats.FrameTime = static_cast<float>((now - lastTimestamp) * 1000.0);
lastTimestamp = now;

stats.Stats["gpu"] = Renderer::Device().DeviceProperties().deviceName;

stats.FramebufferSize = Renderer::Window().FramebufferSize();
stats.FrameRate = frameRate;
//stats.FrameTime = static_cast<float>(timeDelta * 1000);


stats.TotalFrames = totalFrames_;
stats.RenderTime = time_ - sceneInitialTime_;
Expand Down Expand Up @@ -427,6 +456,8 @@ void NextRendererApplication<Renderer>::OnCursorPosition(const double xpos, cons

// Camera motions
modelViewController_.OnCursorPosition(xpos, ypos);

mousePos_ = glm::vec2(xpos, ypos);
}

template <typename Renderer>
Expand All @@ -441,6 +472,16 @@ void NextRendererApplication<Renderer>::OnMouseButton(const int button, const in

// Camera motions
modelViewController_.OnMouseButton(button, action, mods);
#if !ANDROID
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
{
userSettings_.AutoFocus = true;
}
else if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE)
{
userSettings_.AutoFocus = false;
}
#endif
}

template <typename Renderer>
Expand Down Expand Up @@ -473,9 +514,22 @@ void NextRendererApplication<Renderer>::OnDropFile(int path_count, const char* p
{
userSettings_.SceneIndex = SceneList::AddExternalScene(path);
}

if( ext == "hdr")
{
Assets::GlobalTexturePool::UpdateHDRTexture(0, path, Vulkan::SamplerConfig());
userSettings_.SkyIdx = 0;
}
}
}

template <typename Renderer>
void NextRendererApplication<Renderer>::BeforeNextFrame()
{
TaskCoordinator::GetInstance()->Tick();
Renderer::BeforeNextFrame();
}

template <typename Renderer>
void NextRendererApplication<Renderer>::OnTouch(bool down, double xpos, double ypos)
{
Expand Down Expand Up @@ -538,7 +592,7 @@ void NextRendererApplication<Renderer>::LoadScene(const uint32_t sceneIndex)
userSettings_.FieldOfView = cameraInitialSate_.FieldOfView;
userSettings_.Aperture = cameraInitialSate_.Aperture;
userSettings_.FocusDistance = cameraInitialSate_.FocusDistance;
userSettings_.SkyIdx = userSettings_.HDRIfile != "" ? userSettings_.HDRIsLoaded - 1 : cameraInitialSate_.SkyIdx;
userSettings_.SkyIdx = cameraInitialSate_.SkyIdx;
userSettings_.SunRotation = cameraInitialSate_.SunRotation;

userSettings_.cameras = cameraInitialSate_.cameras;
Expand Down
4 changes: 3 additions & 1 deletion src/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ class NextRendererApplication final : public Renderer
void OnMouseButton(int button, int action, int mods) override;
void OnScroll(double xoffset, double yoffset) override;
void OnDropFile(int path_count, const char* paths[]) override;
void BeforeNextFrame() override;

Vulkan::Window& GetWindow() {return Renderer::Window();}

private:

void LoadScene(uint32_t sceneIndex);
Expand Down Expand Up @@ -96,4 +96,6 @@ class NextRendererApplication final : public Renderer
uint32_t benchmarkTotalFrames_{};
uint32_t benchmarkNumber_{0};
std::ofstream benchmarkCsvReportFile;

glm::vec2 mousePos_ {};
};
3 changes: 2 additions & 1 deletion src/Assets/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Scene::Scene(Vulkan::CommandPool& commandPool,
std::vector<Material>& materials,
std::vector<LightObject>& lights,
bool supportRayTracing) :
materials_(std::move(materials)),
models_(std::move(models)),
nodes_(std::move(nodes))
{
Expand Down Expand Up @@ -114,7 +115,7 @@ Scene::Scene(Vulkan::CommandPool& commandPool,

Vulkan::BufferUtil::CreateDeviceBuffer(commandPool, "Vertices", VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | rtxFlags | flags, vertices, vertexBuffer_, vertexBufferMemory_);
Vulkan::BufferUtil::CreateDeviceBuffer(commandPool, "Indices", VK_BUFFER_USAGE_INDEX_BUFFER_BIT | rtxFlags | flags, indices, indexBuffer_, indexBufferMemory_);
Vulkan::BufferUtil::CreateDeviceBuffer(commandPool, "Materials", flags, materials, materialBuffer_, materialBufferMemory_);
Vulkan::BufferUtil::CreateDeviceBuffer(commandPool, "Materials", flags, materials_, materialBuffer_, materialBufferMemory_);
Vulkan::BufferUtil::CreateDeviceBuffer(commandPool, "Offsets", flags, offsets_, offsetBuffer_, offsetBufferMemory_);

Vulkan::BufferUtil::CreateDeviceBuffer(commandPool, "AABBs", rtxFlags | flags, aabbs, aabbBuffer_, aabbBufferMemory_);
Expand Down
9 changes: 8 additions & 1 deletion src/Assets/Scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace Assets

const std::vector<Node>& Nodes() const { return nodes_; }
const std::vector<Model>& Models() const { return models_; }
const std::vector<Material>& Materials() const { return materials_; }
const std::vector<glm::uvec2>& Offsets() const { return offsets_; }


Expand All @@ -62,8 +63,12 @@ namespace Assets
const uint32_t GetIndicesCount() const {return indicesCount_;}
const uint32_t GetVerticeCount() const {return verticeCount_;}
const uint32_t GetIndirectDrawBatchCount() const {return indirectDrawBatchCount_;}


uint32_t GetSelectedId() const { return selectedId_; }
void SetSelectedId( uint32_t id ) const { selectedId_ = id; }

private:
const std::vector<Material> materials_;
const std::vector<Model> models_;
const std::vector<Node> nodes_;
std::vector<glm::uvec2> offsets_;
Expand Down Expand Up @@ -100,6 +105,8 @@ namespace Assets
uint32_t indicesCount_ {};
uint32_t verticeCount_ {};
uint32_t indirectDrawBatchCount_ {};

mutable uint32_t selectedId_ = -1;
};

}
Loading

0 comments on commit 7880e63

Please sign in to comment.