[go: nahoru, domu]

Skip to content

Commit

Permalink
Add shift resource bindings in Vulkan (#50)
Browse files Browse the repository at this point in the history
* Added Options parameters to C# wrapper
* Added Vulkan Shift resource binding options
* Fixed issue packMatricesInRowMajor is inverted
  • Loading branch information
Jorgemagic authored and Minmin Gong committed Sep 25, 2019
1 parent d1c81f5 commit b514328
Show file tree
Hide file tree
Showing 25 changed files with 104 additions and 44 deletions.
11 changes: 8 additions & 3 deletions Include/ShaderConductor/ShaderConductor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,17 @@ namespace ShaderConductor
struct Options
{
bool packMatricesInRowMajor = true; // Experimental: Decide how a matrix get packed
bool enable16bitTypes = false; // Enable 16-bit types, such as half, uint16_t. Requires shader model 6.2+
bool enableDebugInfo = false; // Embed debug info into the binary
bool disableOptimizations = false; // Force to turn off optimizations. Ignore optimizationLevel below.
bool enable16bitTypes = false; // Enable 16-bit types, such as half, uint16_t. Requires shader model 6.2+
bool enableDebugInfo = false; // Embed debug info into the binary
bool disableOptimizations = false; // Force to turn off optimizations. Ignore optimizationLevel below.

int optimizationLevel = 3; // 0 to 3, no optimization to most optimization
ShaderModel shaderModel = { 6, 0 };

int shiftAllTexturesBindings;
int shiftAllSamplersBindings;
int shiftAllCBuffersBindings;
int shiftAllUABuffersBindings;
};

struct TargetDesc
Expand Down
36 changes: 32 additions & 4 deletions Source/Core/ShaderConductor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ namespace
}

*includeSource = nullptr;
return Dxcompiler::Instance().Library()->CreateBlobWithEncodingOnHeapCopy(
source->Data(), source->Size(), CP_UTF8, reinterpret_cast<IDxcBlobEncoding**>(includeSource));
return Dxcompiler::Instance().Library()->CreateBlobWithEncodingOnHeapCopy(source->Data(), source->Size(), CP_UTF8,
reinterpret_cast<IDxcBlobEncoding**>(includeSource));
}

ULONG STDMETHODCALLTYPE AddRef() override
Expand Down Expand Up @@ -392,11 +392,11 @@ namespace
// See also https://antiagainst.github.io/post/hlsl-for-vulkan-matrices/
if (options.packMatricesInRowMajor)
{
dxcArgStrings.push_back(L"-Zpc");
dxcArgStrings.push_back(L"-Zpr");
}
else
{
dxcArgStrings.push_back(L"-Zpr");
dxcArgStrings.push_back(L"-Zpc");
}

if (options.enable16bitTypes)
Expand Down Expand Up @@ -432,6 +432,34 @@ namespace
}
}

if (options.shiftAllCBuffersBindings > 0)
{
dxcArgStrings.push_back(L"-fvk-b-shift");
dxcArgStrings.push_back(std::to_wstring(options.shiftAllCBuffersBindings));
dxcArgStrings.push_back(L"all");
}

if (options.shiftAllUABuffersBindings > 0)
{
dxcArgStrings.push_back(L"-fvk-u-shift");
dxcArgStrings.push_back(std::to_wstring(options.shiftAllUABuffersBindings));
dxcArgStrings.push_back(L"all");
}

if (options.shiftAllSamplersBindings > 0)
{
dxcArgStrings.push_back(L"-fvk-s-shift");
dxcArgStrings.push_back(std::to_wstring(options.shiftAllSamplersBindings));
dxcArgStrings.push_back(L"all");
}

if (options.shiftAllTexturesBindings > 0)
{
dxcArgStrings.push_back(L"-fvk-t-shift");
dxcArgStrings.push_back(std::to_wstring(options.shiftAllTexturesBindings));
dxcArgStrings.push_back(L"all");
}

