Compare commits

...

2 Commits

Author SHA1 Message Date
Caleb Cornett 2b773101c8 Added shader cross-compiler using glslc and spirv-cross 2023-01-14 23:34:55 -05:00
TheSpydog f7250ab12a Remove fixed command buffers + minor cleanup (#33)
Removes the `fixed` parameter from AcquireCommandBuffer, and removes a couple other unused bools from the VulkanCommandBuffer struct.

Co-authored-by: Caleb Cornett <caleb.cornett@outlook.com>
Reviewed-on: MoonsideGames/Refresh#33
Co-authored-by: TheSpydog <thespydog@noreply.example.org>
Co-committed-by: TheSpydog <thespydog@noreply.example.org>
2023-01-14 18:03:58 +00:00
6 changed files with 120 additions and 32 deletions

View File

@ -1168,15 +1168,9 @@ REFRESHAPI Refresh_TextureFormat Refresh_GetSwapchainFormat(
* A command buffer may only be used on the thread that
* it was acquired on. Using it on any other thread is an error.
*
* fixed:
* If a command buffer is designated as fixed, it can be
* acquired once, have commands recorded into it, and
* be re-submitted indefinitely.
*
*/
REFRESHAPI Refresh_CommandBuffer* Refresh_AcquireCommandBuffer(
Refresh_Device *device,
uint8_t fixed
Refresh_Device *device
);
/* Acquires a texture to use for presentation.

105
shadercompiler/Program.cs Normal file
View File

@ -0,0 +1,105 @@
using System;
using System.IO;
using System.Diagnostics;
partial class Program
{
public static int Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Usage: refreshc <path-to-glsl> <output-directory>");
return 1;
}
string glslPath = args[0];
string outputDir = args[1];
if (!ValidateArgs(glslPath, outputDir))
{
return 1;
}
string shaderName = Path.GetFileNameWithoutExtension(glslPath);
string shaderType = Path.GetExtension(glslPath);
string spirvPath = Path.Combine(outputDir, $"{shaderName}.spv");
if (CompileGlslToSpirv(glslPath, shaderName, spirvPath) != 0)
{
return 1;
}
string hlslPath = Path.Combine(outputDir, $"{shaderName}.hlsl");
if (TranslateSpirvToHlsl(spirvPath, hlslPath) != 0)
{
return 1;
}
// FIXME: Is there a cross-platform way to compile HLSL to DXBC?
#if PS5
if (TranslateHlslToPS5(hlslPath, shaderName, shaderType, outputDir) != 0)
{
return 1;
}
#endif
return 0;
}
static bool ValidateArgs(string glslPath, string outputDir)
{
if (!File.Exists(glslPath))
{
Console.WriteLine($"refreshc: glsl source file ({glslPath}) does not exist");
return false;
}
string ext = Path.GetExtension(glslPath);
if (ext != ".vert" && ext != ".frag" && ext != ".comp")
{
Console.WriteLine("refreshc: Expected glsl source file with extension '.vert', '.frag', or '.comp'");
return false;
}
if (!Directory.Exists(outputDir))
{
Console.WriteLine($"refreshc: Output directory ({outputDir}) does not exist");
return false;
}
return true;
}
static int CompileGlslToSpirv(string glslPath, string shaderName, string outputPath)
{
Process glslc = Process.Start(
"glslc",
$"{glslPath} -o {outputPath}"
);
glslc.WaitForExit();
if (glslc.ExitCode != 0)
{
Console.WriteLine($"refreshc: Could not compile GLSL code");
return 1;
}
return 0;
}
static int TranslateSpirvToHlsl(string spirvPath, string outputPath)
{
Process spirvcross = Process.Start(
"spirv-cross",
$"{spirvPath} --hlsl --shader-model 50 --output {outputPath}"
);
spirvcross.WaitForExit();
if (spirvcross.ExitCode != 0)
{
Console.WriteLine($"refreshc: Could not translate SPIR-V to HLSL");
return 1;
}
return 0;
}
}

View File

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetName>refreshc</TargetName>
</PropertyGroup>
</Project>

View File

@ -789,13 +789,11 @@ void Refresh_UnclaimWindow(
}
Refresh_CommandBuffer* Refresh_AcquireCommandBuffer(
Refresh_Device *device,
uint8_t fixed
Refresh_Device *device
) {
NULL_RETURN_NULL(device);
return device->AcquireCommandBuffer(
device->driverData,
fixed
device->driverData
);
}

View File

@ -470,8 +470,7 @@ struct Refresh_Device
);
Refresh_CommandBuffer* (*AcquireCommandBuffer)(
Refresh_Renderer *driverData,
uint8_t fixed
Refresh_Renderer *driverData
);
Refresh_Texture* (*AcquireSwapchainTexture)(

View File

@ -1500,10 +1500,6 @@ typedef struct VulkanCommandPool VulkanCommandPool;
typedef struct VulkanCommandBuffer
{
VkCommandBuffer commandBuffer;
uint8_t fixed;
uint8_t submitted;
uint8_t renderPassInProgress;
VulkanCommandPool *commandPool;
VulkanPresentData *presentDatas;
@ -4662,11 +4658,7 @@ static void VULKAN_INTERNAL_BeginCommandBuffer(
beginInfo.pNext = NULL;
beginInfo.flags = 0;
beginInfo.pInheritanceInfo = NULL;
if (!commandBuffer->fixed)
{
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
}
result = renderer->vkBeginCommandBuffer(
commandBuffer->commandBuffer,
@ -8484,8 +8476,6 @@ static void VULKAN_BeginRenderPass(
VK_SUBPASS_CONTENTS_INLINE
);
vulkanCommandBuffer->renderPassInProgress = 1;
SDL_stack_free(clearValues);
for (i = 0; i < colorAttachmentCount; i += 1)
@ -8622,7 +8612,6 @@ static void VULKAN_EndRenderPass(
vulkanCommandBuffer->renderPassDepthTexture = NULL;
vulkanCommandBuffer->currentGraphicsPipeline = NULL;
vulkanCommandBuffer->renderPassInProgress = 0;
}
static void VULKAN_BindGraphicsPipeline(
@ -9197,8 +9186,7 @@ static VulkanCommandBuffer* VULKAN_INTERNAL_GetInactiveCommandBufferFromPool(
}
static Refresh_CommandBuffer* VULKAN_AcquireCommandBuffer(
Refresh_Renderer *driverData,
uint8_t fixed
Refresh_Renderer *driverData
) {
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
VkResult result;
@ -9221,10 +9209,6 @@ static Refresh_CommandBuffer* VULKAN_AcquireCommandBuffer(
commandBuffer->fragmentUniformBuffer = NULL;
commandBuffer->computeUniformBuffer = NULL;
commandBuffer->fixed = fixed;
commandBuffer->submitted = 0;
commandBuffer->renderPassInProgress = 0;
commandBuffer->renderPassColorTargetCount = 0;
VULKAN_INTERNAL_BeginCommandBuffer(renderer, commandBuffer);
@ -9920,7 +9904,6 @@ static void VULKAN_Submit(
);
}
((VulkanCommandBuffer*)pCommandBuffers[i])->submitted = 1;
renderer->submittedCommandBuffers[renderer->submittedCommandBufferCount] = (VulkanCommandBuffer*) pCommandBuffers[i];
renderer->submittedCommandBufferCount += 1;