Skip to content

Commit

Permalink
[Emscripten] Fixed main thread fetch request in SLGLShader
Browse files Browse the repository at this point in the history
  • Loading branch information
chnoblouch committed May 30, 2024
1 parent 98168c0 commit 479083d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 83 deletions.
122 changes: 62 additions & 60 deletions modules/sl/source/gl/SLGLProgramGenerated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,24 +166,24 @@ const string vertOutput_PS_struct_texNum = R"(
const string vertOutput_PS_struct_End = R"(
} vert; )";

const string vertOutput_PS_instanced_transparency = R"(
const string vertOutput_PS_instanced_transparency = R"(
out float transparency; // transparency of a particle )";
const string fragInput_PS_instanced_transparency = R"(
const string fragInput_PS_instanced_transparency = R"(
in float transparency; // transparency of a particle )";

const string fragMain_PS_instanced_v_doColorOverLT = R"(
const string fragMain_PS_instanced_v_doColorOverLT = R"(
vec4 color = vec4(colorByAge(v_age/u_tTL), 1.0); // Particle color)";
const string fragMain_PS_instanced_c = R"(
const string fragMain_PS_instanced_c = R"(
vec4 color = u_color; // Particle color)";
const string fragMain_PS_instanced_transparency = R"(
const string fragMain_PS_instanced_transparency = R"(
color.w *= transparency; // Apply transparency)";
const string vertInput_u_matrix_p = R"(
const string vertInput_u_matrix_p = R"(
uniform mat4 u_pMatrix; // Projection matrix)";
const string vertInput_u_matrix_vertBillboard = R"(
const string vertInput_u_matrix_vertBillboard = R"(
uniform mat4 u_vYawPMatrix; // Projection matrix)";
const string fragInput_PS_u_c = R"(
const string fragInput_PS_u_c = R"(
uniform vec4 u_color; // Particle color)";
const string vertInput_PS_u_ScaRa = R"(
const string vertInput_PS_u_ScaRa = R"(
uniform float u_scale; // Particle scale
uniform float u_radiusW; // Particle width radius)
uniform float u_radiusH; // Particle height radius)";
Expand All @@ -208,29 +208,29 @@ const string vertOutput_PS_tf_initP = R"(
out vec3 tf_initialPosition; // To transform feedback)";

//-----------------------------------------------------------------------------
const string main_Begin = R"(
const string main_Begin = R"(
//-----------------------------------------------------------------------------
void main()
{)";
//-----------------------------------------------------------------------------
const string vertMain_v_P_VS = R"(
const string vertMain_v_P_VS = R"(
mat4 mvMatrix = u_vMatrix * u_mMatrix;
v_P_VS = vec3(mvMatrix * ${localPosition}); // vertex position in view space)";
const string vertMain_v_P_WS_Sm = R"(
const string vertMain_v_P_WS_Sm = R"(
v_P_WS = vec3(u_mMatrix * ${localPosition}); // vertex position in world space)";
const string vertMain_v_N_VS = R"(
const string vertMain_v_N_VS = R"(
mat3 invMvMatrix = mat3(inverse(mvMatrix));
mat3 nMatrix = transpose(invMvMatrix);
v_N_VS = vec3(nMatrix * ${localNormal}); // vertex normal in view space)";
const string vertMain_v_R_OS = R"(
const string vertMain_v_R_OS = R"(
vec3 I = normalize(v_P_VS);
vec3 N = normalize(v_N_VS);
v_R_OS = invMvMatrix * reflect(I, N); // R = I-2.0*dot(N,I)*N;)";
const string vertMain_v_uv0 = R"(
const string vertMain_v_uv0 = R"(
v_uv0 = a_uv0; // pass diffuse color tex.coord. 1 for interpolation)";
const string vertMain_v_uv1 = R"(
const string vertMain_v_uv1 = R"(
v_uv1 = a_uv1; // pass diffuse color tex.coord. 1 for interpolation)";
const string vertMain_skinning = R"(
const string vertMain_skinning = R"(
vec4 skinnedPosition;
vec3 skinnedNormal;
Expand All @@ -254,7 +254,7 @@ const string vertMain_skinning = R"(
skinnedNormal = a_normal;
}
)";
const string vertMain_skinning_Nm = R"(
const string vertMain_skinning_Nm = R"(
vec4 skinnedPosition;
vec3 skinnedNormal;
vec4 skinnedTangent;
Expand All @@ -281,7 +281,7 @@ const string vertMain_skinning_Nm = R"(
skinnedTangent = a_tangent;
}
)";
const string vertMain_TBN_Nm = R"(
const string vertMain_TBN_Nm = R"(
// Building the matrix Eye Space -> Tangent Space
// See the math behind at: http://www.terathon.com/code/tangent.html
Expand Down Expand Up @@ -309,51 +309,51 @@ const string vertMain_TBN_Nm = R"(