switch (targetLanguage)
{
case ShadingLanguage::Dxil:
Expand Down
2 changes: 1 addition & 1 deletion Source/Tests/Data/Expected/PNTriangles_DS.300.essl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ layout(triangles) in;

layout(std140) uniform type_cbPNTriangles
{
layout(row_major) mat4 viewProj;
mat4 viewProj;
vec4 lightDir;
} cbPNTriangles;

Expand Down
2 changes: 1 addition & 1 deletion Source/Tests/Data/Expected/PNTriangles_DS.300.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ out gl_PerVertex

layout(std140) uniform type_cbPNTriangles
{
layout(row_major) mat4 viewProj;
mat4 viewProj;
vec4 lightDir;
} cbPNTriangles;

Expand Down
2 changes: 1 addition & 1 deletion Source/Tests/Data/Expected/PNTriangles_DS.310.essl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ layout(triangles) in;

layout(binding = 0, std140) uniform type_cbPNTriangles
{
layout(row_major) mat4 viewProj;
mat4 viewProj;
vec4 lightDir;
} cbPNTriangles;

Expand Down
2 changes: 1 addition & 1 deletion Source/Tests/Data/Expected/PNTriangles_DS.410.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ out gl_PerVertex

layout(std140) uniform type_cbPNTriangles
{
layout(row_major) mat4 viewProj;
mat4 viewProj;
vec4 lightDir;
} cbPNTriangles;

Expand Down
2 changes: 1 addition & 1 deletion Source/Tests/Data/Expected/PNTriangles_DS.msl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct main0_patchIn
float _70 = _67 * 3.0;
float _71 = _68 * 3.0;
float _72 = _69 * 3.0;
out.gl_Position = float4(((((((((((in_var_POSITION[0] * _69) * gl_TessCoord.z) + ((in_var_POSITION[1] * _67) * gl_TessCoord.x)) + ((in_var_POSITION[2] * _68) * gl_TessCoord.y)) + ((patchIn.in_var_POSITION3 * _72) * gl_TessCoord.x)) + ((patchIn.in_var_POSITION4 * gl_TessCoord.z) * _70)) + ((patchIn.in_var_POSITION8 * _72) * gl_TessCoord.y)) + ((patchIn.in_var_POSITION5 * _70) * gl_TessCoord.y)) + ((patchIn.in_var_POSITION7 * gl_TessCoord.z) * _71)) + ((patchIn.in_var_POSITION6 * gl_TessCoord.x) * _71)) + ((((patchIn.in_var_CENTER * 6.0) * gl_TessCoord.z) * gl_TessCoord.x) * gl_TessCoord.y), 1.0) * cbPNTriangles.viewProj;
out.gl_Position = cbPNTriangles.viewProj * float4(((((((((((in_var_POSITION[0] * _69) * gl_TessCoord.z) + ((in_var_POSITION[1] * _67) * gl_TessCoord.x)) + ((in_var_POSITION[2] * _68) * gl_TessCoord.y)) + ((patchIn.in_var_POSITION3 * _72) * gl_TessCoord.x)) + ((patchIn.in_var_POSITION4 * gl_TessCoord.z) * _70)) + ((patchIn.in_var_POSITION8 * _72) * gl_TessCoord.y)) + ((patchIn.in_var_POSITION5 * _70) * gl_TessCoord.y)) + ((patchIn.in_var_POSITION7 * gl_TessCoord.z) * _71)) + ((patchIn.in_var_POSITION6 * gl_TessCoord.x) * _71)) + ((((patchIn.in_var_CENTER * 6.0) * gl_TessCoord.z) * gl_TessCoord.x) * gl_TessCoord.y), 1.0);
out.out_var_TEXCOORD0 = ((in_var_TEXCOORD[0] * gl_TessCoord.z) + (in_var_TEXCOORD[1] * gl_TessCoord.x)) + (in_var_TEXCOORD[2] * gl_TessCoord.y);
return out;
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Tests/Data/Expected/Particle_GS.300.essl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const vec2 _48[4] = vec2[](vec2(0.0, 1.0), vec2(1.0), vec2(0.0), vec2(1.0, 0.0))

layout(std140) uniform type_cbMain
{
layout(row_major) mat4 invView;
layout(row_major) mat4 viewProj;
mat4 invView;
mat4 viewProj;
} cbMain;

in vec4 in_var_POSITION[1];
Expand Down
4 changes: 2 additions & 2 deletions Source/Tests/Data/Expected/Particle_GS.300.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const vec2 _48[4] = vec2[](vec2(0.0, 1.0), vec2(1.0), vec2(0.0), vec2(1.0, 0.0))

layout(std140) uniform type_cbMain
{
layout(row_major) mat4 invView;
layout(row_major) mat4 viewProj;
mat4 invView;
mat4 viewProj;
} cbMain;

layout(location = 0) in vec4 in_var_POSITION[1];
Expand Down
4 changes: 2 additions & 2 deletions Source/Tests/Data/Expected/Particle_GS.310.essl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const vec2 _48[4] = vec2[](vec2(0.0, 1.0), vec2(1.0), vec2(0.0), vec2(1.0, 0.0))

layout(binding = 0, std140) uniform type_cbMain
{
layout(row_major) mat4 invView;
layout(row_major) mat4 viewProj;
mat4 invView;
mat4 viewProj;
} cbMain;

layout(location = 0) in vec4 in_var_POSITION[1];
Expand Down
4 changes: 2 additions & 2 deletions Source/Tests/Data/Expected/Particle_GS.410.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const vec2 _48[4] = vec2[](vec2(0.0, 1.0), vec2(1.0), vec2(0.0), vec2(1.0, 0.0))

layout(std140) uniform type_cbMain
{
layout(row_major) mat4 invView;
layout(row_major) mat4 viewProj;
mat4 invView;
mat4 viewProj;
} cbMain;

layout(location = 0) in vec4 in_var_POSITION[1];
Expand Down
2 changes: 1 addition & 1 deletion Source/Tests/Data/Expected/Transform_VS.30.hlsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cbuffer type_cbVS : register(b0)
{
column_major float4x4 cbVS_wvp : packoffset(c0);
row_major float4x4 cbVS_wvp : packoffset(c0);
};

uniform float4 gl_HalfPixel;
Expand Down
2 changes: 1 addition & 1 deletion Source/Tests/Data/Expected/Transform_VS.300.essl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

layout(std140) uniform type_cbVS
{
layout(row_major) mat4 wvp;
mat4 wvp;
} cbVS;

layout(location = 0) in vec4 in_var_POSITION;
Expand Down
2 changes: 1 addition & 1 deletion Source/Tests/Data/Expected/Transform_VS.300.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ out gl_PerVertex

layout(std140) uniform type_cbVS
{
layout(row_major) mat4 wvp;
mat4 wvp;
} cbVS;

in vec4 in_var_POSITION;
Expand Down
2 changes: 1 addition & 1 deletion Source/Tests/Data/Expected/Transform_VS.310.essl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

layout(binding = 0, std140) uniform type_cbVS
{
layout(row_major) mat4 wvp;
mat4 wvp;
} cbVS;

layout(location = 0) in vec4 in_var_POSITION;
Expand Down
2 changes: 1 addition & 1 deletion Source/Tests/Data/Expected/Transform_VS.40.hlsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cbuffer type_cbVS : register(b0)
{
column_major float4x4 cbVS_wvp : packoffset(c0);
row_major float4x4 cbVS_wvp : packoffset(c0);
};


Expand Down
2 changes: 1 addition & 1 deletion Source/Tests/Data/Expected/Transform_VS.410.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ out gl_PerVertex

layout(std140) uniform type_cbVS
{
layout(row_major) mat4 wvp;
mat4 wvp;
} cbVS;

layout(location = 0) in vec4 in_var_POSITION;
Expand Down
2 changes: 1 addition & 1 deletion Source/Tests/Data/Expected/Transform_VS.50.hlsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cbuffer type_cbVS : register(b0)
{
column_major float4x4 cbVS_wvp : packoffset(c0);
row_major float4x4 cbVS_wvp : packoffset(c0);
};


Expand Down
2 changes: 1 addition & 1 deletion Source/Tests/Data/Expected/Transform_VS.msl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct main0_in
vertex main0_out main0(main0_in in [[stage_in]], constant type_cbVS& cbVS [[buffer(0)]])
{
main0_out out = {};
out.gl_Position = in.in_var_POSITION * cbVS.wvp;
out.gl_Position = cbVS.wvp * in.in_var_POSITION;
return out;
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ out gl_PerVertex

layout(std140) uniform type_cbVS
{
mat4 wvp;
layout(row_major) mat4 wvp;
} cbVS;

in vec4 in_var_POSITION;
Expand Down
4 changes: 4 additions & 0 deletions Source/Wrapper/Native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ void Compile(SourceDescription* source, OptionsDescription* optionsDesc, TargetD
options.disableOptimizations = optionsDesc->disableOptimizations;
options.optimizationLevel = optionsDesc->optimizationLevel;
options.shaderModel = { static_cast<uint8_t>(optionsDesc->shaderModel.major), static_cast<uint8_t>(optionsDesc->shaderModel.minor) };
options.shiftAllTexturesBindings = optionsDesc->shiftAllTexturesBindings;
options.shiftAllSamplersBindings = optionsDesc->shiftAllSamplersBindings;
options.shiftAllCBuffersBindings = optionsDesc->shiftAllCBuffersBindings;
options.shiftAllUABuffersBindings = optionsDesc->shiftAllUABuffersBindings;

Compiler::TargetDesc targetDesc;
targetDesc.language = target->shadingLanguage;
Expand Down
5 changes: 5 additions & 0 deletions Source/Wrapper/Native.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ struct OptionsDescription
int optimizationLevel = 3; // 0 to 3, no optimization to most optimization

ShaderModel shaderModel = { 6, 0 };

int shiftAllTexturesBindings;
int shiftAllSamplersBindings;
int shiftAllCBuffersBindings;
int shiftAllUABuffersBindings;
};

struct TargetDescription
Expand Down
4 changes: 4 additions & 0 deletions Source/Wrapper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ static void Main(string[] args)
//optionsDesc.disableOptimizations = true;
//optionsDesc.optimizationLevel = 3;
//optionsDesc.shaderModel = new Wrapper.ShaderModel(6, 2);
//optionsDesc.shiftAllCBuffersBindings = 10;
//optionsDesc.shiftAllTexturesBindings = 20;
//optionsDesc.shiftAllSamplersBindings = 30;
//optionsDesc.shiftAllUABuffersBindings = 40;

Wrapper.TargetDesc targetDesc = new Wrapper.TargetDesc()
{
Expand Down
5 changes: 5 additions & 0 deletions Source/Wrapper/Wrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public struct OptionsDesc

public ShaderModel shaderModel;

public int shiftAllTexturesBindings;
public int shiftAllSamplersBindings;
public int shiftAllCBuffersBindings;
public int shiftAllUABuffersBindings;

public static OptionsDesc Default
{
get
Expand Down
39 changes: 24 additions & 15 deletions Source/Wrapper/shader.hlsl
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

float4x4 worldViewProj;
cbuffer Matrices : register(b0)
{
float4x4 worldViewProj;
};

Texture2D DiffuseTexture : register(t0);
SamplerState Sampler : register(s0);

struct VS_IN
{
float4 pos : POSITION;
float4 col : COLOR;
float4 pos : POSITION;
float4 col : COLOR;
float2 tex : TEXCOORD;
};

struct PS_IN
{
float4 pos : SV_POSITION;
float4 col : COLOR;
float4 pos : SV_POSITION;
float4 col : COLOR;
float2 tex : TEXCOORD;
};

PS_IN VS( VS_IN input )
PS_IN VS(VS_IN input)
{
PS_IN output = (PS_IN)0;

output.pos = mul(input.pos, worldViewProj);
output.col = input.col;

return output;
PS_IN output = (PS_IN)0;

output.pos = mul(input.pos, worldViewProj);
output.col = input.col;
output.tex = input.tex;

return output;
}

float4 PS( PS_IN input ) : SV_Target
float4 PS(PS_IN input) : SV_Target
{
return input.col;
}
return DiffuseTexture.Sample(Sampler, input.tex);
}

0 comments on commit b514328

Please sign in to comment.