Skip to content

Commit

Permalink
Implemented edge detection demo
Browse files Browse the repository at this point in the history
  • Loading branch information
vcoda committed Aug 25, 2020
1 parent cec6b9d commit 831041c
Show file tree
Hide file tree
Showing 10 changed files with 448 additions and 5 deletions.
15 changes: 15 additions & 0 deletions aggregated-graphics-samples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "debug-tangents", "debug-tan
{67B01ECD-2613-4FA0-84F7-87F5BCF40EB1} = {67B01ECD-2613-4FA0-84F7-87F5BCF40EB1}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "edge-detection", "edge-detection\edge-detection.vcxproj", "{591F9775-8088-457A-B237-A7EA1DEF699D}"
ProjectSection(ProjectDependencies) = postProject
{8D9D4A3E-439A-4210-8879-259B20D992CA} = {8D9D4A3E-439A-4210-8879-259B20D992CA}
{51CC36B3-921E-4853-ACD5-CB6CBC27FBA1} = {51CC36B3-921E-4853-ACD5-CB6CBC27FBA1}
{67B01ECD-2613-4FA0-84F7-87F5BCF40EB1} = {67B01ECD-2613-4FA0-84F7-87F5BCF40EB1}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand Down Expand Up @@ -271,6 +278,14 @@ Global
{AC7F8BDD-74EB-4B86-B7D8-FA79797C6ACB}.Release|x64.Build.0 = Release|x64
{AC7F8BDD-74EB-4B86-B7D8-FA79797C6ACB}.Release|x86.ActiveCfg = Release|Win32
{AC7F8BDD-74EB-4B86-B7D8-FA79797C6ACB}.Release|x86.Build.0 = Release|Win32
{591F9775-8088-457A-B237-A7EA1DEF699D}.Debug|x64.ActiveCfg = Debug|x64
{591F9775-8088-457A-B237-A7EA1DEF699D}.Debug|x64.Build.0 = Debug|x64
{591F9775-8088-457A-B237-A7EA1DEF699D}.Debug|x86.ActiveCfg = Debug|Win32
{591F9775-8088-457A-B237-A7EA1DEF699D}.Debug|x86.Build.0 = Debug|Win32
{591F9775-8088-457A-B237-A7EA1DEF699D}.Release|x64.ActiveCfg = Release|x64
{591F9775-8088-457A-B237-A7EA1DEF699D}.Release|x64.Build.0 = Release|x64
{591F9775-8088-457A-B237-A7EA1DEF699D}.Release|x86.ActiveCfg = Release|Win32
{591F9775-8088-457A-B237-A7EA1DEF699D}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
156 changes: 156 additions & 0 deletions edge-detection/edge-detection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#include "graphicsApp.h"
#include "colorTable.h"
#include "quadric/include/cube.h"
#include "quadric/include/sphere.h"
#include "quadric/include/teapot.h"