//-----------------------------------------------------------------------------
// Things that goes directly to geometry shader
const string vertMain_PS_v_t_default = R"(
const string vertMain_PS_v_t_default = R"(
if(age < 0.0)
vert.transparency = 0.0; // To be discard, because the particle is to be born
else
vert.transparency = 1.0;)";
const string vertMain_PS_v_t_begin = R"(
const string vertMain_PS_v_t_begin = R"(
if(age < 0.0)
vert.transparency = 0.0; // To be discard, because the particle is to be born
else
{
vert.transparency = age / u_tTL; // Get by the ratio age:lifetime)";
const string vertMain_PS_v_t_linear = R"(
const string vertMain_PS_v_t_linear = R"(
vert.transparency = 1.0 - vert.transparency; // Linear)";
const string vertMain_PS_v_t_curve = R"(
const string vertMain_PS_v_t_curve = R"(
vert.transparency = pow(vert.transparency,3.0) * u_al_bernstein.x +
pow(vert.transparency,2.0) * u_al_bernstein.y +
vert.transparency * u_al_bernstein.z +
u_al_bernstein.w; // Get transparency by bezier curve)";
const string vertMain_PS_v_t_end = R"(
const string vertMain_PS_v_t_end = R"(
})";
const string vertMain_PS_v_r = R"(
const string vertMain_PS_v_r = R"(
vert.rotation = a_rotation;)";
const string vertMain_PS_v_s = R"(
const string vertMain_PS_v_s = R"(
vert.size = age / u_tTL;)";
const string vertMain_PS_v_s_curve = R"(
const string vertMain_PS_v_s_curve = R"(
vert.size = pow(vert.size,3.0) * u_si_bernstein.x +
pow(vert.size,2.0) * u_si_bernstein.y +
vert.size * u_si_bernstein.z +
u_si_bernstein.w; // Get transparency by bezier curve)";

