Skip to content

Commit

Permalink
Added [u/i]sampler3D.
Browse files Browse the repository at this point in the history
  • Loading branch information
bkaradzic committed Mar 18, 2015
1 parent bbf2fa6 commit b2da0cc
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 33 deletions.
37 changes: 37 additions & 0 deletions src/bgfx_shader.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ struct BgfxSampler3D
Texture3D m_texture;
};

struct BgfxISampler3D
{
Texture3D<ivec4> m_texture;
};

struct BgfxUSampler3D
{
Texture3D<uvec4> m_texture;
};

vec4 bgfxTexture3D(BgfxSampler3D _sampler, vec3 _coord)
{
return _sampler.m_texture.Sample(_sampler.m_sampler, _coord);
Expand All @@ -84,6 +94,20 @@ vec4 bgfxTexture3DLod(BgfxSampler3D _sampler, vec3 _coord, float _level)
return _sampler.m_texture.SampleLevel(_sampler.m_sampler, _coord, _level);
}

ivec4 bgfxTexture3D(BgfxISampler3D _sampler, vec3 _coord)
{
ivec3 size;
_sampler.m_texture.GetDimensions(size.x, size.y, size.z);
return _sampler.m_texture.Load(ivec4(_coord * size, 0) );
}

uvec4 bgfxTexture3D(BgfxUSampler3D _sampler, vec3 _coord)
{
uvec3 size;
_sampler.m_texture.GetDimensions(size.x, size.y, size.z);
return _sampler.m_texture.Load(uvec4(_coord * size, 0) );
}

