├── examples └── 01-cubes-brtshaderc │ ├── varying.def.sc │ ├── common │ ├── common.sh │ ├── shaderlib.sh │ └── bgfx_shader.sh │ ├── fs_cubes.sc │ ├── makefile │ ├── vs_cubes.sc │ └── cubes.cpp ├── scripts ├── brtshaderc-example.lua ├── genie.lua └── brtshaderc.lua ├── tools └── brtshaderc │ ├── brtshaderc.h │ └── brtshaderc.cpp └── README.md /examples/01-cubes-brtshaderc/varying.def.sc: -------------------------------------------------------------------------------- 1 | vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); 2 | 3 | vec3 a_position : POSITION; 4 | vec4 a_color0 : COLOR0; 5 | -------------------------------------------------------------------------------- /examples/01-cubes-brtshaderc/common/common.sh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011-2018 Branimir Karadzic. All rights reserved. 3 | * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause 4 | */ 5 | 6 | #include "bgfx_shader.sh" 7 | #include "shaderlib.sh" 8 | -------------------------------------------------------------------------------- /examples/01-cubes-brtshaderc/fs_cubes.sc: -------------------------------------------------------------------------------- 1 | $input v_color0 2 | 3 | /* 4 | * Copyright 2011-2018 Branimir Karadzic. All rights reserved. 5 | * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause 6 | */ 7 | 8 | #include "common/common.sh" 9 | 10 | void main() 11 | { 12 | gl_FragColor = v_color0; 13 | } 14 | -------------------------------------------------------------------------------- /examples/01-cubes-brtshaderc/makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2011-2018 Branimir Karadzic. All rights reserved. 3 | # License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause 4 | # 5 | 6 | BGFX_DIR=../.. 7 | RUNTIME_DIR=$(BGFX_DIR)/examples/runtime 8 | BUILD_DIR=../../.build 9 | 10 | include $(BGFX_DIR)/scripts/shader.mk 11 | -------------------------------------------------------------------------------- /examples/01-cubes-brtshaderc/vs_cubes.sc: -------------------------------------------------------------------------------- 1 | $input a_position, a_color0 2 | $output v_color0 3 | 4 | /* 5 | * Copyright 2011-2018 Branimir Karadzic. All rights reserved. 6 | * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause 7 | */ 8 | 9 | #include "common/common.sh" 10 | 11 | void main() 12 | { 13 | gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) ); 14 | v_color0 = a_color0; 15 | } 16 | -------------------------------------------------------------------------------- /scripts/brtshaderc-example.lua: -------------------------------------------------------------------------------- 1 | if _OPTIONS["with-examples"] 2 | or _OPTIONS["with-combined-examples"] then 3 | group "examples" 4 | 5 | exampleProject(_OPTIONS["with-combined-examples"], "01-cubes-brtshaderc") 6 | 7 | includedirs { 8 | path.join(BGFX_DIR, "tools/brtshaderc") 9 | } 10 | 11 | links { 12 | "brtshaderc" 13 | } 14 | 15 | files { 16 | path.join(BGFX_DIR, "examples/01-cubes-brtshaderc/cubes.cpp") 17 | } 18 | end 19 | 20 | -------------------------------------------------------------------------------- /tools/brtshaderc/brtshaderc.h: -------------------------------------------------------------------------------- 1 | namespace shaderc 2 | { 3 | enum ShaderType 4 | { 5 | ST_VERTEX = 'v', /// vertex 6 | ST_FRAGMENT = 'f', /// fragment 7 | ST_COMPUTE = 'c', /// compute 8 | }; 9 | 10 | /** 11 | * Compile a shader from source file and return memory pointer that contains the compiled shader. 12 | * 13 | * @param type : Shader type to comile (vertex, fragment or compute) 14 | * @param filePath : Shader source file path. 15 | * @param defines : List of defines semicolon separated ex: "foo=1;bar;baz=1". 16 | * @param varyingPath : File path for varying.def.sc, or assume default name is "varying.def.sc" in current dir. 17 | * @param profile : shader profile ("ps_4_0", "vs_4_0", ...). If null, library try to set default profile for current context. 18 | * @return a memory block of compiled shader ready to use with bgfx::createShader, or null if failed. 19 | */ 20 | const bgfx::Memory* compileShader( 21 | ShaderType type 22 | , const char* filePath 23 | , const char* defines = nullptr 24 | , const char* varyingPath = nullptr 25 | , const char* profile = nullptr 26 | ); 27 | 28 | 29 | /** 30 | * Compile a shader from arguments list (same as shaderc binary version). 31 | * 32 | * @param argc : Arguments count. 33 | * @param argv : Arguments list. 34 | * @return a memory block of compiled shader ready to use with bgfx::createShader, or null if failed. 35 | */ 36 | const bgfx::Memory* compileShader(int argc, const char* argv[]); 37 | 38 | } 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # brtshaderc 2 | Hack of the bgfx shaderc tools in library version to compile bgfx shaders source at runtime. 3 | Inspired from Lumix engine code. 4 | 5 | - It use the original shaderc code without doing any modification, it's just a wrapper (easy to keep updated with bgfx). 6 | - Compiled shaders are built at application runtime in memory without any write file access to disk. 7 | 8 | ## Compilation 9 | 10 | Drop content in bgfx folder and build bgfx as usual. 11 | This will build 'brtshaderc' a library version of shaderc than can be linked to your project. 12 | 13 | ## Example 14 | 15 | ```cpp 16 | #include 17 | ... 18 | 19 | // compile vertex shader, with default arguments. 20 | const bgfx::Memory* memVsh = shaderc::compileShader(shaderc::ST_VERTEX, "vs_cubes.sc"); 21 | bgfx::ShaderHandle vsh = bgfx::createShader(memVsh); 22 | 23 | // compile fragment shader, with specific arguments for defines, varying def file, shader profile. 24 | const bgfx::Memory* memFsh = shaderc::compileShader(shaderc::ST_FRAGMENT, "fs_cubes.sc", "myDefines", "varying.def.sc", "ps_5_0"); 25 | bgfx::ShaderHandle fsh = bgfx::createShader(memFsh); 26 | 27 | // build program using shaders 28 | mProgram = bgfx::createProgram(vsh, fsh, true); 29 | ``` 30 | 31 | or using arguments list syntax style (same as binary version) : 32 | 33 | ``` 34 | int argc = 0; 35 | const char* argv[16]; 36 | argv[argc++] = "-f"; 37 | argv[argc++] = "vs_cubes.sc"; 38 | argv[argc++] = "--varyingdef"; 39 | argv[argc++] = "varying.def.sc"; 40 | argv[argc++] = "--type"; 41 | argv[argc++] = "v"; 42 | argv[argc++] = "--platform"; 43 | argv[argc++] = "windows"; 44 | argv[argc++] = "--profile"; 45 | argv[argc++] = "ps_4_0"; 46 | 47 | const bgfx::Memory* memVsh = shaderc::compileShader(argc, argv); 48 | bgfx::ShaderHandle vsh = bgfx::createShader(memVsh); 49 | ``` -------------------------------------------------------------------------------- /examples/01-cubes-brtshaderc/cubes.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011-2018 Branimir Karadzic. All rights reserved. 3 | * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause 4 | */ 5 | 6 | #include "common.h" 7 | #include "bgfx_utils.h" 8 | #include "imgui/imgui.h" 9 | #include 10 | 11 | namespace 12 | { 13 | 14 | struct PosColorVertex 15 | { 16 | float m_x; 17 | float m_y; 18 | float m_z; 19 | uint32_t m_abgr; 20 | 21 | static void init() 22 | { 23 | ms_decl 24 | .begin() 25 | .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float) 26 | .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true) 27 | .end(); 28 | }; 29 | 30 | static bgfx::VertexDecl ms_decl; 31 | }; 32 | 33 | bgfx::VertexDecl PosColorVertex::ms_decl; 34 | 35 | static PosColorVertex s_cubeVertices[] = 36 | { 37 | {-1.0f, 1.0f, 1.0f, 0xff000000 }, 38 | { 1.0f, 1.0f, 1.0f, 0xff0000ff }, 39 | {-1.0f, -1.0f, 1.0f, 0xff00ff00 }, 40 | { 1.0f, -1.0f, 1.0f, 0xff00ffff }, 41 | {-1.0f, 1.0f, -1.0f, 0xffff0000 }, 42 | { 1.0f, 1.0f, -1.0f, 0xffff00ff }, 43 | {-1.0f, -1.0f, -1.0f, 0xffffff00 }, 44 | { 1.0f, -1.0f, -1.0f, 0xffffffff }, 45 | }; 46 | 47 | static const uint16_t s_cubeTriList[] = 48 | { 49 | 0, 1, 2, // 0 50 | 1, 3, 2, 51 | 4, 6, 5, // 2 52 | 5, 6, 7, 53 | 0, 2, 4, // 4 54 | 4, 2, 6, 55 | 1, 5, 3, // 6 56 | 5, 7, 3, 57 | 0, 4, 1, // 8 58 | 4, 5, 1, 59 | 2, 3, 6, // 10 60 | 6, 3, 7, 61 | }; 62 | 63 | static const uint16_t s_cubeTriStrip[] = 64 | { 65 | 0, 1, 2, 66 | 3, 67 | 7, 68 | 1, 69 | 5, 70 | 0, 71 | 4, 72 | 2, 73 | 6, 74 | 7, 75 | 4, 76 | 5, 77 | }; 78 | 79 | static const uint16_t s_cubeLineList[] = 80 | { 81 | 0, 1, 82 | 0, 2, 83 | 0, 4, 84 | 1, 3, 85 | 1, 5, 86 | 2, 3, 87 | 2, 6, 88 | 3, 7, 89 | 4, 5, 90 | 4, 6, 91 | 5, 7, 92 | 6, 7, 93 | }; 94 | 95 | static const uint16_t s_cubeLineStrip[] = 96 | { 97 | 0, 2, 3, 1, 5, 7, 6, 4, 98 | 0, 2, 6, 4, 5, 7, 3, 1, 99 | 0, 100 | }; 101 | 102 | static const uint16_t s_cubePoints[] = 103 | { 104 | 0, 1, 2, 3, 4, 5, 6, 7 105 | }; 106 | 107 | static const char* s_ptNames[] 108 | { 109 | "Triangle List", 110 | "Triangle Strip", 111 | "Lines", 112 | "Line Strip", 113 | "Points", 114 | }; 115 | 116 | static const uint64_t s_ptState[] 117 | { 118 | UINT64_C(0), 119 | BGFX_STATE_PT_TRISTRIP, 120 | BGFX_STATE_PT_LINES, 121 | BGFX_STATE_PT_LINESTRIP, 122 | BGFX_STATE_PT_POINTS, 123 | }; 124 | BX_STATIC_ASSERT(BX_COUNTOF(s_ptState) == BX_COUNTOF(s_ptNames) ); 125 | 126 | class ExampleCubes : public entry::AppI 127 | { 128 | public: 129 | ExampleCubes(const char* _name, const char* _description) 130 | : entry::AppI(_name, _description) 131 | , m_pt(0) 132 | , m_r(true) 133 | , m_g(true) 134 | , m_b(true) 135 | , m_a(true) 136 | { 137 | } 138 | 139 | void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override 140 | { 141 | Args args(_argc, _argv); 142 | 143 | m_width = _width; 144 | m_height = _height; 145 | m_debug = BGFX_DEBUG_NONE; 146 | m_reset = BGFX_RESET_VSYNC; 147 | 148 | bgfx::Init init; 149 | init.type = args.m_type; 150 | init.vendorId = args.m_pciId; 151 | init.resolution.width = m_width; 152 | init.resolution.height = m_height; 153 | init.resolution.reset = m_reset; 154 | bgfx::init(init); 155 | 156 | // Enable debug text. 157 | bgfx::setDebug(m_debug); 158 | 159 | // Set view 0 clear state. 160 | bgfx::setViewClear(0 161 | , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH 162 | , 0x303030ff 163 | , 1.0f 164 | , 0 165 | ); 166 | 167 | // Create vertex stream declaration. 168 | PosColorVertex::init(); 169 | 170 | // Create static vertex buffer. 171 | m_vbh = bgfx::createVertexBuffer( 172 | // Static data can be passed with bgfx::makeRef 173 | bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) ) 174 | , PosColorVertex::ms_decl 175 | ); 176 | 177 | // Create static index buffer for triangle list rendering. 178 | m_ibh[0] = bgfx::createIndexBuffer( 179 | // Static data can be passed with bgfx::makeRef 180 | bgfx::makeRef(s_cubeTriList, sizeof(s_cubeTriList) ) 181 | ); 182 | 183 | // Create static index buffer for triangle strip rendering. 184 | m_ibh[1] = bgfx::createIndexBuffer( 185 | // Static data can be passed with bgfx::makeRef 186 | bgfx::makeRef(s_cubeTriStrip, sizeof(s_cubeTriStrip) ) 187 | ); 188 | 189 | // Create static index buffer for line list rendering. 190 | m_ibh[2] = bgfx::createIndexBuffer( 191 | // Static data can be passed with bgfx::makeRef 192 | bgfx::makeRef(s_cubeLineList, sizeof(s_cubeLineList) ) 193 | ); 194 | 195 | // Create static index buffer for line strip rendering. 196 | m_ibh[3] = bgfx::createIndexBuffer( 197 | // Static data can be passed with bgfx::makeRef 198 | bgfx::makeRef(s_cubeLineStrip, sizeof(s_cubeLineStrip) ) 199 | ); 200 | 201 | // Create static index buffer for point list rendering. 202 | m_ibh[4] = bgfx::createIndexBuffer( 203 | // Static data can be passed with bgfx::makeRef 204 | bgfx::makeRef(s_cubePoints, sizeof(s_cubePoints) ) 205 | ); 206 | 207 | // Create program from shaders. 208 | //m_program = loadProgram("vs_cubes", "fs_cubes"); 209 | 210 | //----------------------------------------------------- 211 | // compile shader with brtshaderc 212 | //----------------------------------------------------- 213 | 214 | #define vs_src "../01-cubes-brtshaderc/vs_cubes.sc" 215 | #define fs_src "../01-cubes-brtshaderc/fs_cubes.sc" 216 | #define def_src "../01-cubes-brtshaderc/varying.def.sc" 217 | 218 | // compile vertex shader 219 | const bgfx::Memory* memVsh = shaderc::compileShader(shaderc::ST_VERTEX, vs_src, "", def_src); 220 | bgfx::ShaderHandle vsh = bgfx::createShader(memVsh); 221 | 222 | // compile fragment shader 223 | const bgfx::Memory* memFsh = shaderc::compileShader(shaderc::ST_FRAGMENT, fs_src, "", def_src); 224 | bgfx::ShaderHandle fsh = bgfx::createShader(memFsh); 225 | 226 | // build program using shaders 227 | m_program = bgfx::createProgram(vsh, fsh, true); 228 | 229 | 230 | 231 | 232 | 233 | 234 | m_timeOffset = bx::getHPCounter(); 235 | 236 | imguiCreate(); 237 | } 238 | 239 | virtual int shutdown() override 240 | { 241 | imguiDestroy(); 242 | 243 | // Cleanup. 244 | for (uint32_t ii = 0; ii < BX_COUNTOF(m_ibh); ++ii) 245 | { 246 | bgfx::destroy(m_ibh[ii]); 247 | } 248 | 249 | bgfx::destroy(m_vbh); 250 | bgfx::destroy(m_program); 251 | 252 | // Shutdown bgfx. 253 | bgfx::shutdown(); 254 | 255 | return 0; 256 | } 257 | 258 | bool update() override 259 | { 260 | if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) ) 261 | { 262 | imguiBeginFrame(m_mouseState.m_mx 263 | , m_mouseState.m_my 264 | , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0) 265 | | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0) 266 | | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0) 267 | , m_mouseState.m_mz 268 | , uint16_t(m_width) 269 | , uint16_t(m_height) 270 | ); 271 | 272 | showExampleDialog(this); 273 | 274 | ImGui::SetNextWindowPos( 275 | ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f) 276 | , ImGuiCond_FirstUseEver 277 | ); 278 | ImGui::SetNextWindowSize( 279 | ImVec2(m_width / 5.0f, m_height / 3.5f) 280 | , ImGuiCond_FirstUseEver 281 | ); 282 | ImGui::Begin("Settings" 283 | , NULL 284 | , 0 285 | ); 286 | 287 | ImGui::Checkbox("Write R", &m_r); 288 | ImGui::Checkbox("Write G", &m_g); 289 | ImGui::Checkbox("Write B", &m_b); 290 | ImGui::Checkbox("Write A", &m_a); 291 | 292 | ImGui::Text("Primitive topology:"); 293 | ImGui::Combo("", (int*)&m_pt, s_ptNames, BX_COUNTOF(s_ptNames) ); 294 | 295 | ImGui::End(); 296 | 297 | imguiEndFrame(); 298 | 299 | float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) ); 300 | 301 | float at[3] = { 0.0f, 0.0f, 0.0f }; 302 | float eye[3] = { 0.0f, 0.0f, -35.0f }; 303 | 304 | // Set view and projection matrix for view 0. 305 | { 306 | float view[16]; 307 | bx::mtxLookAt(view, eye, at); 308 | 309 | float proj[16]; 310 | bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth); 311 | bgfx::setViewTransform(0, view, proj); 312 | 313 | // Set view 0 default viewport. 314 | bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) ); 315 | } 316 | 317 | // This dummy draw call is here to make sure that view 0 is cleared 318 | // if no other draw calls are submitted to view 0. 319 | bgfx::touch(0); 320 | 321 | bgfx::IndexBufferHandle ibh = m_ibh[m_pt]; 322 | uint64_t state = 0 323 | | (m_r ? BGFX_STATE_WRITE_R : 0) 324 | | (m_g ? BGFX_STATE_WRITE_G : 0) 325 | | (m_b ? BGFX_STATE_WRITE_B : 0) 326 | | (m_a ? BGFX_STATE_WRITE_A : 0) 327 | | BGFX_STATE_WRITE_Z 328 | | BGFX_STATE_DEPTH_TEST_LESS 329 | | BGFX_STATE_CULL_CW 330 | | BGFX_STATE_MSAA 331 | | s_ptState[m_pt] 332 | ; 333 | 334 | // Submit 11x11 cubes. 335 | for (uint32_t yy = 0; yy < 11; ++yy) 336 | { 337 | for (uint32_t xx = 0; xx < 11; ++xx) 338 | { 339 | float mtx[16]; 340 | bx::mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f); 341 | mtx[12] = -15.0f + float(xx)*3.0f; 342 | mtx[13] = -15.0f + float(yy)*3.0f; 343 | mtx[14] = 0.0f; 344 | 345 | // Set model matrix for rendering. 346 | bgfx::setTransform(mtx); 347 | 348 | // Set vertex and index buffer. 349 | bgfx::setVertexBuffer(0, m_vbh); 350 | bgfx::setIndexBuffer(ibh); 351 | 352 | // Set render states. 353 | bgfx::setState(state); 354 | 355 | // Submit primitive for rendering to view 0. 356 | bgfx::submit(0, m_program); 357 | } 358 | } 359 | 360 | // Advance to next frame. Rendering thread will be kicked to 361 | // process submitted rendering primitives. 362 | bgfx::frame(); 363 | 364 | return true; 365 | } 366 | 367 | return false; 368 | } 369 | 370 | entry::MouseState m_mouseState; 371 | 372 | uint32_t m_width; 373 | uint32_t m_height; 374 | uint32_t m_debug; 375 | uint32_t m_reset; 376 | bgfx::VertexBufferHandle m_vbh; 377 | bgfx::IndexBufferHandle m_ibh[BX_COUNTOF(s_ptState)]; 378 | bgfx::ProgramHandle m_program; 379 | int64_t m_timeOffset; 380 | int32_t m_pt; 381 | 382 | bool m_r; 383 | bool m_g; 384 | bool m_b; 385 | bool m_a; 386 | }; 387 | 388 | } // namespace 389 | 390 | ENTRY_IMPLEMENT_MAIN(ExampleCubes, "01-cubes", "Rendering simple static mesh."); 391 | -------------------------------------------------------------------------------- /examples/01-cubes-brtshaderc/common/shaderlib.sh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011-2018 Branimir Karadzic. All rights reserved. 3 | * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause 4 | */ 5 | 6 | #ifndef __SHADERLIB_SH__ 7 | #define __SHADERLIB_SH__ 8 | 9 | vec4 encodeRE8(float _r) 10 | { 11 | float exponent = ceil(log2(_r) ); 12 | return vec4(_r / exp2(exponent) 13 | , 0.0 14 | , 0.0 15 | , (exponent + 128.0) / 255.0 16 | ); 17 | } 18 | 19 | float decodeRE8(vec4 _re8) 20 | { 21 | float exponent = _re8.w * 255.0 - 128.0; 22 | return _re8.x * exp2(exponent); 23 | } 24 | 25 | vec4 encodeRGBE8(vec3 _rgb) 26 | { 27 | vec4 rgbe8; 28 | float maxComponent = max(max(_rgb.x, _rgb.y), _rgb.z); 29 | float exponent = ceil(log2(maxComponent) ); 30 | rgbe8.xyz = _rgb / exp2(exponent); 31 | rgbe8.w = (exponent + 128.0) / 255.0; 32 | return rgbe8; 33 | } 34 | 35 | vec3 decodeRGBE8(vec4 _rgbe8) 36 | { 37 | float exponent = _rgbe8.w * 255.0 - 128.0; 38 | vec3 rgb = _rgbe8.xyz * exp2(exponent); 39 | return rgb; 40 | } 41 | 42 | vec3 encodeNormalUint(vec3 _normal) 43 | { 44 | return _normal * 0.5 + 0.5; 45 | } 46 | 47 | vec3 decodeNormalUint(vec3 _encodedNormal) 48 | { 49 | return _encodedNormal * 2.0 - 1.0; 50 | } 51 | 52 | vec2 encodeNormalSphereMap(vec3 _normal) 53 | { 54 | return normalize(_normal.xy) * sqrt(_normal.z * 0.5 + 0.5); 55 | } 56 | 57 | vec3 decodeNormalSphereMap(vec2 _encodedNormal) 58 | { 59 | float zz = dot(_encodedNormal, _encodedNormal) * 2.0 - 1.0; 60 | return vec3(normalize(_encodedNormal.xy) * sqrt(1.0 - zz*zz), zz); 61 | } 62 | 63 | vec2 octahedronWrap(vec2 _val) 64 | { 65 | // Reference: 66 | // Octahedron normal vector encoding 67 | // http://kriscg.blogspot.com/2014/04/octahedron-normal-vector-encoding.html 68 | return (1.0 - abs(_val.yx) ) 69 | * mix(vec2_splat(-1.0), vec2_splat(1.0), vec2(greaterThanEqual(_val.xy, vec2_splat(0.0) ) ) ); 70 | } 71 | 72 | vec2 encodeNormalOctahedron(vec3 _normal) 73 | { 74 | _normal /= abs(_normal.x) + abs(_normal.y) + abs(_normal.z); 75 | _normal.xy = _normal.z >= 0.0 ? _normal.xy : octahedronWrap(_normal.xy); 76 | _normal.xy = _normal.xy * 0.5 + 0.5; 77 | return _normal.xy; 78 | } 79 | 80 | vec3 decodeNormalOctahedron(vec2 _encodedNormal) 81 | { 82 | _encodedNormal = _encodedNormal * 2.0 - 1.0; 83 | 84 | vec3 normal; 85 | normal.z = 1.0 - abs(_encodedNormal.x) - abs(_encodedNormal.y); 86 | normal.xy = normal.z >= 0.0 ? _encodedNormal.xy : octahedronWrap(_encodedNormal.xy); 87 | return normalize(normal); 88 | } 89 | 90 | vec3 convertRGB2XYZ(vec3 _rgb) 91 | { 92 | // Reference: 93 | // RGB/XYZ Matrices 94 | // http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html 95 | vec3 xyz; 96 | xyz.x = dot(vec3(0.4124564, 0.3575761, 0.1804375), _rgb); 97 | xyz.y = dot(vec3(0.2126729, 0.7151522, 0.0721750), _rgb); 98 | xyz.z = dot(vec3(0.0193339, 0.1191920, 0.9503041), _rgb); 99 | return xyz; 100 | } 101 | 102 | vec3 convertXYZ2RGB(vec3 _xyz) 103 | { 104 | vec3 rgb; 105 | rgb.x = dot(vec3( 3.2404542, -1.5371385, -0.4985314), _xyz); 106 | rgb.y = dot(vec3(-0.9692660, 1.8760108, 0.0415560), _xyz); 107 | rgb.z = dot(vec3( 0.0556434, -0.2040259, 1.0572252), _xyz); 108 | return rgb; 109 | } 110 | 111 | vec3 convertXYZ2Yxy(vec3 _xyz) 112 | { 113 | // Reference: 114 | // http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_xyY.html 115 | float inv = 1.0/dot(_xyz, vec3(1.0, 1.0, 1.0) ); 116 | return vec3(_xyz.y, _xyz.x*inv, _xyz.y*inv); 117 | } 118 | 119 | vec3 convertYxy2XYZ(vec3 _Yxy) 120 | { 121 | // Reference: 122 | // http://www.brucelindbloom.com/index.html?Eqn_xyY_to_XYZ.html 123 | vec3 xyz; 124 | xyz.x = _Yxy.x*_Yxy.y/_Yxy.z; 125 | xyz.y = _Yxy.x; 126 | xyz.z = _Yxy.x*(1.0 - _Yxy.y - _Yxy.z)/_Yxy.z; 127 | return xyz; 128 | } 129 | 130 | vec3 convertRGB2Yxy(vec3 _rgb) 131 | { 132 | return convertXYZ2Yxy(convertRGB2XYZ(_rgb) ); 133 | } 134 | 135 | vec3 convertYxy2RGB(vec3 _Yxy) 136 | { 137 | return convertXYZ2RGB(convertYxy2XYZ(_Yxy) ); 138 | } 139 | 140 | vec3 convertRGB2Yuv(vec3 _rgb) 141 | { 142 | vec3 yuv; 143 | yuv.x = dot(_rgb, vec3(0.299, 0.587, 0.114) ); 144 | yuv.y = (_rgb.x - yuv.x)*0.713 + 0.5; 145 | yuv.z = (_rgb.z - yuv.x)*0.564 + 0.5; 146 | return yuv; 147 | } 148 | 149 | vec3 convertYuv2RGB(vec3 _yuv) 150 | { 151 | vec3 rgb; 152 | rgb.x = _yuv.x + 1.403*(_yuv.y-0.5); 153 | rgb.y = _yuv.x - 0.344*(_yuv.y-0.5) - 0.714*(_yuv.z-0.5); 154 | rgb.z = _yuv.x + 1.773*(_yuv.z-0.5); 155 | return rgb; 156 | } 157 | 158 | vec3 convertRGB2YIQ(vec3 _rgb) 159 | { 160 | vec3 yiq; 161 | yiq.x = dot(vec3(0.299, 0.587, 0.114 ), _rgb); 162 | yiq.y = dot(vec3(0.595716, -0.274453, -0.321263), _rgb); 163 | yiq.z = dot(vec3(0.211456, -0.522591, 0.311135), _rgb); 164 | return yiq; 165 | } 166 | 167 | vec3 convertYIQ2RGB(vec3 _yiq) 168 | { 169 | vec3 rgb; 170 | rgb.x = dot(vec3(1.0, 0.9563, 0.6210), _yiq); 171 | rgb.y = dot(vec3(1.0, -0.2721, -0.6474), _yiq); 172 | rgb.z = dot(vec3(1.0, -1.1070, 1.7046), _yiq); 173 | return rgb; 174 | } 175 | 176 | vec3 toLinear(vec3 _rgb) 177 | { 178 | return pow(abs(_rgb), vec3_splat(2.2) ); 179 | } 180 | 181 | vec4 toLinear(vec4 _rgba) 182 | { 183 | return vec4(toLinear(_rgba.xyz), _rgba.w); 184 | } 185 | 186 | vec3 toLinearAccurate(vec3 _rgb) 187 | { 188 | vec3 lo = _rgb / 12.92; 189 | vec3 hi = pow( (_rgb + 0.055) / 1.055, vec3_splat(2.4) ); 190 | vec3 rgb = mix(hi, lo, vec3(lessThanEqual(_rgb, vec3_splat(0.04045) ) ) ); 191 | return rgb; 192 | } 193 | 194 | vec4 toLinearAccurate(vec4 _rgba) 195 | { 196 | return vec4(toLinearAccurate(_rgba.xyz), _rgba.w); 197 | } 198 | 199 | float toGamma(float _r) 200 | { 201 | return pow(abs(_r), 1.0/2.2); 202 | } 203 | 204 | vec3 toGamma(vec3 _rgb) 205 | { 206 | return pow(abs(_rgb), vec3_splat(1.0/2.2) ); 207 | } 208 | 209 | vec4 toGamma(vec4 _rgba) 210 | { 211 | return vec4(toGamma(_rgba.xyz), _rgba.w); 212 | } 213 | 214 | vec3 toGammaAccurate(vec3 _rgb) 215 | { 216 | vec3 lo = _rgb * 12.92; 217 | vec3 hi = pow(abs(_rgb), vec3_splat(1.0/2.4) ) * 1.055 - 0.055; 218 | vec3 rgb = mix(hi, lo, vec3(lessThanEqual(_rgb, vec3_splat(0.0031308) ) ) ); 219 | return rgb; 220 | } 221 | 222 | vec4 toGammaAccurate(vec4 _rgba) 223 | { 224 | return vec4(toGammaAccurate(_rgba.xyz), _rgba.w); 225 | } 226 | 227 | vec3 toReinhard(vec3 _rgb) 228 | { 229 | return toGamma(_rgb/(_rgb+vec3_splat(1.0) ) ); 230 | } 231 | 232 | vec4 toReinhard(vec4 _rgba) 233 | { 234 | return vec4(toReinhard(_rgba.xyz), _rgba.w); 235 | } 236 | 237 | vec3 toFilmic(vec3 _rgb) 238 | { 239 | _rgb = max(vec3_splat(0.0), _rgb - 0.004); 240 | _rgb = (_rgb*(6.2*_rgb + 0.5) ) / (_rgb*(6.2*_rgb + 1.7) + 0.06); 241 | return _rgb; 242 | } 243 | 244 | vec4 toFilmic(vec4 _rgba) 245 | { 246 | return vec4(toFilmic(_rgba.xyz), _rgba.w); 247 | } 248 | 249 | vec3 toAcesFilmic(vec3 _rgb) 250 | { 251 | // Reference: 252 | // ACES Filmic Tone Mapping Curve 253 | // https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/ 254 | float aa = 2.51f; 255 | float bb = 0.03f; 256 | float cc = 2.43f; 257 | float dd = 0.59f; 258 | float ee = 0.14f; 259 | return saturate( (_rgb*(aa*_rgb + bb) )/(_rgb*(cc*_rgb + dd) + ee) ); 260 | } 261 | 262 | vec4 toAcesFilmic(vec4 _rgba) 263 | { 264 | return vec4(toAcesFilmic(_rgba.xyz), _rgba.w); 265 | } 266 | 267 | vec3 luma(vec3 _rgb) 268 | { 269 | float yy = dot(vec3(0.2126729, 0.7151522, 0.0721750), _rgb); 270 | return vec3_splat(yy); 271 | } 272 | 273 | vec4 luma(vec4 _rgba) 274 | { 275 | return vec4(luma(_rgba.xyz), _rgba.w); 276 | } 277 | 278 | vec3 conSatBri(vec3 _rgb, vec3 _csb) 279 | { 280 | vec3 rgb = _rgb * _csb.z; 281 | rgb = mix(luma(rgb), rgb, _csb.y); 282 | rgb = mix(vec3_splat(0.5), rgb, _csb.x); 283 | return rgb; 284 | } 285 | 286 | vec4 conSatBri(vec4 _rgba, vec3 _csb) 287 | { 288 | return vec4(conSatBri(_rgba.xyz, _csb), _rgba.w); 289 | } 290 | 291 | vec3 posterize(vec3 _rgb, float _numColors) 292 | { 293 | return floor(_rgb*_numColors) / _numColors; 294 | } 295 | 296 | vec4 posterize(vec4 _rgba, float _numColors) 297 | { 298 | return vec4(posterize(_rgba.xyz, _numColors), _rgba.w); 299 | } 300 | 301 | vec3 sepia(vec3 _rgb) 302 | { 303 | vec3 color; 304 | color.x = dot(_rgb, vec3(0.393, 0.769, 0.189) ); 305 | color.y = dot(_rgb, vec3(0.349, 0.686, 0.168) ); 306 | color.z = dot(_rgb, vec3(0.272, 0.534, 0.131) ); 307 | return color; 308 | } 309 | 310 | vec4 sepia(vec4 _rgba) 311 | { 312 | return vec4(sepia(_rgba.xyz), _rgba.w); 313 | } 314 | 315 | vec3 blendOverlay(vec3 _base, vec3 _blend) 316 | { 317 | vec3 lt = 2.0 * _base * _blend; 318 | vec3 gte = 1.0 - 2.0 * (1.0 - _base) * (1.0 - _blend); 319 | return mix(lt, gte, step(vec3_splat(0.5), _base) ); 320 | } 321 | 322 | vec4 blendOverlay(vec4 _base, vec4 _blend) 323 | { 324 | return vec4(blendOverlay(_base.xyz, _blend.xyz), _base.w); 325 | } 326 | 327 | vec3 adjustHue(vec3 _rgb, float _hue) 328 | { 329 | vec3 yiq = convertRGB2YIQ(_rgb); 330 | float angle = _hue + atan2(yiq.z, yiq.y); 331 | float len = length(yiq.yz); 332 | return convertYIQ2RGB(vec3(yiq.x, len*cos(angle), len*sin(angle) ) ); 333 | } 334 | 335 | vec4 packFloatToRgba(float _value) 336 | { 337 | const vec4 shift = vec4(256 * 256 * 256, 256 * 256, 256, 1.0); 338 | const vec4 mask = vec4(0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0); 339 | vec4 comp = fract(_value * shift); 340 | comp -= comp.xxyz * mask; 341 | return comp; 342 | } 343 | 344 | float unpackRgbaToFloat(vec4 _rgba) 345 | { 346 | const vec4 shift = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0); 347 | return dot(_rgba, shift); 348 | } 349 | 350 | vec2 packHalfFloat(float _value) 351 | { 352 | const vec2 shift = vec2(256, 1.0); 353 | const vec2 mask = vec2(0, 1.0 / 256.0); 354 | vec2 comp = fract(_value * shift); 355 | comp -= comp.xx * mask; 356 | return comp; 357 | } 358 | 359 | float unpackHalfFloat(vec2 _rg) 360 | { 361 | const vec2 shift = vec2(1.0 / 256.0, 1.0); 362 | return dot(_rg, shift); 363 | } 364 | 365 | float random(vec2 _uv) 366 | { 367 | return fract(sin(dot(_uv.xy, vec2(12.9898, 78.233) ) ) * 43758.5453); 368 | } 369 | 370 | vec3 fixCubeLookup(vec3 _v, float _lod, float _topLevelCubeSize) 371 | { 372 | // Reference: 373 | // Seamless cube-map filtering 374 | // http://the-witness.net/news/2012/02/seamless-cube-map-filtering/ 375 | float ax = abs(_v.x); 376 | float ay = abs(_v.y); 377 | float az = abs(_v.z); 378 | float vmax = max(max(ax, ay), az); 379 | float scale = 1.0 - exp2(_lod) / _topLevelCubeSize; 380 | if (ax != vmax) { _v.x *= scale; } 381 | if (ay != vmax) { _v.y *= scale; } 382 | if (az != vmax) { _v.z *= scale; } 383 | return _v; 384 | } 385 | 386 | vec2 texture2DBc5(sampler2D _sampler, vec2 _uv) 387 | { 388 | #if BGFX_SHADER_LANGUAGE_HLSL && BGFX_SHADER_LANGUAGE_HLSL <= 3 389 | return texture2D(_sampler, _uv).yx; 390 | #else 391 | return texture2D(_sampler, _uv).xy; 392 | #endif 393 | } 394 | 395 | #endif // __SHADERLIB_SH__ 396 | -------------------------------------------------------------------------------- /scripts/genie.lua: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2018 Branimir Karadzic. All rights reserved. 3 | -- License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause 4 | -- 5 | 6 | newoption { 7 | trigger = "with-amalgamated", 8 | description = "Enable amalgamated build.", 9 | } 10 | 11 | newoption { 12 | trigger = "with-sdl", 13 | description = "Enable SDL entry.", 14 | } 15 | 16 | newoption { 17 | trigger = "with-glfw", 18 | description = "Enable GLFW entry.", 19 | } 20 | 21 | newoption { 22 | trigger = "with-profiler", 23 | description = "Enable build with intrusive profiler.", 24 | } 25 | 26 | newoption { 27 | trigger = "with-scintilla", 28 | description = "Enable building with Scintilla editor.", 29 | } 30 | 31 | newoption { 32 | trigger = "with-shared-lib", 33 | description = "Enable building shared library.", 34 | } 35 | 36 | newoption { 37 | trigger = "with-tools", 38 | description = "Enable building tools.", 39 | } 40 | 41 | newoption { 42 | trigger = "with-combined-examples", 43 | description = "Enable building examples (combined as single executable).", 44 | } 45 | 46 | newoption { 47 | trigger = "with-examples", 48 | description = "Enable building examples.", 49 | } 50 | 51 | solution "bgfx" 52 | configurations { 53 | "Debug", 54 | "Release", 55 | } 56 | 57 | if _ACTION == "xcode4" then 58 | platforms { 59 | "Universal", 60 | } 61 | else 62 | platforms { 63 | "x32", 64 | "x64", 65 | -- "Xbox360", 66 | "Native", -- for targets where bitness is not specified 67 | } 68 | end 69 | 70 | language "C++" 71 | startproject "example-00-helloworld" 72 | 73 | MODULE_DIR = path.getabsolute("../") 74 | BGFX_DIR = path.getabsolute("..") 75 | BX_DIR = os.getenv("BX_DIR") 76 | BIMG_DIR = os.getenv("BIMG_DIR") 77 | 78 | local BGFX_BUILD_DIR = path.join(BGFX_DIR, ".build") 79 | local BGFX_THIRD_PARTY_DIR = path.join(BGFX_DIR, "3rdparty") 80 | if not BX_DIR then 81 | BX_DIR = path.getabsolute(path.join(BGFX_DIR, "../bx")) 82 | end 83 | 84 | if not BIMG_DIR then 85 | BIMG_DIR = path.getabsolute(path.join(BGFX_DIR, "../bimg")) 86 | end 87 | 88 | if not os.isdir(BX_DIR) or not os.isdir(BIMG_DIR) then 89 | 90 | if not os.isdir(BX_DIR) then 91 | print("bx not found at " .. BX_DIR) 92 | end 93 | 94 | if not os.isdir(BIMG_DIR) then 95 | print("bimg not found at " .. BIMG_DIR) 96 | end 97 | 98 | print("For more info see: https://bkaradzic.github.io/bgfx/build.html") 99 | os.exit() 100 | end 101 | 102 | dofile (path.join(BX_DIR, "scripts/toolchain.lua")) 103 | if not toolchain(BGFX_BUILD_DIR, BGFX_THIRD_PARTY_DIR) then 104 | return -- no action specified 105 | end 106 | 107 | function copyLib() 108 | end 109 | 110 | if _OPTIONS["with-sdl"] then 111 | if os.is("windows") then 112 | if not os.getenv("SDL2_DIR") then 113 | print("Set SDL2_DIR enviroment variable.") 114 | end 115 | end 116 | end 117 | 118 | if _OPTIONS["with-profiler"] then 119 | defines { 120 | "ENTRY_CONFIG_PROFILER=1", 121 | "BGFX_CONFIG_PROFILER=1", 122 | } 123 | end 124 | 125 | function exampleProjectDefaults() 126 | 127 | debugdir (path.join(BGFX_DIR, "examples/runtime")) 128 | 129 | includedirs { 130 | path.join(BX_DIR, "include"), 131 | path.join(BIMG_DIR, "include"), 132 | path.join(BGFX_DIR, "include"), 133 | path.join(BGFX_DIR, "3rdparty"), 134 | path.join(BGFX_DIR, "examples/common"), 135 | } 136 | 137 | flags { 138 | "FatalWarnings", 139 | } 140 | 141 | links { 142 | "example-common", 143 | "example-glue", 144 | "bgfx", 145 | "bimg_decode", 146 | "bimg", 147 | "bx", 148 | } 149 | 150 | if _OPTIONS["with-sdl"] then 151 | defines { "ENTRY_CONFIG_USE_SDL=1" } 152 | links { "SDL2" } 153 | 154 | configuration { "osx" } 155 | libdirs { "$(SDL2_DIR)/lib" } 156 | 157 | configuration {} 158 | end 159 | 160 | if _OPTIONS["with-glfw"] then 161 | defines { "ENTRY_CONFIG_USE_GLFW=1" } 162 | links { "glfw3" } 163 | 164 | configuration { "linux or freebsd" } 165 | links { 166 | "Xrandr", 167 | "Xinerama", 168 | "Xi", 169 | "Xxf86vm", 170 | "Xcursor", 171 | } 172 | 173 | configuration { "osx" } 174 | linkoptions { 175 | "-framework CoreVideo", 176 | "-framework IOKit", 177 | } 178 | 179 | configuration {} 180 | end 181 | 182 | configuration { "vs*", "x32 or x64" } 183 | linkoptions { 184 | "/ignore:4199", -- LNK4199: /DELAYLOAD:*.dll ignored; no imports found from *.dll 185 | } 186 | links { -- this is needed only for testing with GLES2/3 on Windows with VS2008 187 | "DelayImp", 188 | } 189 | 190 | configuration { "vs201*", "x32 or x64" } 191 | linkoptions { -- this is needed only for testing with GLES2/3 on Windows with VS201x 192 | "/DELAYLOAD:\"libEGL.dll\"", 193 | "/DELAYLOAD:\"libGLESv2.dll\"", 194 | } 195 | 196 | configuration { "mingw*" } 197 | targetextension ".exe" 198 | links { 199 | "gdi32", 200 | "psapi", 201 | } 202 | 203 | configuration { "vs20*", "x32 or x64" } 204 | links { 205 | "gdi32", 206 | "psapi", 207 | } 208 | 209 | configuration { "durango" } 210 | links { 211 | "d3d11_x", 212 | "d3d12_x", 213 | "combase", 214 | "kernelx", 215 | } 216 | 217 | configuration { "winstore*" } 218 | removelinks { 219 | "DelayImp", 220 | "gdi32", 221 | "psapi" 222 | } 223 | links { 224 | "d3d11", 225 | "d3d12", 226 | "dxgi" 227 | } 228 | linkoptions { 229 | "/ignore:4264" -- LNK4264: archiving object file compiled with /ZW into a static library; note that when authoring Windows Runtime types it is not recommended to link with a static library that contains Windows Runtime metadata 230 | } 231 | 232 | -- WinRT targets need their own output directories or build files stomp over each other 233 | configuration { "x32", "winstore*" } 234 | targetdir (path.join(BGFX_BUILD_DIR, "win32_" .. _ACTION, "bin", _name)) 235 | objdir (path.join(BGFX_BUILD_DIR, "win32_" .. _ACTION, "obj", _name)) 236 | 237 | configuration { "x64", "winstore*" } 238 | targetdir (path.join(BGFX_BUILD_DIR, "win64_" .. _ACTION, "bin", _name)) 239 | objdir (path.join(BGFX_BUILD_DIR, "win64_" .. _ACTION, "obj", _name)) 240 | 241 | configuration { "ARM", "winstore*" } 242 | targetdir (path.join(BGFX_BUILD_DIR, "arm_" .. _ACTION, "bin", _name)) 243 | objdir (path.join(BGFX_BUILD_DIR, "arm_" .. _ACTION, "obj", _name)) 244 | 245 | configuration { "mingw-clang" } 246 | kind "ConsoleApp" 247 | 248 | configuration { "android*" } 249 | kind "ConsoleApp" 250 | targetextension ".so" 251 | linkoptions { 252 | "-shared", 253 | } 254 | links { 255 | "EGL", 256 | "GLESv2", 257 | } 258 | 259 | configuration { "asmjs" } 260 | kind "ConsoleApp" 261 | targetextension ".bc" 262 | 263 | configuration { "linux-* or freebsd", "not linux-steamlink" } 264 | links { 265 | "X11", 266 | "GL", 267 | "pthread", 268 | } 269 | 270 | configuration { "linux-steamlink" } 271 | links { 272 | "EGL", 273 | "GLESv2", 274 | "SDL2", 275 | "pthread", 276 | } 277 | 278 | configuration { "rpi" } 279 | links { 280 | "X11", 281 | "brcmGLESv2", 282 | "brcmEGL", 283 | "bcm_host", 284 | "vcos", 285 | "vchiq_arm", 286 | "pthread", 287 | } 288 | 289 | configuration { "osx" } 290 | linkoptions { 291 | "-framework Cocoa", 292 | "-framework QuartzCore", 293 | "-framework OpenGL", 294 | "-weak_framework Metal", 295 | } 296 | 297 | configuration { "ios* or tvos*" } 298 | kind "ConsoleApp" 299 | linkoptions { 300 | "-framework CoreFoundation", 301 | "-framework Foundation", 302 | "-framework OpenGLES", 303 | "-framework UIKit", 304 | "-framework QuartzCore", 305 | "-weak_framework Metal", 306 | } 307 | 308 | configuration { "xcode4", "ios" } 309 | kind "WindowedApp" 310 | files { 311 | path.join(BGFX_DIR, "examples/runtime/iOS-Info.plist"), 312 | } 313 | 314 | configuration { "xcode4", "tvos" } 315 | kind "WindowedApp" 316 | files { 317 | path.join(BGFX_DIR, "examples/runtime/tvOS-Info.plist"), 318 | } 319 | 320 | 321 | configuration { "qnx*" } 322 | targetextension "" 323 | links { 324 | "EGL", 325 | "GLESv2", 326 | } 327 | 328 | configuration {} 329 | 330 | strip() 331 | end 332 | 333 | function exampleProject(_combined, ...) 334 | 335 | if _combined then 336 | 337 | project ("examples") 338 | uuid (os.uuid("examples")) 339 | kind "WindowedApp" 340 | 341 | for _, name in ipairs({...}) do 342 | 343 | files { 344 | path.join(BGFX_DIR, "examples", name, "**.c"), 345 | path.join(BGFX_DIR, "examples", name, "**.cpp"), 346 | path.join(BGFX_DIR, "examples", name, "**.h"), 347 | } 348 | 349 | removefiles { 350 | path.join(BGFX_DIR, "examples", name, "**.bin.h"), 351 | } 352 | 353 | end 354 | 355 | files { 356 | path.join(BGFX_DIR, "examples/25-c99/helloworld.c"), -- hack for _main_ 357 | } 358 | 359 | exampleProjectDefaults() 360 | 361 | else 362 | 363 | for _, name in ipairs({...}) do 364 | project ("example-" .. name) 365 | uuid (os.uuid("example-" .. name)) 366 | kind "WindowedApp" 367 | 368 | files { 369 | path.join(BGFX_DIR, "examples", name, "**.c"), 370 | path.join(BGFX_DIR, "examples", name, "**.cpp"), 371 | path.join(BGFX_DIR, "examples", name, "**.h"), 372 | } 373 | 374 | removefiles { 375 | path.join(BGFX_DIR, "examples", name, "**.bin.h"), 376 | } 377 | 378 | defines { 379 | "ENTRY_CONFIG_IMPLEMENT_MAIN=1", 380 | } 381 | 382 | exampleProjectDefaults() 383 | end 384 | end 385 | 386 | end 387 | 388 | dofile "bgfx.lua" 389 | 390 | group "libs" 391 | bgfxProject("", "StaticLib", {}) 392 | 393 | dofile(path.join(BX_DIR, "scripts/bx.lua")) 394 | dofile(path.join(BIMG_DIR, "scripts/bimg.lua")) 395 | dofile(path.join(BIMG_DIR, "scripts/bimg_decode.lua")) 396 | 397 | if _OPTIONS["with-tools"] then 398 | dofile(path.join(BIMG_DIR, "scripts/bimg_encode.lua")) 399 | end 400 | 401 | if _OPTIONS["with-examples"] 402 | or _OPTIONS["with-combined-examples"] 403 | or _OPTIONS["with-tools"] then 404 | group "examples" 405 | dofile "example-common.lua" 406 | end 407 | 408 | if _OPTIONS["with-examples"] 409 | or _OPTIONS["with-combined-examples"] then 410 | group "examples" 411 | 412 | exampleProject(_OPTIONS["with-combined-examples"] 413 | , "00-helloworld" 414 | , "01-cubes" 415 | , "02-metaballs" 416 | , "03-raymarch" 417 | , "04-mesh" 418 | , "05-instancing" 419 | , "06-bump" 420 | , "07-callback" 421 | , "08-update" 422 | , "09-hdr" 423 | , "10-font" 424 | , "11-fontsdf" 425 | , "12-lod" 426 | , "13-stencil" 427 | , "14-shadowvolumes" 428 | , "15-shadowmaps-simple" 429 | , "16-shadowmaps" 430 | , "17-drawstress" 431 | , "18-ibl" 432 | , "19-oit" 433 | , "20-nanovg" 434 | , "21-deferred" 435 | , "22-windows" 436 | , "23-vectordisplay" 437 | , "24-nbody" 438 | , "26-occlusion" 439 | , "27-terrain" 440 | , "28-wireframe" 441 | , "29-debugdraw" 442 | , "30-picking" 443 | , "31-rsm" 444 | , "32-particles" 445 | , "33-pom" 446 | , "34-mvs" 447 | , "35-dynamic" 448 | , "36-sky" 449 | , "37-gpudrivenrendering" 450 | , "38-bloom" 451 | ) 452 | 453 | -- C99 source doesn't compile under WinRT settings 454 | if not premake.vstudio.iswinrt() then 455 | exampleProject(false, "25-c99") 456 | end 457 | end 458 | 459 | if _OPTIONS["with-shared-lib"] then 460 | group "libs" 461 | bgfxProject("-shared-lib", "SharedLib", {}) 462 | end 463 | 464 | if _OPTIONS["with-tools"] then 465 | group "tools" 466 | dofile "shaderc.lua" 467 | dofile "texturec.lua" 468 | dofile "texturev.lua" 469 | dofile "geometryc.lua" 470 | end 471 | 472 | -- build brtshaderc (shaderc in static library version) 473 | dofile "brtshaderc.lua" 474 | dofile "brtshaderc-example.lua" 475 | -------------------------------------------------------------------------------- /scripts/brtshaderc.lua: -------------------------------------------------------------------------------- 1 | -- 2 | -- Copyright 2010-2018 Branimir Karadzic. All rights reserved. 3 | -- License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause 4 | -- 5 | 6 | local GLSL_OPTIMIZER = path.join(BGFX_DIR, "3rdparty/glsl-optimizer") 7 | local FCPP_DIR = path.join(BGFX_DIR, "3rdparty/fcpp") 8 | local GLSLANG = path.join(BGFX_DIR, "3rdparty/glslang") 9 | local SPIRV_TOOLS = path.join(BGFX_DIR, "3rdparty/spirv-tools") 10 | 11 | group "libs" 12 | 13 | project "brtshaderc" 14 | kind "StaticLib" 15 | 16 | files { 17 | path.join(BGFX_DIR, "tools/brtshaderc/brtshaderc.cpp"), 18 | } 19 | 20 | 21 | --project "glslang" 22 | -- kind "StaticLib" 23 | 24 | defines { 25 | "ENABLE_OPT=1", -- spirv-tools 26 | "ENABLE_HLSL=1", 27 | } 28 | 29 | includedirs { 30 | GLSLANG, 31 | path.join(SPIRV_TOOLS, "include"), 32 | path.join(SPIRV_TOOLS, "source"), 33 | } 34 | 35 | files { 36 | path.join(GLSLANG, "glslang/**.cpp"), 37 | path.join(GLSLANG, "glslang/**.h"), 38 | 39 | path.join(GLSLANG, "hlsl/**.cpp"), 40 | path.join(GLSLANG, "hlsl/**.h"), 41 | 42 | path.join(GLSLANG, "SPIRV/**.cpp"), 43 | path.join(GLSLANG, "SPIRV/**.h"), 44 | 45 | path.join(GLSLANG, "OGLCompilersDLL/**.cpp"), 46 | path.join(GLSLANG, "OGLCompilersDLL/**.h"), 47 | } 48 | 49 | removefiles { 50 | path.join(GLSLANG, "glslang/OSDependent/Unix/main.cpp"), 51 | path.join(GLSLANG, "glslang/OSDependent/Windows/main.cpp"), 52 | } 53 | 54 | configuration { "windows" } 55 | removefiles { 56 | path.join(GLSLANG, "glslang/OSDependent/Unix/**.cpp"), 57 | path.join(GLSLANG, "glslang/OSDependent/Unix/**.h"), 58 | } 59 | 60 | configuration { "not windows" } 61 | removefiles { 62 | path.join(GLSLANG, "glslang/OSDependent/Windows/**.cpp"), 63 | path.join(GLSLANG, "glslang/OSDependent/Windows/**.h"), 64 | } 65 | 66 | configuration { "vs*" } 67 | buildoptions { 68 | "/wd4005", -- warning C4005: '_CRT_SECURE_NO_WARNINGS': macro redefinition 69 | "/wd4100", -- warning C4100: 'inclusionDepth' : unreferenced formal parameter 70 | "/wd4127", -- warning C4127: conditional expression is constant 71 | "/wd4189", -- warning C4189: 'isFloat': local variable is initialized but not referenced 72 | "/wd4244", -- warning C4244: '=': conversion from 'int' to 'char', possible loss of data 73 | "/wd4310", -- warning C4310: cast truncates constant value 74 | "/wd4456", -- warning C4456: declaration of 'feature' hides previous local declaration 75 | "/wd4457", -- warning C4457: declaration of 'token' hides function parameter 76 | "/wd4458", -- warning C4458: declaration of 'language' hides class member 77 | "/wd4702", -- warning C4702: unreachable code 78 | "/wd4715", -- warning C4715: 'spv::Builder::makeFpConstant': not all control paths return a value 79 | } 80 | 81 | configuration { "mingw* or linux or osx" } 82 | buildoptions { 83 | "-Wno-ignored-qualifiers", 84 | "-Wno-missing-field-initializers", 85 | "-Wno-reorder", 86 | "-Wno-return-type", 87 | "-Wno-shadow", 88 | "-Wno-sign-compare", 89 | "-Wno-undef", 90 | "-Wno-unknown-pragmas", 91 | "-Wno-unused-function", 92 | "-Wno-unused-parameter", 93 | "-Wno-unused-variable", 94 | } 95 | 96 | configuration { "osx" } 97 | buildoptions { 98 | "-Wno-c++11-extensions", 99 | "-Wno-unused-const-variable", 100 | "-Wno-deprecated-register", 101 | } 102 | 103 | configuration { "linux-*" } 104 | buildoptions { 105 | "-Wno-unused-but-set-variable", 106 | } 107 | 108 | configuration { "mingw* or linux or osx" } 109 | buildoptions { 110 | "-fno-strict-aliasing", -- glslang has bugs if strict aliasing is used. 111 | } 112 | 113 | configuration {} 114 | 115 | --project "glsl-optimizer" 116 | -- kind "StaticLib" 117 | 118 | includedirs { 119 | path.join(GLSL_OPTIMIZER, "src"), 120 | path.join(GLSL_OPTIMIZER, "include"), 121 | path.join(GLSL_OPTIMIZER, "src/mesa"), 122 | path.join(GLSL_OPTIMIZER, "src/mapi"), 123 | path.join(GLSL_OPTIMIZER, "src/glsl"), 124 | } 125 | 126 | files { 127 | path.join(GLSL_OPTIMIZER, "src/mesa/**.c"), 128 | path.join(GLSL_OPTIMIZER, "src/glsl/**.cpp"), 129 | path.join(GLSL_OPTIMIZER, "src/mesa/**.h"), 130 | path.join(GLSL_OPTIMIZER, "src/glsl/**.c"), 131 | path.join(GLSL_OPTIMIZER, "src/glsl/**.cpp"), 132 | path.join(GLSL_OPTIMIZER, "src/glsl/**.h"), 133 | path.join(GLSL_OPTIMIZER, "src/util/**.c"), 134 | path.join(GLSL_OPTIMIZER, "src/util/**.h"), 135 | } 136 | 137 | removefiles { 138 | path.join(GLSL_OPTIMIZER, "src/glsl/glcpp/glcpp.c"), 139 | path.join(GLSL_OPTIMIZER, "src/glsl/glcpp/tests/**"), 140 | path.join(GLSL_OPTIMIZER, "src/glsl/glcpp/**.l"), 141 | path.join(GLSL_OPTIMIZER, "src/glsl/glcpp/**.y"), 142 | path.join(GLSL_OPTIMIZER, "src/glsl/ir_set_program_inouts.cpp"), 143 | path.join(GLSL_OPTIMIZER, "src/glsl/main.cpp"), 144 | path.join(GLSL_OPTIMIZER, "src/glsl/builtin_stubs.cpp"), 145 | } 146 | 147 | configuration { "Release" } 148 | flags { 149 | "Optimize", 150 | } 151 | 152 | removeflags { 153 | -- GCC 4.9 -O2 + -fno-strict-aliasing don't work together... 154 | "OptimizeSpeed", 155 | } 156 | 157 | configuration { "vs*" } 158 | includedirs { 159 | path.join(GLSL_OPTIMIZER, "src/glsl/msvc"), 160 | } 161 | 162 | defines { -- glsl-optimizer 163 | "__STDC__", 164 | "__STDC_VERSION__=199901L", 165 | "strdup=_strdup", 166 | "alloca=_alloca", 167 | "isascii=__isascii", 168 | } 169 | 170 | buildoptions { 171 | "/wd4100", -- error C4100: '' : unreferenced formal parameter 172 | "/wd4127", -- warning C4127: conditional expression is constant 173 | "/wd4132", -- warning C4132: 'deleted_key_value': const object should be initialized 174 | "/wd4189", -- warning C4189: 'interface_type': local variable is initialized but not referenced 175 | "/wd4204", -- warning C4204: nonstandard extension used: non-constant aggregate initializer 176 | "/wd4244", -- warning C4244: '=': conversion from 'const flex_int32_t' to 'YY_CHAR', possible loss of data 177 | "/wd4389", -- warning C4389: '!=': signed/unsigned mismatch 178 | "/wd4245", -- warning C4245: 'return': conversion from 'int' to 'unsigned int', signed/unsigned mismatch 179 | "/wd4701", -- warning C4701: potentially uninitialized local variable 'lower' used 180 | "/wd4702", -- warning C4702: unreachable code 181 | "/wd4706", -- warning C4706: assignment within conditional expression 182 | "/wd4996", -- warning C4996: 'strdup': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _strdup. 183 | } 184 | 185 | configuration { "mingw* or linux or osx" } 186 | buildoptions { 187 | "-fno-strict-aliasing", -- glsl-optimizer has bugs if strict aliasing is used. 188 | 189 | "-Wno-implicit-fallthrough", 190 | "-Wno-sign-compare", 191 | "-Wno-unused-function", 192 | "-Wno-unused-parameter", 193 | } 194 | 195 | removebuildoptions { 196 | "-Wshadow", -- glsl-optimizer is full of -Wshadow warnings ignore it. 197 | } 198 | 199 | configuration { "osx" } 200 | buildoptions { 201 | "-Wno-deprecated-register", 202 | } 203 | 204 | configuration { "mingw* or linux" } 205 | buildoptions { 206 | "-Wno-misleading-indentation", 207 | } 208 | 209 | configuration {} 210 | 211 | --project "fcpp" 212 | -- kind "StaticLib" 213 | 214 | defines { -- fcpp 215 | "NINCLUDE=64", 216 | "NWORK=65536", 217 | "NBUFF=65536", 218 | "OLD_PREPROCESSOR=0", 219 | } 220 | 221 | files { 222 | path.join(FCPP_DIR, "**.h"), 223 | path.join(FCPP_DIR, "cpp1.c"), 224 | path.join(FCPP_DIR, "cpp2.c"), 225 | path.join(FCPP_DIR, "cpp3.c"), 226 | path.join(FCPP_DIR, "cpp4.c"), 227 | path.join(FCPP_DIR, "cpp5.c"), 228 | path.join(FCPP_DIR, "cpp6.c"), 229 | path.join(FCPP_DIR, "cpp6.c"), 230 | } 231 | 232 | configuration { "vs*" } 233 | 234 | buildoptions { 235 | "/wd4055", -- warning C4055: 'type cast': from data pointer 'void *' to function pointer 'void (__cdecl *)(char *,void *)' 236 | "/wd4244", -- warning C4244: '=': conversion from 'const flex_int32_t' to 'YY_CHAR', possible loss of data 237 | "/wd4701", -- warning C4701: potentially uninitialized local variable 'lower' used 238 | "/wd4706", -- warning C4706: assignment within conditional expression 239 | } 240 | 241 | configuration { "not vs*" } 242 | buildoptions { 243 | "-Wno-implicit-fallthrough", 244 | } 245 | 246 | configuration {} 247 | 248 | --project "shaderc" 249 | -- kind "ConsoleApp" 250 | 251 | includedirs { 252 | path.join(BX_DIR, "include"), 253 | path.join(BIMG_DIR, "include"), 254 | path.join(BGFX_DIR, "include"), 255 | 256 | path.join(BGFX_DIR, "3rdparty/dxsdk/include"), 257 | FCPP_DIR, 258 | 259 | path.join(BGFX_DIR, "3rdparty/glslang/glslang/Public"), 260 | path.join(BGFX_DIR, "3rdparty/glslang/glslang/Include"), 261 | path.join(BGFX_DIR, "3rdparty/glslang"), 262 | 263 | path.join(GLSL_OPTIMIZER, "include"), 264 | path.join(GLSL_OPTIMIZER, "src/glsl"), 265 | } 266 | 267 | links { 268 | "bx", 269 | "fcpp", 270 | "glslang", 271 | "glsl-optimizer", 272 | "spirv-opt", 273 | } 274 | 275 | files { 276 | path.join(BGFX_DIR, "tools/shaderc/**.cpp"), 277 | path.join(BGFX_DIR, "tools/shaderc/**.h"), 278 | path.join(BGFX_DIR, "src/vertexdecl.**"), 279 | path.join(BGFX_DIR, "src/shader_spirv.**"), 280 | } 281 | 282 | configuration { "mingw-*" } 283 | targetextension ".exe" 284 | 285 | configuration { "osx" } 286 | links { 287 | "Cocoa.framework", 288 | } 289 | 290 | configuration { "vs*" } 291 | includedirs { 292 | path.join(GLSL_OPTIMIZER, "include/c99"), 293 | } 294 | 295 | configuration { "vs20* or mingw*" } 296 | links { 297 | "psapi", 298 | } 299 | 300 | configuration {} 301 | 302 | if filesexist(BGFX_DIR, path.join(BGFX_DIR, "../bgfx-ext"), { 303 | path.join(BGFX_DIR, "scripts/shaderc.lua"), }) then 304 | 305 | if filesexist(BGFX_DIR, path.join(BGFX_DIR, "../bgfx-ext"), { 306 | path.join(BGFX_DIR, "tools/shaderc/shaderc_pssl.cpp"), }) then 307 | 308 | removefiles { 309 | path.join(BGFX_DIR, "tools/shaderc/shaderc_pssl.cpp"), 310 | } 311 | end 312 | 313 | dofile(path.join(BGFX_DIR, "../bgfx-ext/scripts/shaderc.lua") ) 314 | end 315 | 316 | configuration { "osx or linux*" } 317 | links { 318 | "pthread", 319 | } 320 | 321 | configuration {} 322 | 323 | strip() 324 | 325 | 326 | -------------------------------------------------------------------------------- /tools/brtshaderc/brtshaderc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | // hack to fix the multiple definition link errors 6 | #define getUniformTypeName getUniformTypeName_shaderc 7 | #define nameToUniformTypeEnum nameToUniformTypeEnum_shaderc 8 | #define s_uniformTypeName s_uniformTypeName_shaderc 9 | 10 | namespace bgfx 11 | { 12 | typedef void(*UserErrorFn)(void*, const char*, va_list); 13 | static UserErrorFn s_user_error_fn = nullptr; 14 | static void* s_user_error_ptr = nullptr; 15 | void setShaderCErrorFunction(UserErrorFn fn, void* user_ptr) 16 | { 17 | s_user_error_fn = fn; 18 | s_user_error_ptr = user_ptr; 19 | } 20 | } 21 | 22 | void printError(FILE* file, const char* format, ...) 23 | { 24 | va_list args; 25 | va_start(args, format); 26 | if (bgfx::s_user_error_fn) 27 | { 28 | bgfx::s_user_error_fn(bgfx::s_user_error_ptr, format, args); 29 | } 30 | else 31 | { 32 | vfprintf(file, format, args); 33 | } 34 | va_end(args); 35 | } 36 | 37 | // hack to defined stuff 38 | #define fprintf printError 39 | #define main fakeMain 40 | #define g_allocator g_shaderc_allocator 41 | 42 | // fix warnings 43 | #undef BX_TRACE 44 | #undef BX_WARN 45 | #undef BX_CHECK 46 | 47 | // include original shaderc code files 48 | #include "../../bgfx/tools/shaderc/shaderc.cpp" 49 | #include "../../bgfx/tools/shaderc/shaderc_hlsl.cpp" 50 | #include "../../bgfx/tools/shaderc/shaderc_glsl.cpp" 51 | //#define static_allocate static_allocate_shaderc 52 | //#define static_deallocate static_deallocate_shaderc 53 | //#include "../../bgfx/tools/shaderc/shaderc_spirv.cpp" 54 | //#include "../../bgfx/tools/shaderc/shaderc_pssl.cpp" 55 | 56 | namespace bgfx 57 | { 58 | bool compilePSSLShader(const Options&, uint32_t, const std::string&, bx::WriterI*) 59 | { 60 | return false; 61 | } 62 | 63 | bool compileSPIRVShader(const Options&, uint32_t, const std::string&, bx::WriterI*) 64 | { 65 | return false; 66 | } 67 | 68 | const char* getPsslPreamble() 69 | { 70 | return ""; 71 | } 72 | } 73 | 74 | 75 | 76 | #include "brtshaderc.h" 77 | using namespace bgfx; 78 | 79 | namespace shaderc 80 | { 81 | /// not a real FileWriter, but a hack to redirect write() to a memory block. 82 | class BufferWriter : public bx::FileWriter 83 | { 84 | public: 85 | 86 | BufferWriter() 87 | { 88 | } 89 | 90 | ~BufferWriter() 91 | { 92 | } 93 | 94 | bool open(const bx::FilePath& _filePath, bool _append, bx::Error* _err) override 95 | { 96 | return true; 97 | } 98 | 99 | const bgfx::Memory* finalize() 100 | { 101 | if(_buffer.size() > 0) 102 | { 103 | _buffer.push_back('\0'); 104 | 105 | const bgfx::Memory* mem = bgfx::alloc(_buffer.size()); 106 | bx::memCopy(mem->data, _buffer.data(), _buffer.size()); 107 | return mem; 108 | } 109 | 110 | return nullptr; 111 | } 112 | 113 | int32_t write(const void* _data, int32_t _size, bx::Error* _err) 114 | { 115 | const char* data = (const char*)_data; 116 | _buffer.insert(_buffer.end(), data, data+_size); 117 | return _size; 118 | } 119 | 120 | private: 121 | BX_ALIGN_DECL(16, uint8_t) m_internal[64]; 122 | typedef std::vector Buffer; 123 | Buffer _buffer; 124 | }; 125 | 126 | 127 | 128 | const bgfx::Memory* compileShader(ShaderType type, const char* filePath, const char* defines, const char* varyingPath, const char* profile) 129 | { 130 | bgfx::Options options; 131 | 132 | options.inputFilePath = filePath; 133 | options.shaderType = type; 134 | 135 | // set platform 136 | #if BX_PLATFORM_LINUX 137 | options.platform = "linux"; 138 | #elif BX_PLATFORM_WINDOWS 139 | options.platform = "windows"; 140 | #elif BX_PLATFORM_ANDROID 141 | options.platform = "android"; 142 | #elif BX_PLATFORM_EMSCRIPTEN 143 | options.platform = "asm.js"; 144 | #elif BX_PLATFORM_IOS 145 | options.platform = "ios"; 146 | #elif BX_PLATFORM_OSX 147 | options.platform = "osx"; 148 | #endif 149 | 150 | // set profile 151 | if(profile) 152 | { 153 | // user profile 154 | options.profile = profile; 155 | } 156 | else 157 | { 158 | // set default profile for current running renderer. 159 | 160 | bgfx::RendererType::Enum renderType = bgfx::getRendererType(); 161 | 162 | switch(renderType) 163 | { 164 | default: 165 | case bgfx::RendererType::Noop: //!< No rendering. 166 | break; 167 | case bgfx::RendererType::Direct3D9: //!< Direct3D 9.0 168 | { 169 | if(type == 'v') 170 | options.profile = "vs_3_0"; 171 | else if(type == 'f') 172 | options.profile = "ps_3_0"; 173 | else if(type == 'c') 174 | options.profile = "ps_3_0"; 175 | } 176 | break; 177 | case bgfx::RendererType::Direct3D11: //!< Direct3D 11.0 178 | { 179 | if(type == 'v') 180 | options.profile = "vs_4_0"; 181 | else if(type == 'f') 182 | options.profile = "ps_4_0"; 183 | else if(type == 'c') 184 | options.profile = "cs_5_0"; 185 | } 186 | break; 187 | case bgfx::RendererType::Direct3D12: //!< Direct3D 12.0 188 | { 189 | if(type == 'v') 190 | options.profile = "vs_5_0"; 191 | else if(type == 'f') 192 | options.profile = "ps_5_0"; 193 | else if(type == 'c') 194 | options.profile = "cs_5_0"; 195 | } 196 | case bgfx::RendererType::Gnm: //!< GNM 197 | break; 198 | case bgfx::RendererType::Metal: //!< Metal 199 | break; 200 | case bgfx::RendererType::OpenGLES: //!< OpenGL ES 2.0+ 201 | break; 202 | case bgfx::RendererType::OpenGL: //!< OpenGL 2.1+ 203 | { 204 | if(type == 'v' || type == 'f') 205 | options.profile = "120"; 206 | else if(type == 'c') 207 | options.profile = "430"; 208 | } 209 | break; 210 | case bgfx::RendererType::Vulkan: //!< Vulkan 211 | break; 212 | }; 213 | } 214 | 215 | 216 | // include current dir 217 | std::string dir; 218 | { 219 | bx::FilePath fp(filePath); 220 | bx::StringView path(fp.getPath() ); 221 | 222 | dir.assign(path.getPtr(), path.getTerm() ); 223 | options.includeDirs.push_back(dir); 224 | } 225 | 226 | // set defines 227 | while (NULL != defines 228 | && '\0' != *defines) 229 | { 230 | defines = bx::strLTrimSpace(defines).getPtr(); 231 | bx::StringView eol = bx::strFind(defines, ';'); 232 | std::string define(defines, eol.getPtr() ); 233 | options.defines.push_back(define.c_str() ); 234 | defines = ';' == *eol.getPtr() ? eol.getPtr()+1 : eol.getPtr(); 235 | } 236 | 237 | 238 | // set varyingdef 239 | std::string defaultVarying = dir + "varying.def.sc"; 240 | const char* varyingdef = varyingPath ? varyingPath : defaultVarying.c_str(); 241 | bgfx::File attribdef(varyingdef); 242 | const char* parse = attribdef.getData(); 243 | if (NULL != parse 244 | && *parse != '\0') 245 | { 246 | options.dependencies.push_back(varyingdef); 247 | } 248 | else 249 | { 250 | fprintf(stderr, "ERROR: Failed to parse varying def file: \"%s\" No input/output semantics will be generated in the code!\n", varyingdef); 251 | return nullptr; 252 | } 253 | 254 | 255 | 256 | // read shader source file 257 | bx::FileReader reader; 258 | if (!bx::open(&reader, filePath) ) 259 | { 260 | fprintf(stderr, "Unable to open file '%s'.\n", filePath); 261 | return nullptr; 262 | } 263 | 264 | // add padding 265 | const size_t padding = 16384; 266 | uint32_t size = (uint32_t)bx::getSize(&reader); 267 | char* data = new char[size+padding+1]; 268 | size = (uint32_t)bx::read(&reader, data, size); 269 | 270 | if (data[0] == '\xef' 271 | && data[1] == '\xbb' 272 | && data[2] == '\xbf') 273 | { 274 | bx::memMove(data, &data[3], size-3); 275 | size -= 3; 276 | } 277 | 278 | // Compiler generates "error X3000: syntax error: unexpected end of file" 279 | // if input doesn't have empty line at EOF. 280 | data[size] = '\n'; 281 | bx::memSet(&data[size+1], 0, padding); 282 | bx::close(&reader); 283 | 284 | 285 | std::string commandLineComment = "// shaderc command line:\n"; 286 | 287 | // compile shader. 288 | 289 | BufferWriter writer; 290 | if ( bgfx::compileShader(attribdef.getData(), commandLineComment.c_str(), data, size, options, &writer) ) 291 | { 292 | // this will copy the compiled shader data to a memory block and return mem ptr 293 | return writer.finalize(); 294 | } 295 | 296 | return nullptr; 297 | } 298 | 299 | 300 | 301 | 302 | 303 | void help(const char* _error = NULL) 304 | { 305 | if (NULL != _error) 306 | { 307 | fprintf(stderr, "brtshaderc error:\n%s\n\n", _error); 308 | } 309 | } 310 | 311 | bx::StringView baseName(const char* _filePath) 312 | { 313 | bx::FilePath fp(_filePath); 314 | return bx::strFind(_filePath, fp.getBaseName() ); 315 | } 316 | 317 | int compileShader(int _argc, const char* _argv[], bx::FileWriter* _writer) 318 | { 319 | bx::CommandLine cmdLine(_argc, _argv); 320 | 321 | if (cmdLine.hasArg('v', "version") ) 322 | { 323 | fprintf(stderr 324 | , "shaderc, bgfx shader compiler tool, version %d.%d.%d.\n" 325 | , BGFX_SHADERC_VERSION_MAJOR 326 | , BGFX_SHADERC_VERSION_MINOR 327 | , BGFX_API_VERSION 328 | ); 329 | return bx::kExitSuccess; 330 | } 331 | 332 | if (cmdLine.hasArg('h', "help") ) 333 | { 334 | help(); 335 | return bx::kExitFailure; 336 | } 337 | 338 | //@@g_verbose = cmdLine.hasArg("verbose"); 339 | 340 | const char* filePath = cmdLine.findOption('f'); 341 | if (NULL == filePath) 342 | { 343 | help("Shader file name must be specified."); 344 | return bx::kExitFailure; 345 | } 346 | 347 | const char* outFilePath = cmdLine.findOption('o'); 348 | ///@if (NULL == outFilePath) 349 | ///@{ 350 | ///@ help("Output file name must be specified."); 351 | ///@ return bx::kExitFailure; 352 | ///@} 353 | 354 | const char* type = cmdLine.findOption('\0', "type"); 355 | if (NULL == type) 356 | { 357 | help("Must specify shader type."); 358 | return bx::kExitFailure; 359 | } 360 | 361 | Options options; 362 | options.inputFilePath = filePath; 363 | options.outputFilePath = outFilePath; 364 | options.shaderType = bx::toLower(type[0]); 365 | 366 | options.disasm = cmdLine.hasArg('\0', "disasm"); 367 | 368 | const char* platform = cmdLine.findOption('\0', "platform"); 369 | if (NULL == platform) 370 | { 371 | platform = ""; 372 | } 373 | 374 | options.platform = platform; 375 | 376 | options.raw = cmdLine.hasArg('\0', "raw"); 377 | 378 | const char* profile = cmdLine.findOption('p', "profile"); 379 | 380 | if ( NULL != profile) 381 | { 382 | options.profile = profile; 383 | } 384 | 385 | { 386 | options.debugInformation = cmdLine.hasArg('\0', "debug"); 387 | options.avoidFlowControl = cmdLine.hasArg('\0', "avoid-flow-control"); 388 | options.noPreshader = cmdLine.hasArg('\0', "no-preshader"); 389 | options.partialPrecision = cmdLine.hasArg('\0', "partial-precision"); 390 | options.preferFlowControl = cmdLine.hasArg('\0', "prefer-flow-control"); 391 | options.backwardsCompatibility = cmdLine.hasArg('\0', "backwards-compatibility"); 392 | options.warningsAreErrors = cmdLine.hasArg('\0', "Werror"); 393 | options.keepIntermediate = cmdLine.hasArg('\0', "keep-intermediate"); 394 | 395 | uint32_t optimization = 3; 396 | if (cmdLine.hasArg(optimization, 'O') ) 397 | { 398 | options.optimize = true; 399 | options.optimizationLevel = optimization; 400 | } 401 | } 402 | 403 | ///@const char* bin2c = NULL; 404 | ///@if (cmdLine.hasArg("bin2c") ) 405 | ///@{ 406 | ///@ bin2c = cmdLine.findOption("bin2c"); 407 | ///@ if (NULL == bin2c) 408 | ///@ { 409 | ///@ bin2c = baseName(outFilePath); 410 | ///@ uint32_t len = (uint32_t)bx::strLen(bin2c); 411 | ///@ char* temp = (char*)alloca(len+1); 412 | ///@ for (char *out = temp; *bin2c != '\0';) 413 | ///@ { 414 | ///@ char ch = *bin2c++; 415 | ///@ if (isalnum(ch) ) 416 | ///@ { 417 | ///@ *out++ = ch; 418 | ///@ } 419 | ///@ else 420 | ///@ { 421 | ///@ *out++ = '_'; 422 | ///@ } 423 | ///@ } 424 | ///@ temp[len] = '\0'; 425 | ///@ 426 | ///@ bin2c = temp; 427 | ///@ } 428 | ///@} 429 | 430 | options.depends = cmdLine.hasArg("depends"); 431 | options.preprocessOnly = cmdLine.hasArg("preprocess"); 432 | const char* includeDir = cmdLine.findOption('i'); 433 | 434 | BX_TRACE("depends: %d", options.depends); 435 | BX_TRACE("preprocessOnly: %d", options.preprocessOnly); 436 | BX_TRACE("includeDir: %s", includeDir); 437 | 438 | for (int ii = 1; NULL != includeDir; ++ii) 439 | { 440 | options.includeDirs.push_back(includeDir); 441 | includeDir = cmdLine.findOption(ii, 'i'); 442 | } 443 | 444 | std::string dir; 445 | { 446 | bx::FilePath fp(filePath); 447 | bx::StringView path(fp.getPath() ); 448 | 449 | dir.assign(path.getPtr(), path.getTerm() ); 450 | options.includeDirs.push_back(dir); 451 | } 452 | 453 | const char* defines = cmdLine.findOption("define"); 454 | while (NULL != defines 455 | && '\0' != *defines) 456 | { 457 | defines = bx::strLTrimSpace(defines).getPtr(); 458 | bx::StringView eol = bx::strFind(defines, ';'); 459 | std::string define(defines, eol.getPtr() ); 460 | options.defines.push_back(define.c_str() ); 461 | defines = ';' == *eol.getPtr() ? eol.getPtr()+1 : eol.getPtr(); 462 | } 463 | 464 | std::string commandLineComment = "// shaderc command line:\n//"; 465 | for (int32_t ii = 0, num = cmdLine.getNum(); ii < num; ++ii) 466 | { 467 | commandLineComment += " "; 468 | commandLineComment += cmdLine.get(ii); 469 | } 470 | commandLineComment += "\n\n"; 471 | 472 | bool compiled = false; 473 | 474 | bx::FileReader reader; 475 | if (!bx::open(&reader, filePath) ) 476 | { 477 | fprintf(stderr, "Unable to open file '%s'.\n", filePath); 478 | } 479 | else 480 | { 481 | std::string defaultVarying = dir + "varying.def.sc"; 482 | const char* varyingdef = cmdLine.findOption("varyingdef", defaultVarying.c_str() ); 483 | File attribdef(varyingdef); 484 | const char* parse = attribdef.getData(); 485 | if (NULL != parse 486 | && *parse != '\0') 487 | { 488 | options.dependencies.push_back(varyingdef); 489 | } 490 | else 491 | { 492 | fprintf(stderr, "ERROR: Failed to parse varying def file: \"%s\" No input/output semantics will be generated in the code!\n", varyingdef); 493 | } 494 | 495 | const size_t padding = 16384; 496 | uint32_t size = (uint32_t)bx::getSize(&reader); 497 | char* data = new char[size+padding+1]; 498 | size = (uint32_t)bx::read(&reader, data, size); 499 | 500 | if (data[0] == '\xef' 501 | && data[1] == '\xbb' 502 | && data[2] == '\xbf') 503 | { 504 | bx::memMove(data, &data[3], size-3); 505 | size -= 3; 506 | } 507 | 508 | // Compiler generates "error X3000: syntax error: unexpected end of file" 509 | // if input doesn't have empty line at EOF. 510 | data[size] = '\n'; 511 | bx::memSet(&data[size+1], 0, padding); 512 | bx::close(&reader); 513 | 514 | ///@bx::FileWriter* writer = NULL; 515 | bx::FileWriter* writer = _writer; 516 | 517 | ///@if (NULL != bin2c) 518 | ///@{ 519 | ///@ writer = new Bin2cWriter(bin2c); 520 | ///@} 521 | ///@else 522 | ///@{ 523 | ///@ writer = new bx::FileWriter; 524 | ///@} 525 | 526 | if (!bx::open(writer, outFilePath) ) 527 | { 528 | fprintf(stderr, "Unable to open output file '%s'.", outFilePath); 529 | return bx::kExitFailure; 530 | } 531 | 532 | compiled = compileShader(attribdef.getData(), commandLineComment.c_str(), data, size, options, writer); 533 | 534 | bx::close(writer); 535 | ///@delete writer; 536 | } 537 | } 538 | 539 | const bgfx::Memory* compileShader(int argc, const char* argv[]) 540 | { 541 | BufferWriter writer; 542 | int error = compileShader(argc, argv, &writer); 543 | 544 | if(!error) 545 | { 546 | return writer.finalize(); 547 | } 548 | 549 | return nullptr; 550 | } 551 | 552 | } 553 | 554 | // restore previous defines BX_TRACE BX_WARN and BX_CHECK 555 | #include "../../bgfx/tools/shaderc/shaderc.h" 556 | -------------------------------------------------------------------------------- /examples/01-cubes-brtshaderc/common/bgfx_shader.sh: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011-2018 Branimir Karadzic. All rights reserved. 3 | * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause 4 | */ 5 | 6 | #ifndef BGFX_SHADER_H_HEADER_GUARD 7 | #define BGFX_SHADER_H_HEADER_GUARD 8 | 9 | #if !defined(BGFX_CONFIG_MAX_BONES) 10 | # define BGFX_CONFIG_MAX_BONES 32 11 | #endif // !defined(BGFX_CONFIG_MAX_BONES) 12 | 13 | #ifndef __cplusplus 14 | 15 | #if BGFX_SHADER_LANGUAGE_HLSL > 3 16 | # define BRANCH [branch] 17 | # define LOOP [loop] 18 | # define UNROLL [unroll] 19 | #else 20 | # define BRANCH 21 | # define LOOP 22 | # define UNROLL 23 | #endif // BGFX_SHADER_LANGUAGE_HLSL > 3 24 | 25 | #if BGFX_SHADER_LANGUAGE_HLSL > 3 && BGFX_SHADER_TYPE_FRAGMENT 26 | # define EARLY_DEPTH_STENCIL [earlydepthstencil] 27 | #else 28 | # define EARLY_DEPTH_STENCIL 29 | #endif // BGFX_SHADER_LANGUAGE_HLSL > 3 && BGFX_SHADER_TYPE_FRAGMENT 30 | 31 | #if BGFX_SHADER_LANGUAGE_GLSL 32 | # define ARRAY_BEGIN(_type, _name, _count) _type _name[_count] = _type[]( 33 | # define ARRAY_END() ) 34 | #else 35 | # define ARRAY_BEGIN(_type, _name, _count) _type _name[_count] = { 36 | # define ARRAY_END() } 37 | #endif // BGFX_SHADER_LANGUAGE_GLSL 38 | 39 | #if BGFX_SHADER_LANGUAGE_HLSL || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV 40 | # define CONST(_x) static const _x 41 | # define dFdx(_x) ddx(_x) 42 | # define dFdy(_y) ddy(-_y) 43 | # define inversesqrt(_x) rsqrt(_x) 44 | # define fract(_x) frac(_x) 45 | 46 | # define bvec2 bool2 47 | # define bvec3 bool3 48 | # define bvec4 bool4 49 | 50 | # if BGFX_SHADER_LANGUAGE_HLSL > 4 51 | # define REGISTER(_type, _reg) register(_type[_reg]) 52 | # else 53 | # define REGISTER(_type, _reg) register(_type ## _reg) 54 | # endif // BGFX_SHADER_LANGUAGE_HLSL 55 | 56 | # if BGFX_SHADER_LANGUAGE_HLSL > 3 || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV 57 | # if BGFX_SHADER_LANGUAGE_HLSL > 4 || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV 58 | # define dFdxCoarse(_x) ddx_coarse(_x) 59 | # define dFdxFine(_x) ddx_fine(_x) 60 | # define dFdyCoarse(_y) ddy_coarse(-_y) 61 | # define dFdyFine(_y) ddy_fine(-_y) 62 | # endif // BGFX_SHADER_LANGUAGE_HLSL > 4 63 | 64 | # if BGFX_SHADER_LANGUAGE_HLSL 65 | float intBitsToFloat(int _x) { return asfloat(_x); } 66 | vec2 intBitsToFloat(uint2 _x) { return asfloat(_x); } 67 | vec3 intBitsToFloat(uint3 _x) { return asfloat(_x); } 68 | vec4 intBitsToFloat(uint4 _x) { return asfloat(_x); } 69 | # endif // BGFX_SHADER_LANGUAGE_HLSL 70 | 71 | float uintBitsToFloat(uint _x) { return asfloat(_x); } 72 | vec2 uintBitsToFloat(uint2 _x) { return asfloat(_x); } 73 | vec3 uintBitsToFloat(uint3 _x) { return asfloat(_x); } 74 | vec4 uintBitsToFloat(uint4 _x) { return asfloat(_x); } 75 | 76 | uint floatBitsToUint(float _x) { return asuint(_x); } 77 | uvec2 floatBitsToUint(vec2 _x) { return asuint(_x); } 78 | uvec3 floatBitsToUint(vec3 _x) { return asuint(_x); } 79 | uvec4 floatBitsToUint(vec4 _x) { return asuint(_x); } 80 | 81 | int floatBitsToInt(float _x) { return asint(_x); } 82 | ivec2 floatBitsToInt(vec2 _x) { return asint(_x); } 83 | ivec3 floatBitsToInt(vec3 _x) { return asint(_x); } 84 | ivec4 floatBitsToInt(vec4 _x) { return asint(_x); } 85 | 86 | uint bitfieldReverse(uint _x) { return reversebits(_x); } 87 | uint2 bitfieldReverse(uint2 _x) { return reversebits(_x); } 88 | uint3 bitfieldReverse(uint3 _x) { return reversebits(_x); } 89 | uint4 bitfieldReverse(uint4 _x) { return reversebits(_x); } 90 | 91 | # if !BGFX_SHADER_LANGUAGE_SPIRV 92 | uint packHalf2x16(vec2 _x) 93 | { 94 | return (f32tof16(_x.y)<<16) | f32tof16(_x.x); 95 | } 96 | 97 | vec2 unpackHalf2x16(uint _x) 98 | { 99 | return vec2(f16tof32(_x & 0xffff), f16tof32(_x >> 16) ); 100 | } 101 | # endif // !BGFX_SHADER_LANGUAGE_SPIRV 102 | 103 | struct BgfxSampler2D 104 | { 105 | SamplerState m_sampler; 106 | Texture2D m_texture; 107 | }; 108 | 109 | struct BgfxISampler2D 110 | { 111 | Texture2D m_texture; 112 | }; 113 | 114 | struct BgfxUSampler2D 115 | { 116 | Texture2D m_texture; 117 | }; 118 | 119 | struct BgfxSampler2DArray 120 | { 121 | SamplerState m_sampler; 122 | Texture2DArray m_texture; 123 | }; 124 | 125 | struct BgfxSampler2DShadow 126 | { 127 | SamplerComparisonState m_sampler; 128 | Texture2D m_texture; 129 | }; 130 | 131 | struct BgfxSampler3D 132 | { 133 | SamplerState m_sampler; 134 | Texture3D m_texture; 135 | }; 136 | 137 | struct BgfxISampler3D 138 | { 139 | Texture3D m_texture; 140 | }; 141 | 142 | struct BgfxUSampler3D 143 | { 144 | Texture3D m_texture; 145 | }; 146 | 147 | struct BgfxSamplerCube 148 | { 149 | SamplerState m_sampler; 150 | TextureCube m_texture; 151 | }; 152 | 153 | struct BgfxSampler2DMS 154 | { 155 | Texture2DMS m_texture; 156 | }; 157 | 158 | vec4 bgfxTexture2D(BgfxSampler2D _sampler, vec2 _coord) 159 | { 160 | return _sampler.m_texture.Sample(_sampler.m_sampler, _coord); 161 | } 162 | 163 | vec4 bgfxTexture2DLod(BgfxSampler2D _sampler, vec2 _coord, float _level) 164 | { 165 | return _sampler.m_texture.SampleLevel(_sampler.m_sampler, _coord, _level); 166 | } 167 | 168 | vec4 bgfxTexture2DProj(BgfxSampler2D _sampler, vec3 _coord) 169 | { 170 | vec2 coord = _coord.xy * rcp(_coord.z); 171 | return _sampler.m_texture.Sample(_sampler.m_sampler, coord); 172 | } 173 | 174 | vec4 bgfxTexture2DProj(BgfxSampler2D _sampler, vec4 _coord) 175 | { 176 | vec2 coord = _coord.xy * rcp(_coord.w); 177 | return _sampler.m_texture.Sample(_sampler.m_sampler, coord); 178 | } 179 | 180 | vec4 bgfxTexture2DGrad(BgfxSampler2D _sampler, vec2 _coord, vec2 _dPdx, vec2 _dPdy) 181 | { 182 | return _sampler.m_texture.SampleGrad(_sampler.m_sampler, _coord, _dPdx, _dPdy); 183 | } 184 | 185 | vec4 bgfxTexture2DArray(BgfxSampler2DArray _sampler, vec3 _coord) 186 | { 187 | return _sampler.m_texture.Sample(_sampler.m_sampler, _coord); 188 | } 189 | 190 | vec4 bgfxTexture2DArrayLod(BgfxSampler2DArray _sampler, vec3 _coord, float _lod) 191 | { 192 | return _sampler.m_texture.SampleLevel(_sampler.m_sampler, _coord, _lod); 193 | } 194 | 195 | float bgfxShadow2D(BgfxSampler2DShadow _sampler, vec3 _coord) 196 | { 197 | return _sampler.m_texture.SampleCmpLevelZero(_sampler.m_sampler, _coord.xy, _coord.z); 198 | } 199 | 200 | float bgfxShadow2DProj(BgfxSampler2DShadow _sampler, vec4 _coord) 201 | { 202 | vec3 coord = _coord.xyz * rcp(_coord.w); 203 | return _sampler.m_texture.SampleCmpLevelZero(_sampler.m_sampler, coord.xy, coord.z); 204 | } 205 | 206 | vec4 bgfxTexture3D(BgfxSampler3D _sampler, vec3 _coord) 207 | { 208 | return _sampler.m_texture.Sample(_sampler.m_sampler, _coord); 209 | } 210 | 211 | vec4 bgfxTexture3DLod(BgfxSampler3D _sampler, vec3 _coord, float _level) 212 | { 213 | return _sampler.m_texture.SampleLevel(_sampler.m_sampler, _coord, _level); 214 | } 215 | 216 | ivec4 bgfxTexture3D(BgfxISampler3D _sampler, vec3 _coord) 217 | { 218 | uvec3 size; 219 | _sampler.m_texture.GetDimensions(size.x, size.y, size.z); 220 | return _sampler.m_texture.Load(ivec4(_coord * size, 0) ); 221 | } 222 | 223 | uvec4 bgfxTexture3D(BgfxUSampler3D _sampler, vec3 _coord) 224 | { 225 | uvec3 size; 226 | _sampler.m_texture.GetDimensions(size.x, size.y, size.z); 227 | return _sampler.m_texture.Load(ivec4(_coord * size, 0) ); 228 | } 229 | 230 | vec4 bgfxTextureCube(BgfxSamplerCube _sampler, vec3 _coord) 231 | { 232 | return _sampler.m_texture.Sample(_sampler.m_sampler, _coord); 233 | } 234 | 235 | vec4 bgfxTextureCubeLod(BgfxSamplerCube _sampler, vec3 _coord, float _level) 236 | { 237 | return _sampler.m_texture.SampleLevel(_sampler.m_sampler, _coord, _level); 238 | } 239 | 240 | vec4 bgfxTexelFetch(BgfxSampler2D _sampler, ivec2 _coord, int _lod) 241 | { 242 | return _sampler.m_texture.Load(ivec3(_coord, _lod) ); 243 | } 244 | 245 | vec2 bgfxTextureSize(BgfxSampler2D _sampler, int _lod) 246 | { 247 | vec2 result; 248 | _sampler.m_texture.GetDimensions(result.x, result.y); 249 | return result; 250 | } 251 | 252 | ivec4 bgfxTexelFetch(BgfxISampler2D _sampler, ivec2 _coord, int _lod) 253 | { 254 | return _sampler.m_texture.Load(ivec3(_coord, _lod) ); 255 | } 256 | 257 | uvec4 bgfxTexelFetch(BgfxUSampler2D _sampler, ivec2 _coord, int _lod) 258 | { 259 | return _sampler.m_texture.Load(ivec3(_coord, _lod) ); 260 | } 261 | 262 | vec4 bgfxTexelFetch(BgfxSampler2DMS _sampler, ivec2 _coord, int _sampleIdx) 263 | { 264 | return _sampler.m_texture.Load(_coord, _sampleIdx); 265 | } 266 | 267 | vec4 bgfxTexelFetch(BgfxSampler3D _sampler, ivec3 _coord, int _lod) 268 | { 269 | return _sampler.m_texture.Load(ivec4(_coord, _lod) ); 270 | } 271 | 272 | vec3 bgfxTextureSize(BgfxSampler3D _sampler, int _lod) 273 | { 274 | vec3 result; 275 | _sampler.m_texture.GetDimensions(result.x, result.y, result.z); 276 | return result; 277 | } 278 | 279 | # define SAMPLER2D(_name, _reg) \ 280 | uniform SamplerState _name ## Sampler : REGISTER(s, _reg); \ 281 | uniform Texture2D _name ## Texture : REGISTER(t, _reg); \ 282 | static BgfxSampler2D _name = { _name ## Sampler, _name ## Texture } 283 | # define ISAMPLER2D(_name, _reg) \ 284 | uniform Texture2D _name ## Texture : REGISTER(t, _reg); \ 285 | static BgfxISampler2D _name = { _name ## Texture } 286 | # define USAMPLER2D(_name, _reg) \ 287 | uniform Texture2D _name ## Texture : REGISTER(t, _reg); \ 288 | static BgfxUSampler2D _name = { _name ## Texture } 289 | # define sampler2D BgfxSampler2D 290 | # define texture2D(_sampler, _coord) bgfxTexture2D(_sampler, _coord) 291 | # define texture2DLod(_sampler, _coord, _level) bgfxTexture2DLod(_sampler, _coord, _level) 292 | # define texture2DProj(_sampler, _coord) bgfxTexture2DProj(_sampler, _coord) 293 | # define texture2DGrad(_sampler, _coord, _dPdx, _dPdy) bgfxTexture2DGrad(_sampler, _coord, _dPdx, _dPdy) 294 | 295 | # define SAMPLER2DARRAY(_name, _reg) \ 296 | uniform SamplerState _name ## Sampler : REGISTER(s, _reg); \ 297 | uniform Texture2DArray _name ## Texture : REGISTER(t, _reg); \ 298 | static BgfxSampler2DArray _name = { _name ## Sampler, _name ## Texture } 299 | # define sampler2DArray BgfxSampler2DArray 300 | # define texture2DArray(_sampler, _coord) bgfxTexture2DArray(_sampler, _coord) 301 | # define texture2DArrayLod(_sampler, _coord, _lod) bgfxTexture2DArrayLod(_sampler, _coord, _lod) 302 | 303 | # define SAMPLER2DMS(_name, _reg) \ 304 | uniform Texture2DMS _name ## Texture : REGISTER(t, _reg); \ 305 | static BgfxSampler2DMS _name = { _name ## Texture } 306 | # define sampler2DMS BgfxSampler2DMS 307 | 308 | # define SAMPLER2DSHADOW(_name, _reg) \ 309 | uniform SamplerComparisonState _name ## SamplerComparison : REGISTER(s, _reg); \ 310 | uniform Texture2D _name ## Texture : REGISTER(t, _reg); \ 311 | static BgfxSampler2DShadow _name = { _name ## SamplerComparison, _name ## Texture } 312 | # define sampler2DShadow BgfxSampler2DShadow 313 | # define shadow2D(_sampler, _coord) bgfxShadow2D(_sampler, _coord) 314 | # define shadow2DProj(_sampler, _coord) bgfxShadow2DProj(_sampler, _coord) 315 | 316 | # define SAMPLER3D(_name, _reg) \ 317 | uniform SamplerState _name ## Sampler : REGISTER(s, _reg); \ 318 | uniform Texture3D _name ## Texture : REGISTER(t, _reg); \ 319 | static BgfxSampler3D _name = { _name ## Sampler, _name ## Texture } 320 | # define ISAMPLER3D(_name, _reg) \ 321 | uniform Texture3D _name ## Texture : REGISTER(t, _reg); \ 322 | static BgfxISampler3D _name = { _name ## Texture } 323 | # define USAMPLER3D(_name, _reg) \ 324 | uniform Texture3D _name ## Texture : REGISTER(t, _reg); \ 325 | static BgfxUSampler3D _name = { _name ## Texture } 326 | # define sampler3D BgfxSampler3D 327 | # define texture3D(_sampler, _coord) bgfxTexture3D(_sampler, _coord) 328 | # define texture3DLod(_sampler, _coord, _level) bgfxTexture3DLod(_sampler, _coord, _level) 329 | 330 | # define SAMPLERCUBE(_name, _reg) \ 331 | uniform SamplerState _name ## Sampler : REGISTER(s, _reg); \ 332 | uniform TextureCube _name ## Texture : REGISTER(t, _reg); \ 333 | static BgfxSamplerCube _name = { _name ## Sampler, _name ## Texture } 334 | # define samplerCube BgfxSamplerCube 335 | # define textureCube(_sampler, _coord) bgfxTextureCube(_sampler, _coord) 336 | # define textureCubeLod(_sampler, _coord, _level) bgfxTextureCubeLod(_sampler, _coord, _level) 337 | 338 | # define texelFetch(_sampler, _coord, _lod) bgfxTexelFetch(_sampler, _coord, _lod) 339 | # define textureSize(_sampler, _lod) bgfxTextureSize(_sampler, _lod) 340 | # else 341 | 342 | # define sampler2DShadow sampler2D 343 | 344 | vec4 bgfxTexture2DProj(sampler2D _sampler, vec3 _coord) 345 | { 346 | return tex2Dproj(_sampler, vec4(_coord.xy, 0.0, _coord.z) ); 347 | } 348 | 349 | vec4 bgfxTexture2DProj(sampler2D _sampler, vec4 _coord) 350 | { 351 | return tex2Dproj(_sampler, _coord); 352 | } 353 | 354 | float bgfxShadow2D(sampler2DShadow _sampler, vec3 _coord) 355 | { 356 | #if 0 357 | float occluder = tex2D(_sampler, _coord.xy).x; 358 | return step(_coord.z, occluder); 359 | #else 360 | return tex2Dproj(_sampler, vec4(_coord.xy, _coord.z, 1.0) ).x; 361 | #endif // 0 362 | } 363 | 364 | float bgfxShadow2DProj(sampler2DShadow _sampler, vec4 _coord) 365 | { 366 | #if 0 367 | vec3 coord = _coord.xyz * rcp(_coord.w); 368 | float occluder = tex2D(_sampler, coord.xy).x; 369 | return step(coord.z, occluder); 370 | #else 371 | return tex2Dproj(_sampler, _coord).x; 372 | #endif // 0 373 | } 374 | 375 | # define SAMPLER2D(_name, _reg) uniform sampler2D _name : REGISTER(s, _reg) 376 | # define SAMPLER2DMS(_name, _reg) uniform sampler2DMS _name : REGISTER(s, _reg) 377 | # define texture2D(_sampler, _coord) tex2D(_sampler, _coord) 378 | # define texture2DProj(_sampler, _coord) bgfxTexture2DProj(_sampler, _coord) 379 | 380 | # define SAMPLER2DARRAY(_name, _reg) SAMPLER2D(_name, _reg) 381 | # define texture2DArray(_sampler, _coord) texture2D(_sampler, (_coord).xy) 382 | # define texture2DArrayLod(_sampler, _coord, _lod) texture2DLod(_sampler, _coord, _lod) 383 | 384 | # define SAMPLER2DSHADOW(_name, _reg) uniform sampler2DShadow _name : REGISTER(s, _reg) 385 | # define shadow2D(_sampler, _coord) bgfxShadow2D(_sampler, _coord) 386 | # define shadow2DProj(_sampler, _coord) bgfxShadow2DProj(_sampler, _coord) 387 | 388 | # define SAMPLER3D(_name, _reg) uniform sampler3D _name : REGISTER(s, _reg) 389 | # define texture3D(_sampler, _coord) tex3D(_sampler, _coord) 390 | 391 | # define SAMPLERCUBE(_name, _reg) uniform samplerCUBE _name : REGISTER(s, _reg) 392 | # define textureCube(_sampler, _coord) texCUBE(_sampler, _coord) 393 | 394 | # if BGFX_SHADER_LANGUAGE_HLSL == 2 395 | # define texture2DLod(_sampler, _coord, _level) tex2D(_sampler, (_coord).xy) 396 | # define texture2DGrad(_sampler, _coord, _dPdx, _dPdy) tex2D(_sampler, _coord) 397 | # define texture3DLod(_sampler, _coord, _level) tex3D(_sampler, (_coord).xyz) 398 | # define textureCubeLod(_sampler, _coord, _level) texCUBE(_sampler, (_coord).xyz) 399 | # else 400 | # define texture2DLod(_sampler, _coord, _level) tex2Dlod(_sampler, vec4( (_coord).xy, 0.0, _level) ) 401 | # define texture2DGrad(_sampler, _coord, _dPdx, _dPdy) tex2Dgrad(_sampler, _coord, _dPdx, _dPdy) 402 | # define texture3DLod(_sampler, _coord, _level) tex3Dlod(_sampler, vec4( (_coord).xyz, _level) ) 403 | # define textureCubeLod(_sampler, _coord, _level) texCUBElod(_sampler, vec4( (_coord).xyz, _level) ) 404 | # endif // BGFX_SHADER_LANGUAGE_HLSL == 2 405 | 406 | # endif // BGFX_SHADER_LANGUAGE_HLSL > 3 407 | 408 | vec3 instMul(vec3 _vec, mat3 _mtx) { return mul(_mtx, _vec); } 409 | vec3 instMul(mat3 _mtx, vec3 _vec) { return mul(_vec, _mtx); } 410 | vec4 instMul(vec4 _vec, mat4 _mtx) { return mul(_mtx, _vec); } 411 | vec4 instMul(mat4 _mtx, vec4 _vec) { return mul(_vec, _mtx); } 412 | 413 | bvec2 lessThan(vec2 _a, vec2 _b) { return _a < _b; } 414 | bvec3 lessThan(vec3 _a, vec3 _b) { return _a < _b; } 415 | bvec4 lessThan(vec4 _a, vec4 _b) { return _a < _b; } 416 | 417 | bvec2 lessThanEqual(vec2 _a, vec2 _b) { return _a <= _b; } 418 | bvec3 lessThanEqual(vec3 _a, vec3 _b) { return _a <= _b; } 419 | bvec4 lessThanEqual(vec4 _a, vec4 _b) { return _a <= _b; } 420 | 421 | bvec2 greaterThan(vec2 _a, vec2 _b) { return _a > _b; } 422 | bvec3 greaterThan(vec3 _a, vec3 _b) { return _a > _b; } 423 | bvec4 greaterThan(vec4 _a, vec4 _b) { return _a > _b; } 424 | 425 | bvec2 greaterThanEqual(vec2 _a, vec2 _b) { return _a >= _b; } 426 | bvec3 greaterThanEqual(vec3 _a, vec3 _b) { return _a >= _b; } 427 | bvec4 greaterThanEqual(vec4 _a, vec4 _b) { return _a >= _b; } 428 | 429 | bvec2 notEqual(vec2 _a, vec2 _b) { return _a != _b; } 430 | bvec3 notEqual(vec3 _a, vec3 _b) { return _a != _b; } 431 | bvec4 notEqual(vec4 _a, vec4 _b) { return _a != _b; } 432 | 433 | bvec2 equal(vec2 _a, vec2 _b) { return _a == _b; } 434 | bvec3 equal(vec3 _a, vec3 _b) { return _a == _b; } 435 | bvec4 equal(vec4 _a, vec4 _b) { return _a == _b; } 436 | 437 | float mix(float _a, float _b, float _t) { return lerp(_a, _b, _t); } 438 | vec2 mix(vec2 _a, vec2 _b, vec2 _t) { return lerp(_a, _b, _t); } 439 | vec3 mix(vec3 _a, vec3 _b, vec3 _t) { return lerp(_a, _b, _t); } 440 | vec4 mix(vec4 _a, vec4 _b, vec4 _t) { return lerp(_a, _b, _t); } 441 | 442 | float mod(float _a, float _b) { return _a - _b * floor(_a / _b); } 443 | vec2 mod(vec2 _a, vec2 _b) { return _a - _b * floor(_a / _b); } 444 | vec3 mod(vec3 _a, vec3 _b) { return _a - _b * floor(_a / _b); } 445 | vec4 mod(vec4 _a, vec4 _b) { return _a - _b * floor(_a / _b); } 446 | 447 | #else 448 | # define CONST(_x) const _x 449 | # define atan2(_x, _y) atan(_x, _y) 450 | # define mul(_a, _b) ( (_a) * (_b) ) 451 | # define saturate(_x) clamp(_x, 0.0, 1.0) 452 | # define SAMPLER2D(_name, _reg) uniform sampler2D _name 453 | # define SAMPLER2DMS(_name, _reg) uniform sampler2DMS _name 454 | # define SAMPLER3D(_name, _reg) uniform sampler3D _name 455 | # define SAMPLERCUBE(_name, _reg) uniform samplerCube _name 456 | # define SAMPLER2DSHADOW(_name, _reg) uniform sampler2DShadow _name 457 | 458 | # define SAMPLER2DARRAY(_name, _reg) uniform sampler2DArray _name 459 | # define SAMPLER2DMSARRAY(_name, _reg) uniform sampler2DMSArray _name 460 | # define SAMPLERCUBEARRAY(_name, _reg) uniform samplerCubeArray _name 461 | # define SAMPLER2DARRAYSHADOW(_name, _reg) uniform sampler2DArrayShadow _name 462 | 463 | # if BGFX_SHADER_LANGUAGE_GLSL >= 130 464 | # define ISAMPLER2D(_name, _reg) uniform isampler2D _name 465 | # define USAMPLER2D(_name, _reg) uniform usampler2D _name 466 | # define ISAMPLER3D(_name, _reg) uniform isampler3D _name 467 | # define USAMPLER3D(_name, _reg) uniform usampler3D _name 468 | 469 | # define texture2D(_sampler, _coord) texture(_sampler, _coord) 470 | # define texture2DArray(_sampler, _coord) texture(_sampler, _coord) 471 | # define texture3D(_sampler, _coord) texture(_sampler, _coord) 472 | # endif // BGFX_SHADER_LANGUAGE_GLSL >= 130 473 | 474 | vec3 instMul(vec3 _vec, mat3 _mtx) { return mul(_vec, _mtx); } 475 | vec3 instMul(mat3 _mtx, vec3 _vec) { return mul(_mtx, _vec); } 476 | vec4 instMul(vec4 _vec, mat4 _mtx) { return mul(_vec, _mtx); } 477 | vec4 instMul(mat4 _mtx, vec4 _vec) { return mul(_mtx, _vec); } 478 | 479 | float rcp(float _a) { return 1.0/_a; } 480 | vec2 rcp(vec2 _a) { return vec2(1.0)/_a; } 481 | vec3 rcp(vec3 _a) { return vec3(1.0)/_a; } 482 | vec4 rcp(vec4 _a) { return vec4(1.0)/_a; } 483 | #endif // BGFX_SHADER_LANGUAGE_* 484 | 485 | vec2 vec2_splat(float _x) { return vec2(_x, _x); } 486 | vec3 vec3_splat(float _x) { return vec3(_x, _x, _x); } 487 | vec4 vec4_splat(float _x) { return vec4(_x, _x, _x, _x); } 488 | 489 | #if BGFX_SHADER_LANGUAGE_GLSL >= 130 || BGFX_SHADER_LANGUAGE_HLSL || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV 490 | uvec2 uvec2_splat(uint _x) { return uvec2(_x, _x); } 491 | uvec3 uvec3_splat(uint _x) { return uvec3(_x, _x, _x); } 492 | uvec4 uvec4_splat(uint _x) { return uvec4(_x, _x, _x, _x); } 493 | #endif // BGFX_SHADER_LANGUAGE_GLSL >= 130 || BGFX_SHADER_LANGUAGE_HLSL || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV 494 | 495 | mat4 mtxFromRows(vec4 _0, vec4 _1, vec4 _2, vec4 _3) 496 | { 497 | #if BGFX_SHADER_LANGUAGE_GLSL 498 | return transpose(mat4(_0, _1, _2, _3) ); 499 | #else 500 | return mat4(_0, _1, _2, _3); 501 | #endif // BGFX_SHADER_LANGUAGE_GLSL 502 | } 503 | 504 | mat4 mtxFromCols(vec4 _0, vec4 _1, vec4 _2, vec4 _3) 505 | { 506 | #if BGFX_SHADER_LANGUAGE_GLSL 507 | return mat4(_0, _1, _2, _3); 508 | #else 509 | return transpose(mat4(_0, _1, _2, _3) ); 510 | #endif // BGFX_SHADER_LANGUAGE_GLSL 511 | } 512 | 513 | uniform vec4 u_viewRect; 514 | uniform vec4 u_viewTexel; 515 | uniform mat4 u_view; 516 | uniform mat4 u_invView; 517 | uniform mat4 u_proj; 518 | uniform mat4 u_invProj; 519 | uniform mat4 u_viewProj; 520 | uniform mat4 u_invViewProj; 521 | uniform mat4 u_model[BGFX_CONFIG_MAX_BONES]; 522 | uniform mat4 u_modelView; 523 | uniform mat4 u_modelViewProj; 524 | uniform vec4 u_alphaRef4; 525 | #define u_alphaRef u_alphaRef4.x 526 | 527 | #endif // __cplusplus 528 | 529 | #endif // BGFX_SHADER_H_HEADER_GUARD 530 | --------------------------------------------------------------------------------