const string vertMain_PS_v_doColorOverLT = R"(
const string vertMain_PS_v_doColorOverLT = R"(
vert.color = colorByAge(age/u_tTL);)";
const string vertOutput_PS_age = R"(
const string vertOutput_PS_age = R"(
out float v_age; // Age of a particle)";
const string fragInput_PS_age = R"(
const string fragInput_PS_age = R"(
in float v_age; // Age of a particle)";
const string vertMain_PS_v_texNum = R"(
const string vertMain_PS_v_texNum = R"(
vert.texNum = a_texNum;)";
const string vertMain_PS_v_a = R"(
const string vertMain_PS_v_a = R"(
float age = u_time - a_startTime; // Get the age of the particle)";
const string vertMain_PS_v_tC = R"(
const string vertMain_PS_v_tC = R"(
v_texCoord = 0.5 * (a_instancePos.xy + vec2(1.0));)";
const string vertMain_PS_v_age = R"(
const string vertMain_PS_v_age = R"(
v_age = age;)";
const string vertMain_PS_v_tC_flipbook = R"(
const string vertMain_PS_v_tC_flipbook = R"(
int actCI = int(mod(float(a_texNum), float(u_col)));
float actC = float(actCI);
float actR = floor(float(int(a_texNum) - actCI) / float(u_col));
Expand Down Expand Up @@ -846,7 +846,7 @@ const string fragMain_PS_endAll = R"(
o_fragColor.rgb = pow(o_fragColor.rgb, vec3(u_oneOverGamma));
})";
//-----------------------------------------------------------------------------
const string fragInput_u_lightAll = R"(
const string fragInput_u_lightAll = R"(
uniform bool u_lightIsOn[NUM_LIGHTS]; // flag if light is on
uniform vec4 u_lightPosVS[NUM_LIGHTS]; // position of light in view space
Expand All @@ -862,44 +862,44 @@ uniform bool u_lightDoAtt[NUM_LIGHTS]; // flag if att. must be calc
uniform vec4 u_globalAmbi; // Global ambient scene color
uniform float u_oneOverGamma; // 1.0f / Gamma correction value
)";
const string fragInput_u_matBlinnAll = R"(
const string fragInput_u_matBlinnAll = R"(
uniform vec4 u_matAmbi; // ambient color reflection coefficient (ka)
uniform vec4 u_matDiff; // diffuse color reflection coefficient (kd)
uniform vec4 u_matSpec; // specular color reflection coefficient (ks)
uniform vec4 u_matEmis; // emissive color for self-shining materials
uniform float u_matShin; // shininess exponent
)";
const string fragInput_u_matAmbi = R"(
const string fragInput_u_matAmbi = R"(
uniform vec4 u_matAmbi; // ambient color reflection coefficient (ka))";
const string fragInput_u_matDiff = R"(
const string fragInput_u_matDiff = R"(
uniform vec4 u_matDiff; // diffuse color reflection coefficient (kd))";
const string fragInput_u_matEmis = R"(
const string fragInput_u_matEmis = R"(
uniform vec4 u_matEmis; // emissive color (ke))";
const string fragInput_u_matRough = R"(
const string fragInput_u_matRough = R"(
uniform float u_matRough; // roughness factor (0-1))";
const string fragInput_u_matMetal = R"(
const string fragInput_u_matMetal = R"(
uniform float u_matMetal; // metalness factor (0-1)";
//-----------------------------------------------------------------------------
const string fragInput_u_matTexDm = R"(
const string fragInput_u_matTexDm = R"(
uniform sampler2D u_matTextureDiffuse0; // Diffuse color map)";
const string fragInput_u_matTexNm = R"(
const string fragInput_u_matTexNm = R"(
uniform sampler2D u_matTextureNormal0; // Normal bump map)";
const string fragInput_u_matTexEm = R"(
const string fragInput_u_matTexEm = R"(
uniform sampler2D u_matTextureEmissive0; // PBR material emissive texture)";
const string fragInput_u_matTexOm = R"(
const string fragInput_u_matTexOm = R"(
uniform sampler2D u_matTextureOcclusion0; // Ambient occlusion map)";
const string fragInput_u_matTexRm = R"(
const string fragInput_u_matTexRm = R"(
uniform sampler2D u_matTextureRoughness0; // PBR material roughness texture)";
const string fragInput_u_matTexMm = R"(
const string fragInput_u_matTexMm = R"(
uniform sampler2D u_matTextureMetallic0; // PBR material metallic texture)";
const string fragInput_u_matTexRmMm = R"(
const string fragInput_u_matTexRmMm = R"(
uniform sampler2D u_matTextureRoughMetal0; // PBR material roughness-metallic texture)";
const string fragInput_u_matTexOmRmMm = R"(
const string fragInput_u_matTexOmRmMm = R"(
uniform sampler2D u_matTextureOccluRoughMetal0; // PBR material occlusion-roughness-metalic texture)";
const string fragInput_u_matGetsSm = R"(
const string fragInput_u_matGetsSm = R"(
uniform bool u_matGetsShadows; // flag if material receives shadows)";
const string fragInput_u_skyCookEnvMaps = R"(
const string fragInput_u_skyCookEnvMaps = R"(
uniform samplerCube u_skyIrradianceCubemap; // PBR skybox irradiance light
uniform samplerCube u_skyRoughnessCubemap; // PBR skybox cubemap for rough reflections
uniform sampler2D u_skyBrdfLutTexture; // PBR lighting lookup table for BRDF
Expand Down Expand Up @@ -1925,8 +1925,8 @@ void SLGLProgramGenerated::buildProgramCodePS(SLMaterial* mat,

//-----------------------------------------------------------------------------
void SLGLProgramGenerated::buildPerPixCook(SLMaterial* mat,
SLVLight* lights,
SLbool supportGPUSkinning)
SLVLight* lights,
SLbool supportGPUSkinning)
{
assert(mat && lights);
assert(_shaders.size() > 1 &&
Expand Down Expand Up @@ -2054,8 +2054,8 @@ void SLGLProgramGenerated::buildPerPixCook(SLMaterial* mat,
}
//-----------------------------------------------------------------------------
void SLGLProgramGenerated::buildPerPixBlinn(SLMaterial* mat,
SLVLight* lights,
SLbool supportGPUSkinning)
SLVLight* lights,
SLbool supportGPUSkinning)
{
assert(mat && lights);
assert(_shaders.size() > 1 &&
Expand Down Expand Up @@ -2192,10 +2192,10 @@ void SLGLProgramGenerated::buildPerPixParticleInstanced(SLMaterial* mat)
vertCode += shaderHeader();

// Vertex shader inputs
vertCode += vertInput_PS_a_InstPos; // instance position
vertCode += vertInput_PS_a_p; // position
vertCode += vertInput_PS_a_st; // start time
if (rot) vertCode += vertInput_PS_a_r; // rotation as float
vertCode += vertInput_PS_a_InstPos; // instance position
vertCode += vertInput_PS_a_p; // position
vertCode += vertInput_PS_a_st; // start time
if (rot) vertCode += vertInput_PS_a_r; // rotation as float

if (FlBoTex)
{
Expand Down Expand Up @@ -2940,6 +2940,7 @@ void SLGLProgramGenerated::addCodeToShader(SLGLShader* shader,
#endif
shader->name(name);

#ifndef SL_EMSCRIPTEN
// Check if generatedShaderPath folder exists
generatedShaderPath = SLGLProgramManager::configPath + "generatedShaders/";
if (!Utils::dirExists(SLGLProgramManager::configPath))
Expand All @@ -2953,6 +2954,7 @@ void SLGLProgramGenerated::addCodeToShader(SLGLShader* shader,
}

shader->file(generatedShaderPath + name);
#endif
}
//-----------------------------------------------------------------------------
//! Sets a variable in the shader code.
Expand Down
8 changes: 5 additions & 3 deletions modules/sl/source/gl/SLGLShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,11 @@ SLbool SLGLShader::createAndCompile(SLVLight* lights)
return false;
}

#ifndef SL_EMSCRIPTEN
// Write generated shader out
if (!_file.empty())
{
#if defined(DEBUG) || defined(_DEBUG)
# if defined(DEBUG) || defined(_DEBUG)
string filename = Utils::getFileName(_file);
string path = Utils::getDirName(_file);
if (Utils::dirExists(path))
Expand All @@ -174,7 +175,7 @@ SLbool SLGLShader::createAndCompile(SLVLight* lights)
}
else
SL_WARN_MSG("**** No path to write shader ***");
#else
# else
if (!SLFileStorage::exists(_file, IOK_shader))
{
string filename = Utils::getFileName(_file);
Expand All @@ -187,10 +188,11 @@ SLbool SLGLShader::createAndCompile(SLVLight* lights)
else
SL_WARN_MSG("**** No path to write shader ***");
}
#endif
# endif
}
else
SL_WARN_MSG("**** No shader path and filename for shader ***");
#endif

return true;
}
Expand Down
27 changes: 7 additions & 20 deletions modules/utils/source/io/SLFileStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,9 @@ SLIOStream* SLFileStorage::open(std::string path,

if (mode == IOM_read)
{
if (kind == IOK_shader)
if (kind == IOK_shader || kind == IOK_image || kind == IOK_model || kind == IOK_font)
{
// Generated shaders exist in memory, pre-written shaders
// need to be downloaded from the server.

if (SLIOMemory::exists(path))
return new SLIOReaderMemory(path);
else
return new SLIOReaderFetch(path);
}
else if (kind == IOK_image || kind == IOK_model || kind == IOK_font)
{
// Images, models and fonts are always stored on the server.
// Shaders, images, models and fonts are always stored on the server.
return new SLIOReaderFetch(path);
}
else if (kind == IOK_config)
Expand All @@ -92,14 +82,11 @@ SLIOStream* SLFileStorage::open(std::string path,
}
else if (mode == IOM_write)
{
// Generated shaders are written to memory, config files are written
// to local storage, so they can be read when the website is reloaded,
// images (e.g. screenshots) are displayed in the browser window so
// the user can download them.
// Config files are written to local storage so they can be read when
// the website is reloaded, images (e.g. screenshots) are displayed in
// the browser window so the user can download them.

if (kind == IOK_shader)
return new SLIOWriterMemory(path);
else if (kind == IOK_config)
if (kind == IOK_config)
return new SLIOWriterLocalStorage(path);
else if (kind == IOK_image)
return new SLIOWriterBrowserPopup(path);
Expand Down Expand Up @@ -140,7 +127,7 @@ bool SLFileStorage::exists(std::string path, SLIOStreamKind kind)
return false;

if (kind == IOK_shader)
return SLIOMemory::exists(path) || SLIOReaderFetch::exists(path);
return SLIOReaderFetch::exists(path);
else if (kind == IOK_config)
return SLIOLocalStorage::exists(path) || SLIOReaderFetch::exists(path);
else
Expand Down
6 changes: 6 additions & 0 deletions modules/utils/source/io/SLIOFetch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
#ifdef SL_STORAGE_WEB
//-----------------------------------------------------------------------------
# include <emscripten/fetch.h>
# include <pthread.h>
# include <cassert>
# include <iostream>
//-----------------------------------------------------------------------------
bool SLIOReaderFetch::exists(std::string url)
{
assert(pthread_self() != 0 && "Fetching is not allowed on the main thread");

emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
std::strcpy(attr.requestMethod, "HEAD");
Expand All @@ -30,6 +34,8 @@ bool SLIOReaderFetch::exists(std::string url)
SLIOReaderFetch::SLIOReaderFetch(std::string url)
: SLIOReaderMemory(url)
{
assert(pthread_self() != 0 && "Fetching is not allowed on the main thread");

std::cout << "FETCH \"" << url << "\"" << std::endl;

emscripten_fetch_attr_t attr;
Expand Down

0 comments on commit 479083d

Please sign in to comment.