From 9020c480763cff017a5a3166b34d7f89350ef5df Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Fri, 21 Apr 2017 14:05:17 +0200 Subject: [PATCH] Support and test 1D and 3D textures in HLSL4/5 --- reference/shaders-hlsl/frag/tex-sampling.frag | 44 ++++++++++++++----- shaders-hlsl/frag/tex-sampling.frag | 36 +++++++++++---- spirv_hlsl.cpp | 20 ++++++++- 3 files changed, 80 insertions(+), 20 deletions(-) diff --git a/reference/shaders-hlsl/frag/tex-sampling.frag b/reference/shaders-hlsl/frag/tex-sampling.frag index b18a6174..5976a73e 100644 --- a/reference/shaders-hlsl/frag/tex-sampling.frag +++ b/reference/shaders-hlsl/frag/tex-sampling.frag @@ -1,12 +1,20 @@ -Texture2D tex; -SamplerState _tex_sampler; +Texture1D tex1d; +SamplerState _tex1d_sampler; +Texture2D tex2d; +SamplerState _tex2d_sampler; +Texture3D tex3d; +SamplerState _tex3d_sampler; -static float2 texCoord; +static float texCoord1d; +static float2 texCoord2d; +static float3 texCoord3d; static float4 FragColor; struct SPIRV_Cross_Input { - float2 texCoord : TEXCOORD0; + float texCoord1d : TEXCOORD0; + float2 texCoord2d : TEXCOORD1; + float3 texCoord3d : TEXCOORD2; }; struct SPIRV_Cross_Output @@ -31,18 +39,32 @@ float3 SPIRV_Cross_projectTextureCoordinate(float4 coord) void frag_main() { - float4 texcolor = tex.Sample(_tex_sampler, texCoord); - texcolor += tex.Sample(_tex_sampler, texCoord, int2(1, 2)); - texcolor += tex.SampleLevel(_tex_sampler, texCoord, 2.0f); - texcolor += tex.SampleGrad(_tex_sampler, texCoord, float2(1.0f, 2.0f), float2(3.0f, 4.0f)); - texcolor += tex.Sample(_tex_sampler, SPIRV_Cross_projectTextureCoordinate(float3(texCoord, 2.0f))); - texcolor += tex.SampleBias(_tex_sampler, texCoord, 1.0f); + float4 texcolor = tex1d.Sample(_tex1d_sampler, texCoord1d); + texcolor += tex1d.Sample(_tex1d_sampler, texCoord1d, 1); + texcolor += tex1d.SampleLevel(_tex1d_sampler, texCoord1d, 2.0f); + texcolor += tex1d.SampleGrad(_tex1d_sampler, texCoord1d, 1.0f, 2.0f); + texcolor += tex1d.Sample(_tex1d_sampler, SPIRV_Cross_projectTextureCoordinate(float2(texCoord1d, 2.0f))); + texcolor += tex1d.SampleBias(_tex1d_sampler, texCoord1d, 1.0f); + texcolor += tex2d.Sample(_tex2d_sampler, texCoord2d); + texcolor += tex2d.Sample(_tex2d_sampler, texCoord2d, int2(1, 2)); + texcolor += tex2d.SampleLevel(_tex2d_sampler, texCoord2d, 2.0f); + texcolor += tex2d.SampleGrad(_tex2d_sampler, texCoord2d, float2(1.0f, 2.0f), float2(3.0f, 4.0f)); + texcolor += tex2d.Sample(_tex2d_sampler, SPIRV_Cross_projectTextureCoordinate(float3(texCoord2d, 2.0f))); + texcolor += tex2d.SampleBias(_tex2d_sampler, texCoord2d, 1.0f); + texcolor += tex3d.Sample(_tex3d_sampler, texCoord3d); + texcolor += tex3d.Sample(_tex3d_sampler, texCoord3d, int3(1, 2, 3)); + texcolor += tex3d.SampleLevel(_tex3d_sampler, texCoord3d, 2.0f); + texcolor += tex3d.SampleGrad(_tex3d_sampler, texCoord3d, float3(1.0f, 2.0f, 3.0f), float3(4.0f, 5.0f, 6.0f)); + texcolor += tex3d.Sample(_tex3d_sampler, SPIRV_Cross_projectTextureCoordinate(float4(texCoord3d, 2.0f))); + texcolor += tex3d.SampleBias(_tex3d_sampler, texCoord3d, 1.0f); FragColor = texcolor; } SPIRV_Cross_Output main(SPIRV_Cross_Input stage_input) { - texCoord = stage_input.texCoord; + texCoord1d = stage_input.texCoord1d; + texCoord2d = stage_input.texCoord2d; + texCoord3d = stage_input.texCoord3d; frag_main(); SPIRV_Cross_Output stage_output; stage_output.FragColor = FragColor; diff --git a/shaders-hlsl/frag/tex-sampling.frag b/shaders-hlsl/frag/tex-sampling.frag index 0dc598d4..b9718e60 100644 --- a/shaders-hlsl/frag/tex-sampling.frag +++ b/shaders-hlsl/frag/tex-sampling.frag @@ -1,16 +1,36 @@ #version 450 -uniform sampler2D tex; -in vec2 texCoord; +uniform sampler1D tex1d; +uniform sampler2D tex2d; +uniform sampler3D tex3d; + +in float texCoord1d; +in vec2 texCoord2d; +in vec3 texCoord3d; out vec4 FragColor; void main() { - vec4 texcolor = texture(tex, texCoord); - texcolor += textureOffset(tex, texCoord, ivec2(1, 2)); - texcolor += textureLod(tex, texCoord, 2); - texcolor += textureGrad(tex, texCoord, vec2(1.0, 2.0), vec2(3.0, 4.0)); - texcolor += textureProj(tex, vec3(texCoord, 2.0)); - texcolor += texture(tex, texCoord, 1.0); + vec4 texcolor = texture(tex1d, texCoord1d); + texcolor += textureOffset(tex1d, texCoord1d, 1); + texcolor += textureLod(tex1d, texCoord1d, 2); + texcolor += textureGrad(tex1d, texCoord1d, 1.0, 2.0); + texcolor += textureProj(tex1d, vec2(texCoord1d, 2.0)); + texcolor += texture(tex1d, texCoord1d, 1.0); + + texcolor += texture(tex2d, texCoord2d); + texcolor += textureOffset(tex2d, texCoord2d, ivec2(1, 2)); + texcolor += textureLod(tex2d, texCoord2d, 2); + texcolor += textureGrad(tex2d, texCoord2d, vec2(1.0, 2.0), vec2(3.0, 4.0)); + texcolor += textureProj(tex2d, vec3(texCoord2d, 2.0)); + texcolor += texture(tex2d, texCoord2d, 1.0); + + texcolor += texture(tex3d, texCoord3d); + texcolor += textureOffset(tex3d, texCoord3d, ivec3(1, 2, 3)); + texcolor += textureLod(tex3d, texCoord3d, 2); + texcolor += textureGrad(tex3d, texCoord3d, vec3(1.0, 2.0, 3.0), vec3(4.0, 5.0, 6.0)); + texcolor += textureProj(tex3d, vec4(texCoord3d, 2.0)); + texcolor += texture(tex3d, texCoord3d, 1.0); + FragColor = texcolor; } diff --git a/spirv_hlsl.cpp b/spirv_hlsl.cpp index b3fa5954..a8c75bd5 100644 --- a/spirv_hlsl.cpp +++ b/spirv_hlsl.cpp @@ -1337,7 +1337,25 @@ void CompilerHLSL::emit_uniform(const SPIRVariable &var) if (options.shader_model >= 40 && type.basetype == SPIRType::SampledImage) { auto &imagetype = get(type.image.type); - statement("Texture2D<", type_to_glsl(imagetype), "4> ", to_name(var.self), ";"); + string dim; + switch (type.image.dim) + { + case Dim1D: + dim = "1D"; + break; + case Dim2D: + dim = "2D"; + break; + case Dim3D: + dim = "3D"; + break; + case DimCube: + case DimRect: + case DimBuffer: + case DimSubpassData: + SPIRV_CROSS_THROW("Cube/Buffer texture support is not yet implemented for HLSL"); // TODO + } + statement("Texture", dim, "<", type_to_glsl(imagetype), "4> ", to_name(var.self), ";"); statement("SamplerState _", to_name(var.self), "_sampler;"); } else