class EdgeDetection : public GraphicsApp
{
enum {
Cube = 0, Teapot, Sphere,
MaxObjects
};

std::unique_ptr<quadric::Quadric> objects[MaxObjects];
std::shared_ptr<magma::GraphicsPipeline> depthPipeline;
std::shared_ptr<magma::aux::DepthFramebuffer> depthFramebuffer;
DescriptorSet descriptor;

rapid::matrix objTransforms[MaxObjects];

public:
explicit EdgeDetection(const AppEntry& entry):
GraphicsApp(entry, TEXT("Edge detection"), 1280, 720, false)
{
setupViewProjection();
setupTransforms();
createDepthFramebuffer();
createMeshObjects();
setupDescriptorSets();
setupGraphicsPipelines();

renderScene(drawCmdBuffer);
blit(msaaFramebuffer->getColorView(), FrontBuffer);
blit(msaaFramebuffer->getColorView(), BackBuffer);
}

virtual void render(uint32_t bufferIndex) override
{
updateTransforms();
submitCommandBuffers(bufferIndex);
sleep(2); // Cap fps
}

void setupViewProjection()
{
viewProj = std::make_unique<LeftHandedViewProjection>();
viewProj->setPosition(0.f, 11.f, -18.f);
viewProj->setFocus(0.f, 0.f, -1.f);
viewProj->setFieldOfView(45.f);
viewProj->setNearZ(1.f);
viewProj->setFarZ(100.f);
viewProj->setAspectRatio(width/(float)height);
viewProj->updateView();
viewProj->updateProjection();

updateViewProjTransforms();
}

void setupTransforms()
{
createTransformBuffer(MaxObjects);
constexpr float radius = 4.f;
objTransforms[Cube] = rapid::translation(radius, 1.f, 0.f) * rapid::rotationY(rapid::radians(30.f));
objTransforms[Teapot] = rapid::translation(radius, 0.f, 0.f) * rapid::rotationY(rapid::radians(150.f));
objTransforms[Sphere] = rapid::translation(radius, 1.5f, 0.f) * rapid::rotationY(rapid::radians(270.f));
}

void updateTransforms()
{
const rapid::matrix rotation = rapid::rotationY(rapid::radians(-spinX/4.f));
std::vector<rapid::matrix, core::aligned_allocator<rapid::matrix>> transforms(MaxObjects);
transforms[Cube] = objTransforms[Cube] * rotation,
transforms[Teapot] = objTransforms[Teapot] * rotation,
transforms[Sphere] = objTransforms[Sphere] * rotation,
updateObjectTransforms(transforms);
}

void createDepthFramebuffer()
{
depthFramebuffer = std::make_shared<magma::aux::DepthFramebuffer>(device, VK_FORMAT_D16_UNORM, msaaFramebuffer->getExtent());
}

void createMeshObjects()
{
objects[Cube] = std::make_unique<quadric::Cube>(cmdCopyBuf);
objects[Teapot] = std::make_unique<quadric::Teapot>(16, cmdCopyBuf);
objects[Sphere] = std::make_unique<quadric::Sphere>(1.5f, 64, 64, false, cmdCopyBuf);
}

void setupDescriptorSets()
{
using namespace magma::bindings;
using namespace magma::descriptors;
descriptor.layout = std::shared_ptr<magma::DescriptorSetLayout>(new magma::DescriptorSetLayout(device,
VertexStageBinding(0, DynamicUniformBuffer(1))));
descriptor.set = descriptorPool->allocateDescriptorSet(descriptor.layout);
descriptor.set->update(0, transforms);
}

void setupGraphicsPipelines()
{
depthPipeline = createDepthOnlyPipeline(
"transform.o",
objects[0]->getVertexInput(),
descriptor.layout,
depthFramebuffer);
}

void renderScene(std::shared_ptr<magma::CommandBuffer> cmdBuffer)
{
cmdBuffer->begin();
{
depthPass(cmdBuffer);
edgeDetectPass(cmdBuffer);
}
cmdBuffer->end();
}

void depthPass(std::shared_ptr<magma::CommandBuffer> cmdBuffer)
{
cmdBuffer->beginRenderPass(depthFramebuffer->getRenderPass(), depthFramebuffer->getFramebuffer(),
{
magma::clears::depthOne
});
{
cmdBuffer->setViewport(magma::Viewport(0, 0, depthFramebuffer->getExtent()));
cmdBuffer->setScissor(magma::Scissor(0, 0, depthFramebuffer->getExtent()));
cmdBuffer->bindPipeline(depthPipeline);
for (uint32_t i = Cube; i < MaxObjects; ++i)
{
cmdBuffer->bindDescriptorSet(depthPipeline, descriptor.set, transforms->getDynamicOffset(i));
objects[i]->draw(cmdBuffer);
}
}
cmdBuffer->endRenderPass();
}

void edgeDetectPass(std::shared_ptr<magma::CommandBuffer> cmdBuffer)
{
cmdBuffer->beginRenderPass(msaaFramebuffer->getRenderPass(), msaaFramebuffer->getFramebuffer());
{
if (!bltRect)
bltRect = std::make_unique<magma::aux::BlitRectangle>(renderPass, loadShader("edgeDetect.o"));
bltRect->blit(cmdBuffer,
depthFramebuffer->getDepthView(),
VK_FILTER_NEAREST,
VkRect2D{0, 0, msaaFramebuffer->getExtent()});
}
cmdBuffer->endRenderPass();
}
};

