Slang対応

テクニカル諏訪子 <suwako@076.moe>、2026-07-21 12:19
コミット f7ec533c45d5051d13c50caa0c84ee3a0e201cc7 ・親 5f666f6 ・ファイルを見る

6個のファイル、+177 -58

@@ -29,26 +29,26 @@
2929 <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
3030 <ConfigurationType>Application</ConfigurationType>
3131 <UseDebugLibraries>true</UseDebugLibraries>
32- <PlatformToolset>v143</PlatformToolset>
32+ <PlatformToolset>v145</PlatformToolset>
3333 <CharacterSet>Unicode</CharacterSet>
3434 </PropertyGroup>
3535 <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
3636 <ConfigurationType>Application</ConfigurationType>
3737 <UseDebugLibraries>false</UseDebugLibraries>
38- <PlatformToolset>v143</PlatformToolset>
38+ <PlatformToolset>v145</PlatformToolset>
3939 <WholeProgramOptimization>true</WholeProgramOptimization>
4040 <CharacterSet>Unicode</CharacterSet>
4141 </PropertyGroup>
4242 <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
4343 <ConfigurationType>Application</ConfigurationType>
4444 <UseDebugLibraries>true</UseDebugLibraries>
45- <PlatformToolset>v143</PlatformToolset>
45+ <PlatformToolset>v145</PlatformToolset>
4646 <CharacterSet>Unicode</CharacterSet>
4747 </PropertyGroup>
4848 <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
4949 <ConfigurationType>Application</ConfigurationType>
5050 <UseDebugLibraries>false</UseDebugLibraries>
51- <PlatformToolset>v143</PlatformToolset>
51+ <PlatformToolset>v145</PlatformToolset>
5252 <WholeProgramOptimization>true</WholeProgramOptimization>
5353 <CharacterSet>Unicode</CharacterSet>
5454 </PropertyGroup>
@@ -100,10 +100,11 @@
100100 <ClCompile>
101101 <WarningLevel>Level3</WarningLevel>
102102 <SDLCheck>true</SDLCheck>
103- <PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
103+ <PreprocessorDefinitions>_DEBUG;_CONSOLE;USING_SLANG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
104104 <ConformanceMode>true</ConformanceMode>
105- <AdditionalIncludeDirectories>$(SolutionDir)dep\include;$(VULKAN_SDK)\Include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
105+ <AdditionalIncludeDirectories>$(SolutionDir)dep\include;$(VULKAN_SDK)\Include;$(ProjectDir)</AdditionalIncludeDirectories>
106106 <LanguageStandard>stdcpp20</LanguageStandard>
107+ <AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
107108 </ClCompile>
108109 <Link>
109110 <SubSystem>Console</SubSystem>
@@ -113,7 +114,8 @@
113114 </Link>
114115 <PostBuildEvent>
115116 <Command>$(VULKAN_SDK)\Bin\glslc.exe $(ProjectDir)shader.vert -o $(ProjectDir)vert.spv
116-$(VULKAN_SDK)\Bin\glslc.exe $(ProjectDir)shader.frag -o $(ProjectDir)frag.spv</Command>
117+$(VULKAN_SDK)\Bin\glslc.exe $(ProjectDir)shader.frag -o $(ProjectDir)frag.spv
118+$(VULKAN_SDK)\Bin\slangc.exe $(ProjectDir)shader.slang -target spirv -profile spirv_1_4 -emit-spirv-directly -fvk-use-entrypoint-name -entry vertMain -entry fragMain -o slang.spv</Command>
117119 </PostBuildEvent>
118120 </ItemDefinitionGroup>
119121 <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@@ -122,10 +124,11 @@
122124 <FunctionLevelLinking>true</FunctionLevelLinking>
123125 <IntrinsicFunctions>true</IntrinsicFunctions>
124126 <SDLCheck>true</SDLCheck>
125- <PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
127+ <PreprocessorDefinitions>NDEBUG;_CONSOLE;USING_SLANG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
126128 <ConformanceMode>true</ConformanceMode>
127- <AdditionalIncludeDirectories>$(SolutionDir)dep\include;$(VULKAN_SDK)\Include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
129+ <AdditionalIncludeDirectories>$(SolutionDir)dep\include;$(VULKAN_SDK)\Include;$(ProjectDir)</AdditionalIncludeDirectories>
128130 <LanguageStandard>stdcpp20</LanguageStandard>
131+ <AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
129132 </ClCompile>
130133 <Link>
131134 <SubSystem>Console</SubSystem>
@@ -135,11 +138,25 @@
135138 </Link>
136139 <PostBuildEvent>
137140 <Command>$(VULKAN_SDK)\Bin\glslc.exe $(ProjectDir)shader.vert -o $(ProjectDir)vert.spv
138-$(VULKAN_SDK)\Bin\glslc.exe $(ProjectDir)shader.frag -o $(ProjectDir)frag.spv</Command>
141+$(VULKAN_SDK)\Bin\glslc.exe $(ProjectDir)shader.frag -o $(ProjectDir)frag.spv
142+$(VULKAN_SDK)\Bin\slangc.exe $(ProjectDir)shader.slang -target spirv -profile spirv_1_4 -emit-spirv-directly -fvk-use-entrypoint-name -entry vertMain -entry fragMain -o slang.spv</Command>
139143 </PostBuildEvent>
140144 </ItemDefinitionGroup>
141145 <ItemGroup>
142146 <ClCompile Include="main.cc" />
147+ <ClCompile Include="shader\glsl.cc" />
148+ <ClCompile Include="shader\slang.cc" />
149+ <ClCompile Include="util\file.cc" />
150+ </ItemGroup>
151+ <ItemGroup>
152+ <ClInclude Include="shader\glsl.hh" />
153+ <ClInclude Include="shader\slang.hh" />
154+ <ClInclude Include="util\file.hh" />
155+ </ItemGroup>
156+ <ItemGroup>
157+ <None Include="shader.frag" />
158+ <None Include="shader.slang" />
159+ <None Include="shader.vert" />
143160 </ItemGroup>
144161 <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
145162 <ImportGroup Label="ExtensionTargets">
@@ -13,10 +13,53 @@
1313 <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
1414 <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
1515 </Filter>
16+ <Filter Include="Header Files\shader">
17+ <UniqueIdentifier>{6c2608ee-04db-4f8f-abd0-fd3a1d3f67b3}</UniqueIdentifier>
18+ </Filter>
19+ <Filter Include="Source Files\shader">
20+ <UniqueIdentifier>{06354e1d-5a76-473a-842e-b454c5e4b64f}</UniqueIdentifier>
21+ </Filter>
22+ <Filter Include="Header Files\util">
23+ <UniqueIdentifier>{025c7e9a-27a3-411d-993d-4964934e9584}</UniqueIdentifier>
24+ </Filter>
25+ <Filter Include="Source Files\util">
26+ <UniqueIdentifier>{04597cf8-3222-464c-b8ca-69b5e864bf20}</UniqueIdentifier>
27+ </Filter>
1628 </ItemGroup>
1729 <ItemGroup>
1830 <ClCompile Include="main.cc">
1931 <Filter>Source Files</Filter>
2032 </ClCompile>
33+ <ClCompile Include="shader\glsl.cc">
34+ <Filter>Source Files\shader</Filter>
35+ </ClCompile>
36+ <ClCompile Include="shader\slang.cc">
37+ <Filter>Source Files\shader</Filter>
38+ </ClCompile>
39+ <ClCompile Include="util\file.cc">
40+ <Filter>Source Files\util</Filter>
41+ </ClCompile>
42+ </ItemGroup>
43+ <ItemGroup>
44+ <ClInclude Include="shader\glsl.hh">
45+ <Filter>Header Files\shader</Filter>
46+ </ClInclude>
47+ <ClInclude Include="shader\slang.hh">
48+ <Filter>Header Files\shader</Filter>
49+ </ClInclude>
50+ <ClInclude Include="util\file.hh">
51+ <Filter>Header Files\util</Filter>
52+ </ClInclude>
53+ </ItemGroup>
54+ <ItemGroup>
55+ <None Include="shader.frag">
56+ <Filter>Resource Files</Filter>
57+ </None>
58+ <None Include="shader.slang">
59+ <Filter>Resource Files</Filter>
60+ </None>
61+ <None Include="shader.vert">
62+ <Filter>Resource Files</Filter>
63+ </None>
2164 </ItemGroup>
2265 </Project>
@@ -1,9 +1,9 @@
1-#define NOMINMAX
1+#define NOMINMAX
22
33 #pragma region headers
4+#include <util/file.hh>
5+
46 #include <iostream>
5-#include <string>
6-#include <vector>
77 #include <map>
88 #include <ranges>
99 #include <algorithm>
@@ -58,23 +58,6 @@
5858 }
5959 #pragma endregion
6060
61-#pragma region readShader
62-static std::vector<char> readFile(const std::string &filename) {
63- std::ifstream file(filename, std::ios::ate | std::ios::binary);
64- if (!file.is_open()) {
65-
66- return {};
67- }
68-
69- size_t fileSize = (size_t)file.tellg();
70- std::vector<char> buf(fileSize);
71- file.seekg(0);
72- file.read(buf.data(), fileSize);
73- file.close();
74- return buf;
75-}
76-#pragma endregion
77-
7861 #pragma region transitionImgLayout
7962 void transitionImgLayout(uint32_t imgIdx,
8063 vk::ImageLayout oldLayout,
@@ -165,7 +148,7 @@
165148 #pragma region window
166149 int winWidth = 800;
167150 int winHeight = 600;
168-
151+ std::string winName = reinterpret_cast<const char *>(u8"Vulkanレンダー");
169152
170153 glfwInit();
171154 glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
@@ -208,7 +191,7 @@
208191 });
209192
210193 if (!layerFound) {
211-
194+ assert(false && "必要なレイヤーを見つけられませんでした。\n");
212195 return -1;
213196 }
214197
@@ -262,7 +245,7 @@
262245 #pragma region physdevice
263246 auto physDevs = instance.enumeratePhysicalDevices();
264247 if (physDevs.empty()) {
265-
248+ assert(false && "Vulkanを対応しているGPUを見つけられませんでした。");
266249 return -1;
267250 }
268251
@@ -280,7 +263,7 @@
280263 }
281264
282265 if (physDevices.empty()) {
283-
266+ assert(false && "利用可能なGPUを見つけられませんでした。");
284267 return -1;
285268 }
286269
@@ -300,13 +283,14 @@
300283 });
301284
302285 auto features = physicalDevice.template getFeatures2<vk::PhysicalDeviceFeatures2,
286+ vk::PhysicalDeviceVulkan11Features,
303287 vk::PhysicalDeviceVulkan13Features,
304288 vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT>();
305289 bool supportReqFeature = features.template get<vk::PhysicalDeviceVulkan13Features>().dynamicRendering
306290 && features.template get<vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT>().extendedDynamicState;
307291
308292 if (!supportAllExt || !supportsGfx || !supportReqFeature) {
309-
293+ assert(false && "利用可能なGPUを見つけられませんでした。");
310294 return -1;
311295 }
312296 #pragma endregion
@@ -327,7 +311,7 @@
327311 }
328312
329313 if (queueIdx == ~0) {
330-
314+ assert(false && "グラフィックと表示塞翁しているキューを見つけられませんでした。");
331315 return -1;
332316 }
333317
@@ -335,7 +319,8 @@
335319 float queuePriority = .5f;
336320
337321 vk::PhysicalDeviceFeatures2 baseFeatures{};
338-
322+ vk::PhysicalDeviceVulkan11Features features11{};
323+ features11.shaderDrawParameters = true;
339324 vk::PhysicalDeviceVulkan13Features features13{};
340325 features13.dynamicRendering = true;
341326 features13.synchronization2 = true;
@@ -344,9 +329,11 @@
344329 extDynState.extendedDynamicState = true;
345330
346331 vk::StructureChain<vk::PhysicalDeviceFeatures2,
332+ vk::PhysicalDeviceVulkan11Features,
347333 vk::PhysicalDeviceVulkan13Features,
348334 vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT> featureChain = {
349335 baseFeatures,
336+ features11,
350337 features13,
351338 extDynState,
352339 };
@@ -443,30 +430,47 @@
443430 #pragma endregion
444431
445432 #pragma region gfxPipeline
446- auto vertShader = readFile("vert.spv");
447- if (vertShader.empty()) return -1;
448- auto fragShader = readFile("frag.spv");
449- if (fragShader.empty()) return -1;
433+ //auto vertShader = readFile("vert.spv");
434+ //if (vertShader.empty()) return -1;
435+ //auto fragShader = readFile("frag.spv");
436+ //if (fragShader.empty()) return -1;
450437
451- vk::ShaderModuleCreateInfo vertCreateInfo = {};
452- vertCreateInfo.codeSize = vertShader.size() * sizeof(char);
453- vertCreateInfo.pCode = reinterpret_cast<const uint32_t *>(vertShader.data());
454- vk::ShaderModule vertShaderModule = device.createShaderModule(vertCreateInfo);
438+ //vk::ShaderModuleCreateInfo vertCreateInfo = {};
439+ //vertCreateInfo.codeSize = vertShader.size() * sizeof(char);
440+ //vertCreateInfo.pCode = reinterpret_cast<const uint32_t *>(vertShader.data());
441+ //vk::ShaderModule vertShaderModule = device.createShaderModule(vertCreateInfo);
455442
456- vk::ShaderModuleCreateInfo fragCreateInfo = {};
457- fragCreateInfo.codeSize = fragShader.size() * sizeof(char);
458- fragCreateInfo.pCode = reinterpret_cast<const uint32_t *>(fragShader.data());
459- vk::ShaderModule fragShaderModule = device.createShaderModule(fragCreateInfo);
443+ //vk::ShaderModuleCreateInfo fragCreateInfo = {};
444+ //fragCreateInfo.codeSize = fragShader.size() * sizeof(char);
445+ //fragCreateInfo.pCode = reinterpret_cast<const uint32_t *>(fragShader.data());
446+ //vk::ShaderModule fragShaderModule = device.createShaderModule(fragCreateInfo);
460447
448+ //vk::PipelineShaderStageCreateInfo vertShaderStageInfo = {};
449+ //vertShaderStageInfo.stage = vk::ShaderStageFlagBits::eVertex;
450+ //vertShaderStageInfo.module = vertShaderModule;
451+ //vertShaderStageInfo.pName = "main";
452+
453+ //vk::PipelineShaderStageCreateInfo fragShaderStageInfo = {};
454+ //fragShaderStageInfo.stage = vk::ShaderStageFlagBits::eFragment;
455+ //fragShaderStageInfo.module = fragShaderModule;
456+ //fragShaderStageInfo.pName = "main";
457+
458+ auto shader = readFile("slang.spv");
459+
460+ vk::ShaderModuleCreateInfo info = {};
461+ info.codeSize = shader.size();
462+ info.pCode = reinterpret_cast<const uint32_t *>(shader.data());
463+ vk::ShaderModule shaderModule = device.createShaderModule(info);
464+
461465 vk::PipelineShaderStageCreateInfo vertShaderStageInfo = {};
462466 vertShaderStageInfo.stage = vk::ShaderStageFlagBits::eVertex;
463- vertShaderStageInfo.module = vertShaderModule;
464- vertShaderStageInfo.pName = "main";
467+ vertShaderStageInfo.module = shaderModule;
468+ vertShaderStageInfo.pName = "vertMain";
465469
466470 vk::PipelineShaderStageCreateInfo fragShaderStageInfo = {};
467471 fragShaderStageInfo.stage = vk::ShaderStageFlagBits::eFragment;
468- fragShaderStageInfo.module = fragShaderModule;
469- fragShaderStageInfo.pName = "main";
472+ fragShaderStageInfo.module = shaderModule;
473+ fragShaderStageInfo.pName = "fragMain";
470474
471475 vk::PipelineShaderStageCreateInfo shaderStages[] = {
472476 vertShaderStageInfo,
@@ -567,7 +571,7 @@
567571
568572 auto [result, pipeline] = device.createGraphicsPipeline(nullptr, pipelineInfo);
569573 if (result != vk::Result::eSuccess) {
570-
574+ assert(false && "グラフィックパイプラインの設置にしっぱい。");
571575 return -1;
572576 }
573577
@@ -623,7 +627,7 @@
623627 #pragma region drawFrame
624628 auto fenceResult = device.waitForFences(drawFence, vk::True, UINT64_MAX);
625629 if (fenceResult != vk::Result::eSuccess) {
626-
630+ assert(false && "フェンスまで待つに失敗。");
627631 break;
628632 }
629633
@@ -632,7 +636,7 @@
632636
633637 if (acquireRes == vk::Result::eErrorOutOfDateKHR || acquireRes == vk::Result::eSuboptimalKHR) continue;
634638 if (acquireRes != vk::Result::eSuccess) {
635-
639+ assert(false && "次の画像の受け取りに失敗。");
636640 break;
637641 }
638642
@@ -677,8 +681,9 @@
677681 if (cmdPool) device.destroyCommandPool(cmdPool);
678682 if (gfxPipeline) device.destroyPipeline(gfxPipeline);
679683 if (pipelineLayout) device.destroyPipelineLayout(pipelineLayout);
680- if (fragShaderModule) device.destroyShaderModule(fragShaderModule);
681- if (vertShaderModule) device.destroyShaderModule(vertShaderModule);
684+ if (shaderModule) device.destroyShaderModule(shaderModule);
685+ //if (fragShaderModule) device.destroyShaderModule(fragShaderModule);
686+ //if (vertShaderModule) device.destroyShaderModule(vertShaderModule);
682687
683688 for (auto &view : swapChainImageViews) device.destroyImageView(view);
684689 swapChainImageViews.clear();
@@ -0,0 +1,30 @@
1+static float2 positions[3] = float2[](
2+ float2( 0.0, -0.5),
3+ float2( 0.5, 0.5),
4+ float2(-0.5, 0.5),
5+);
6+
7+static float3 colors[3] = float3[](
8+ float3(1.0, 0.0, 0.0),
9+ float3(0.0, 1.0, 0.0),
10+ float3(0.0, 0.0, 1.0),
11+);
12+
13+struct VertexOutput {
14+ float3 color;
15+ float4 sv_position: SV_Position;
16+};
17+
18+[shader("vertex")]
19+VertexOutput vertMain(uint vid: SV_VertexID) {
20+ VertexOutput out;
21+ out.sv_position = float4(positions[vid], 0.0, 1.0);
22+ out.color = colors[vid];
23+ return out;
24+}
25+
26+[shader("fragment")]
27+float4 fragMain(VertexOutput inVert) : SV_Target {
28+ float3 color = inVert.color;
29+ return float4(color, 1.0);
30+}
@@ -0,0 +1,18 @@
1+#include <util/file.hh>
2+#include <fstream>
3+#include <cassert>
4+
5+std::vector<char> readFile(const std::string &filename) {
6+ std::ifstream file(filename, std::ios::ate | std::ios::binary);
7+ if (!file.is_open()) {
8+ assert(false && u8"ファイルを開くに失敗。");
9+ return {};
10+ }
11+
12+ size_t fileSize = (size_t)file.tellg();
13+ std::vector<char> buf(fileSize);
14+ file.seekg(0);
15+ file.read(buf.data(), fileSize);
16+ file.close();
17+ return buf;
18+}
@@ -0,0 +1,6 @@
1+#pragma once
2+
3+#include <string>
4+#include <vector>
5+
6+std::vector<char> readFile(const std::string &filename);