mame/3rdparty/bgfx/examples/41-tess/terrain_common.sh
Julian Sikorski 0837e7451a WIP: sync bgfx, bx and bimg with latest upstream (#5723)
* Sync with bgfx upstream revision b91d0b6

* Sync with bx upstream revision d60912b

* Sync with bimg upstream revision bd81f60

* Add astc-codec decoder

* Rename VertexDecl to VertexLayout

* Rename UniformType enum Int1 to Sampler.

* Add NVN stub

* Fix unused-const-variable error on macOS

* Drop redundant explicit language parameters
buildoptions_cpp are only applied to c++ files and buildoptions_objcpp are only
applied to objective c++ files. As such, hardcoding -x offers no benefit while
preventing overrides (such as one needed by 3rdparty/bgfx/src/renderer_vk.cpp on
macOS) from working.

* Re-introduce -x c++ in places where C code is compiled as C++ to prevent clang from throwing a warning

* Build bgfx as Objective-C++ on macOS
It is needed due to included headers

* Enable Direct3D12 and Vulkan bgfx rendering backends

* Enable building of spirv shaders

* Properly escape /c in cmd call

* Comment out dx12 bgfx renderer

* Honor VERBOSE setting during shaders build

* Only invert hlsl shader XYZ_TO_sRGB matrix for opengl

* Add spirv shaders

* OpenGL ES needs transposed matrix too

* Metal needs transposed matrix as well
2019-10-13 07:50:38 -04:00

100 lines
2.0 KiB
Bash

#include "bgfx_compute.sh"
#include "matrices.sh"
#include "isubd.sh"
#include "uniforms.sh"
BUFFER_RW(u_AtomicCounterBuffer, uint, 4);
BUFFER_RW(u_SubdBufferOut, uint, 1);
SAMPLER2D(u_DmapSampler, 0); // displacement map
SAMPLER2D(u_SmapSampler, 1); // slope map
// displacement map
float dmap(vec2 pos)
{
return (texture2DLod(u_DmapSampler, pos * 0.5 + 0.5, 0).x) * u_DmapFactor;
}
float distanceToLod(float z, float lodFactor)
{
// Note that we multiply the result by two because the triangles
// edge lengths decreases by half every two subdivision steps.
return -2.0 * log2(clamp(z * lodFactor, 0.0f, 1.0f));
}
float computeLod(vec3 c)
{
//displace
c.z += dmap(mtxGetColumn(u_invView, 3).xy);
vec3 cxf = mul(u_modelView, vec4(c.x, c.y, c.z, 1)).xyz;
float z = length(cxf);
return distanceToLod(z, u_LodFactor);
}
float computeLod(in vec4 v[3])
{
vec3 c = (v[1].xyz + v[2].xyz) / 2.0;
return computeLod(c);
}
float computeLod(in vec3 v[3])
{
vec3 c = (v[1].xyz + v[2].xyz) / 2.0;
return computeLod(c);
}
void writeKey(uint primID, uint key)
{
uint idx = 0;
atomicFetchAndAdd(u_AtomicCounterBuffer[0], 2, idx);
u_SubdBufferOut[idx] = primID;
u_SubdBufferOut[idx+1] = key;
}
void updateSubdBuffer(
uint primID
, uint key
, uint targetLod
, uint parentLod
, bool isVisible
)
{
// extract subdivision level associated to the key
uint keyLod = findMSB(key);
// update the key accordingly
if (/* subdivide ? */ keyLod < targetLod && !isLeafKey(key) && isVisible)
{
uint children[2]; childrenKeys(key, children);
writeKey(primID, children[0]);
writeKey(primID, children[1]);
}
else if (/* keep ? */ keyLod < (parentLod + 1) && isVisible)
{
writeKey(primID, key);
}
else /* merge ? */
{
if (/* is root ? */isRootKey(key))
{
writeKey(primID, key);
}
else if (/* is zero child ? */isChildZeroKey(key)) {
writeKey(primID, parentKey(key));
}
}
}
void updateSubdBuffer(uint primID, uint key, uint targetLod, uint parentLod)
{
updateSubdBuffer(primID, key, targetLod, parentLod, true);
}