std::unique_ptr<IApplication> appFactory(const AppEntry& entry)
{
return std::unique_ptr<EdgeDetection>(new EdgeDetection(entry));
}
178 changes: 178 additions & 0 deletions edge-detection/edge-detection.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{591F9775-8088-457A-B237-A7EA1DEF699D}</ProjectGuid>
<RootNamespace>edgedetection</RootNamespace>
<WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(VK_SDK_PATH)\Include;..\third-party;..\framework</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_DEBUG;VK_USE_PLATFORM_WIN32_KHR;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<AdditionalLibraryDirectories>$(VK_SDK_PATH)\Lib32;..\Debug</AdditionalLibraryDirectories>
<AdditionalDependencies>vulkan-1.lib;magma.lib;quadric.lib;framework.lib;Shcore.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(VK_SDK_PATH)\Include;..\third-party;..\framework</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_DEBUG;VK_USE_PLATFORM_WIN32_KHR;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>$(VK_SDK_PATH)\Lib;..\x64\Debug</AdditionalLibraryDirectories>
<AdditionalDependencies>vulkan-1.lib;magma.lib;quadric.lib;framework.lib;Shcore.lib;%(AdditionalDependencies)</AdditionalDependencies>
<SubSystem>Windows</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(VK_SDK_PATH)\Include;..\third-party;..\framework</AdditionalIncludeDirectories>
<PreprocessorDefinitions>NDEBUG;VK_USE_PLATFORM_WIN32_KHR;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<SubSystem>Windows</SubSystem>
<AdditionalLibraryDirectories>$(VK_SDK_PATH)\Lib32;..\Release</AdditionalLibraryDirectories>
<AdditionalDependencies>vulkan-1.lib;magma.lib;quadric.lib;framework.lib;Shcore.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(VK_SDK_PATH)\Include;..\third-party;..\framework</AdditionalIncludeDirectories>
<PreprocessorDefinitions>NDEBUG;VK_USE_PLATFORM_WIN32_KHR;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<SubSystem>Windows</SubSystem>
<AdditionalLibraryDirectories>$(VK_SDK_PATH)\Lib;..\x64\Release</AdditionalLibraryDirectories>
<AdditionalDependencies>vulkan-1.lib;magma.lib;quadric.lib;framework.lib;Shcore.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="edge-detection.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="shaders\sobel.h" />
</ItemGroup>
<ItemGroup>
<CustomBuild Include="shaders\edgeDetect.frag">
<FileType>Document</FileType>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(VK_SDK_PATH)\Bin32\glslangValidator.exe -V %(FullPath) -I..\framework\shaders -o %(Filename).o</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(VK_SDK_PATH)\Bin32\glslangValidator.exe -V %(FullPath) -I..\framework\shaders -o %(Filename).o</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(VK_SDK_PATH)\Bin32\glslangValidator.exe -V %(FullPath) -I..\framework\shaders -o %(Filename).o</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(VK_SDK_PATH)\Bin32\glslangValidator.exe -V %(FullPath) -I..\framework\shaders -o %(Filename).o</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">%(Filename).o</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">%(Filename).o</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).o</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).o</Outputs>
</CustomBuild>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="shaders\transform.vert">
<FileType>Document</FileType>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(VK_SDK_PATH)\Bin32\glslangValidator.exe -V %(FullPath) -I..\framework\shaders -o %(Filename).o</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(VK_SDK_PATH)\Bin32\glslangValidator.exe -V %(FullPath) -I..\framework\shaders -o %(Filename).o</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(VK_SDK_PATH)\Bin32\glslangValidator.exe -V %(FullPath) -I..\framework\shaders -o %(Filename).o</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(VK_SDK_PATH)\Bin32\glslangValidator.exe -V %(FullPath) -I..\framework\shaders -o %(Filename).o</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">%(Filename).o</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">%(Filename).o</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).o</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).o</Outputs>
</CustomBuild>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Loading

0 comments on commit 831041c

Please sign in to comment.