struct BgfxSamplerCube
{
SamplerState m_sampler;
Expand Down Expand Up @@ -121,6 +145,12 @@ vec4 bgfxTextureCubeLod(BgfxSamplerCube _sampler, vec3 _coord, float _level)
uniform SamplerState _name ## Sampler : register(s[_reg]); \
uniform Texture3D _name ## Texture : register(t[_reg]); \
static BgfxSampler3D _name = { _name ## Sampler, _name ## Texture }
# define ISAMPLER3D(_name, _reg) \
uniform Texture3D<ivec4> _name ## Texture : register(t[_reg]); \
static BgfxISampler3D _name = { _name ## Texture }
# define USAMPLER3D(_name, _reg) \
uniform Texture3D<uvec4> _name ## Texture : register(t[_reg]); \
static BgfxUSampler3D _name = { _name ## Texture }
# define sampler3D BgfxSampler3D
# define texture3D(_sampler, _coord) bgfxTexture3D(_sampler, _coord)
# define texture3DLod(_sampler, _coord, _level) bgfxTexture3DLod(_sampler, _coord, _level)
Expand Down Expand Up @@ -247,6 +277,13 @@ vec4 mod(vec4 _a, vec4 _b) { return _a - _b * floor(_a / _b); }
# define uvec3_splat(_x) uvec3(_x)
# define uvec4_splat(_x) uvec4(_x)

# if BGFX_SHADER_LANGUAGE_GLSL >= 130
# define ISAMPLER3D(_name, _reg) uniform isampler3D _name
# define USAMPLER3D(_name, _reg) uniform usampler3D _name
ivec4 texture3D(isampler3D _sampler, vec3 _coord) { return texture(_sampler, _coord); }
uvec4 texture3D(usampler3D _sampler, vec3 _coord) { return texture(_sampler, _coord); }
# endif // BGFX_SHADER_LANGUAGE_GLSL >= 130

vec3 instMul(vec3 _vec, mat3 _mtx) { return mul(_vec, _mtx); }
vec3 instMul(mat3 _mtx, vec3 _vec) { return mul(_mtx, _vec); }
vec4 instMul(vec4 _vec, mat4 _mtx) { return mul(_vec, _mtx); }
Expand Down
2 changes: 1 addition & 1 deletion src/renderer_d3d11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ namespace bgfx
{ DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN }, // Unknown
{ DXGI_FORMAT_R1_UNORM, DXGI_FORMAT_R1_UNORM, DXGI_FORMAT_UNKNOWN }, // R1
{ DXGI_FORMAT_R8_UNORM, DXGI_FORMAT_R8_UNORM, DXGI_FORMAT_UNKNOWN }, // R8
{ DXGI_FORMAT_R16_UNORM, DXGI_FORMAT_R16_UNORM, DXGI_FORMAT_UNKNOWN }, // R16
{ DXGI_FORMAT_R16_UINT, DXGI_FORMAT_R16_UINT, DXGI_FORMAT_UNKNOWN }, // R16
{ DXGI_FORMAT_R16_FLOAT, DXGI_FORMAT_R16_FLOAT, DXGI_FORMAT_UNKNOWN }, // R16F
{ DXGI_FORMAT_R32_UINT, DXGI_FORMAT_R32_UINT, DXGI_FORMAT_UNKNOWN }, // R32
{ DXGI_FORMAT_R32_FLOAT, DXGI_FORMAT_R32_FLOAT, DXGI_FORMAT_UNKNOWN }, // R32F
Expand Down
66 changes: 34 additions & 32 deletions tools/shaderc/shaderc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,8 @@ int main(int _argc, const char* _argv[])

bool raw = cmdLine.hasArg('\0', "raw");

uint32_t gles = 0;
uint32_t glsl = 0;
uint32_t essl = 0;
uint32_t hlsl = 2;
uint32_t d3d = 11;
const char* profile = cmdLine.findOption('p', "profile");
Expand All @@ -774,10 +775,14 @@ int main(int _argc, const char* _argv[])
{
hlsl = 5;
}
else
{
glsl = atoi(profile);
}
}
else
{
gles = 2;
essl = 2;
}

const char* bin2c = NULL;
Expand Down Expand Up @@ -811,7 +816,7 @@ int main(int _argc, const char* _argv[])
bool preprocessOnly = cmdLine.hasArg("preprocess");
const char* includeDir = cmdLine.findOption('i');

Preprocessor preprocessor(filePath, 0 != gles, includeDir);
Preprocessor preprocessor(filePath, 0 != essl, includeDir);

std::string dir;
{
Expand Down Expand Up @@ -839,43 +844,38 @@ int main(int _argc, const char* _argv[])
preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_FRAGMENT");
preprocessor.setDefaultDefine("BGFX_SHADER_TYPE_VERTEX");

bool glsl = false;
char glslDefine[128];
bx::snprintf(glslDefine, BX_COUNTOF(glslDefine), "BGFX_SHADER_LANGUAGE_GLSL=%d", glsl);

if (0 == bx::stricmp(platform, "android") )
{
preprocessor.setDefine("BX_PLATFORM_ANDROID=1");
preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
glsl = true;
}
else if (0 == bx::stricmp(platform, "asm.js") )
{
preprocessor.setDefine("BX_PLATFORM_EMSCRIPTEN=1");
preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
glsl = true;
}
else if (0 == bx::stricmp(platform, "ios") )
{
preprocessor.setDefine("BX_PLATFORM_IOS=1");
preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
glsl = true;
}
else if (0 == bx::stricmp(platform, "linux") )
{
preprocessor.setDefine("BX_PLATFORM_LINUX=1");
preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
glsl = true;
preprocessor.setDefine(glslDefine);
}
else if (0 == bx::stricmp(platform, "nacl") )
{
preprocessor.setDefine("BX_PLATFORM_NACL=1");
preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
glsl = true;
}
else if (0 == bx::stricmp(platform, "osx") )
{
preprocessor.setDefine("BX_PLATFORM_OSX=1");
preprocessor.setDefine("BGFX_SHADER_LANGUAGE_GLSL=1");
glsl = true;
preprocessor.setDefine(glslDefine);
}
else if (0 == bx::stricmp(platform, "windows") )
{
Expand Down Expand Up @@ -1120,7 +1120,7 @@ int main(int _argc, const char* _argv[])
bx::write(writer, outputHash);
}

if (glsl)
if (0 != glsl)
{
bx::write(writer, uint16_t(0) );

Expand Down Expand Up @@ -1155,7 +1155,7 @@ int main(int _argc, const char* _argv[])
}
else
{
if (glsl)
if (0 != glsl)
{
}
else
Expand Down Expand Up @@ -1269,18 +1269,17 @@ int main(int _argc, const char* _argv[])
bx::write(writer, BGFX_CHUNK_MAGIC_CSH);
bx::write(writer, outputHash);

if (glsl)
if (0 != glsl)
{
std::string code;

if (gles)
if (essl)
{
bx::stringPrintf(code, "#version 310 es\n");
}
else
{
int32_t version = atoi(profile);
bx::stringPrintf(code, "#version %d\n", version == 0 ? 430 : version);
bx::stringPrintf(code, "#version %d\n", glsl == 0 ? 430 : glsl);
}

code += preprocessor.m_preprocessed;
Expand All @@ -1294,7 +1293,7 @@ int main(int _argc, const char* _argv[])

compiled = true;
#else
compiled = compileGLSLShader(cmdLine, gles, code, writer);
compiled = compileGLSLShader(cmdLine, essl, code, writer);
#endif // 0
}
else
Expand Down Expand Up @@ -1339,15 +1338,19 @@ int main(int _argc, const char* _argv[])
}
else
{
if (glsl)
if (0 != glsl)
{
preprocessor.writef(
"#define ivec2 vec2\n"
"#define ivec3 vec3\n"
"#define ivec4 vec4\n"
);
if (120 == glsl
|| essl)
{
preprocessor.writef(
"#define ivec2 vec2\n"
"#define ivec3 vec3\n"
"#define ivec4 vec4\n"
);
}

if (0 == gles)
if (0 == essl)
{
// bgfx shadow2D/Proj behave like EXT_shadow_samplers
// not as GLSL language 1.2 specs shadow2D/Proj.
Expand Down Expand Up @@ -1645,7 +1648,7 @@ int main(int _argc, const char* _argv[])
return EXIT_FAILURE;
}

if (glsl)
if (0 != glsl)
{
const char* profile = cmdLine.findOption('p', "profile");
if (NULL == profile)
Expand Down Expand Up @@ -1697,24 +1700,23 @@ int main(int _argc, const char* _argv[])
bx::write(writer, outputHash);
}

if (glsl)
if (0 != glsl)
{
std::string code;

bool hasTextureLod = NULL != bx::findIdentifierMatch(input, s_ARB_shader_texture_lod /*EXT_shader_texture_lod*/);

if (0 == gles)
if (0 == essl)
{
bx::stringPrintf(code, "#version %s\n", profile);
int32_t version = atoi(profile);

bx::stringPrintf(code
, "#define bgfxShadow2D shadow2D\n"
"#define bgfxShadow2DProj shadow2DProj\n"
);

if (hasTextureLod
&& 130 > version)
&& 130 > glsl)
{
bx::stringPrintf(code
, "#extension GL_ARB_shader_texture_lod : enable\n"
Expand Down Expand Up @@ -1767,7 +1769,7 @@ int main(int _argc, const char* _argv[])
}

code += preprocessor.m_preprocessed;
compiled = compileGLSLShader(cmdLine, gles, code, writer);
compiled = compileGLSLShader(cmdLine, essl, code, writer);
}
else
{
Expand Down

0 comments on commit b2da0cc

Please sign in to comment.