Skip to content

Commit

Permalink
wasm: add separated shaders files for wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
huxingyi committed Aug 26, 2023
1 parent 7043221 commit 0c3e6b4
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 7 deletions.
4 changes: 4 additions & 0 deletions application/resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
<file>shaders/model.frag</file>
<file>shaders/model_core.vert</file>
<file>shaders/model_core.frag</file>
<file>shaders/model_wasm.vert</file>
<file>shaders/model_wasm.frag</file>
<file>shaders/monochrome.vert</file>
<file>shaders/monochrome.frag</file>
<file>shaders/monochrome_core.vert</file>
<file>shaders/monochrome_core.frag</file>
<file>shaders/monochrome_wasm.vert</file>
<file>shaders/monochrome_wasm.frag</file>
<file>resources/cedar_bridge_irradiance.dds</file>
<file>resources/cedar_bridge_specular.dds</file>
<file>resources/dust3d-vertical.png</file>
Expand Down
3 changes: 1 addition & 2 deletions application/shaders/model.frag
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
precision highp float;
precision highp int;
#version 110
uniform sampler2D environmentIrradianceMapId[6];
uniform sampler2D environmentSpecularMapId[6];
uniform sampler2D textureId;
Expand Down
1 change: 1 addition & 0 deletions application/shaders/model.vert
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#version 110
attribute vec4 vertex;
attribute vec3 normal;
attribute vec3 color;
Expand Down
138 changes: 138 additions & 0 deletions application/shaders/model_wasm.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
precision highp float;
precision highp int;
uniform sampler2D environmentIrradianceMapId[6];
uniform sampler2D environmentSpecularMapId[6];
uniform sampler2D textureId;
uniform int textureEnabled;
uniform sampler2D normalMapId;
uniform int normalMapEnabled;
uniform sampler2D metalnessRoughnessAoMapId;
uniform int metalnessMapEnabled;
uniform int roughnessMapEnabled;
uniform int aoMapEnabled;
uniform vec3 eyePosition;
varying vec3 pointPosition;
varying vec3 pointNormal;
varying vec3 pointColor;
varying vec2 pointTexCoord;
varying float pointAlpha;
varying float pointMetalness;
varying float pointRoughness;
varying mat3 pointTBN;

const float PI = 3.1415926;

vec3 fresnelSchlickRoughness(float NoV, vec3 f0, float roughness)
{
return f0 + (max(vec3(1.0 - roughness), f0) - f0) * pow(clamp(1.0 - NoV, 0.0, 1.0), 5.0);
}

void cubemap(vec3 r, out float texId, out vec2 st)
{
vec3 uvw;
vec3 absr = abs(r);
if (absr.x > absr.y && absr.x > absr.z) {
// x major
float negx = step(r.x, 0.0);
uvw = vec3(r.zy, absr.x) * vec3(mix(-1.0, 1.0, negx), -1, 1);
texId = negx;
} else if (absr.y > absr.z) {
// y major
float negy = step(r.y, 0.0);
uvw = vec3(r.xz, absr.y) * vec3(1.0, mix(1.0, -1.0, negy), 1.0);
texId = 2.0 + negy;
} else {
// z major
float negz = step(r.z, 0.0);
uvw = vec3(r.xy, absr.z) * vec3(mix(1.0, -1.0, negz), -1, 1);
texId = 4.0 + negz;
}
st = vec2(uvw.xy / uvw.z + 1.) * .5;
}

vec4 texturesAsCube(in sampler2D maps[6], in vec3 direction)
{
float texId;
vec2 st;
cubemap(direction, texId, st);
vec4 color = vec4(0);
{
vec4 side = texture2D(maps[0], st);
float select = step(0.0 - 0.5, texId) * step(texId, 0.0 + 0.5);
color = mix(color, side, select);
}
{
vec4 side = texture2D(maps[1], st);
float select = step(1.0 - 0.5, texId) * step(texId, 1.0 + 0.5);
color = mix(color, side, select);
}
{
vec4 side = texture2D(maps[2], st);
float select = step(2.0 - 0.5, texId) * step(texId, 2.0 + 0.5);
color = mix(color, side, select);
}
{
vec4 side = texture2D(maps[3], st);
float select = step(3.0 - 0.5, texId) * step(texId, 3.0 + 0.5);
color = mix(color, side, select);
}
{
vec4 side = texture2D(maps[4], st);
float select = step(4.0 - 0.5, texId) * step(texId, 4.0 + 0.5);
color = mix(color, side, select);
}
{
vec4 side = texture2D(maps[5], st);
float select = step(5.0 - 0.5, texId) * step(texId, 5.0 + 0.5);
color = mix(color, side, select);
}
return color;
}

void main()
{
vec3 color = pointColor;
float alpha = pointAlpha;
if (1 == textureEnabled) {
vec4 textColor = texture2D(textureId, pointTexCoord);
color = textColor.rgb;
alpha = textColor.a;
}
vec3 normal = pointNormal;
if (1 == normalMapEnabled) {
normal = texture2D(normalMapId, pointTexCoord).rgb;
normal = pointTBN * normalize(normal * 2.0 - 1.0);
}
float metalness = pointMetalness;
if (1 == metalnessMapEnabled) {
metalness = texture2D(metalnessRoughnessAoMapId, pointTexCoord).b;
}
float roughness = pointRoughness;
if (1 == roughnessMapEnabled) {
roughness = texture2D(metalnessRoughnessAoMapId, pointTexCoord).g;
}
float ambientOcclusion = 1.0;
if (1 == aoMapEnabled) {
ambientOcclusion = texture2D(metalnessRoughnessAoMapId, pointTexCoord).r;
}

vec3 n = normal;
vec3 v = normalize(eyePosition - pointPosition);
vec3 r = reflect(-v, n);

float NoV = abs(dot(n, v)) + 1e-5;

vec3 irradiance = texturesAsCube(environmentIrradianceMapId, r).rgb;
vec3 diffuse = irradiance * (1.0 - metalness) * color;

vec3 f0 = mix(vec3(0.04), color, metalness);
vec3 fresnelFactor = fresnelSchlickRoughness(NoV, f0, roughness);
vec3 specular = fresnelFactor * texturesAsCube(environmentSpecularMapId, r).rgb;

color = (diffuse + specular) * ambientOcclusion;

color = color / (color + vec3(1.0));
color = pow(color, vec3(1.0/2.2));

gl_FragColor = vec4(color, alpha);
}
49 changes: 49 additions & 0 deletions application/shaders/model_wasm.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
attribute vec4 vertex;
attribute vec3 normal;
attribute vec3 color;
attribute vec2 texCoord;
attribute float metalness;
attribute float roughness;
attribute vec3 tangent;
attribute float alpha;
uniform mat4 modelMatrix;
uniform mat3 normalMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
uniform int normalMapEnabled;
varying vec3 pointPosition;
varying vec3 pointNormal;
varying vec3 pointColor;
varying vec2 pointTexCoord;
varying float pointAlpha;
varying float pointMetalness;
varying float pointRoughness;
varying mat3 pointTBN;

mat3 transpose(mat3 m)
{
return mat3(m[0][0], m[1][0], m[2][0],
m[0][1], m[1][1], m[2][1],
m[0][2], m[1][2], m[2][2]);
}

void main()
{
pointPosition = (modelMatrix * vertex).xyz;
pointNormal = normalize((modelMatrix * vec4(normal, 1.0)).xyz);
pointColor = color;
pointTexCoord = texCoord;
pointAlpha = alpha;
pointMetalness = metalness;
pointRoughness = roughness;

gl_Position = projectionMatrix * viewMatrix * vec4(pointPosition, 1.0);

if (1 == normalMapEnabled) {
vec3 T = normalize(normalMatrix * tangent);
vec3 N = normalize(normalMatrix * normal);
T = normalize(T - dot(T, N) * N);
vec3 B = cross(N, T);
pointTBN = mat3(T, B, N);
}
}
3 changes: 1 addition & 2 deletions application/shaders/monochrome.frag
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
precision highp float;
precision highp int;
#version 110
varying vec3 pointPosition;
varying vec3 pointColor;
varying float pointAlpha;
Expand Down
1 change: 1 addition & 0 deletions application/shaders/monochrome.vert
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#version 110
attribute vec4 vertex;
attribute vec3 color;
attribute float alpha;
Expand Down
10 changes: 10 additions & 0 deletions application/shaders/monochrome_wasm.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
precision highp float;
precision highp int;
varying vec3 pointPosition;
varying vec3 pointColor;
varying float pointAlpha;

void main()
{
gl_FragColor = vec4(pointColor, pointAlpha);
}
18 changes: 18 additions & 0 deletions application/shaders/monochrome_wasm.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
attribute vec4 vertex;
attribute vec3 color;
attribute float alpha;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
varying vec3 pointPosition;
varying vec3 pointColor;
varying float pointAlpha;

void main()
{
pointPosition = (modelMatrix * vertex).xyz;
pointColor = color;
pointAlpha = alpha;

gl_Position = projectionMatrix * viewMatrix * vec4(pointPosition, 1.0);
}
8 changes: 5 additions & 3 deletions application/sources/document_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,7 @@ void DocumentWindow::generateComponentPreviewImages()
useThreadedOpenGL = false;
#endif

if (useThreadedOpenGL && QOpenGLContext::supportsThreadedOpenGL()) {
if (useThreadedOpenGL) {
QThread* thread = new QThread;
m_componentPreviewImagesGenerator->moveToThread(thread);
connect(thread, &QThread::started, m_componentPreviewImagesGenerator, &MeshPreviewImagesGenerator::process);
Expand All @@ -1318,8 +1318,10 @@ void DocumentWindow::generateComponentPreviewImages()
connect(thread, &QThread::finished, thread, &QThread::deleteLater);
thread->start();
} else {
connect(m_componentPreviewImagesGenerator, &MeshPreviewImagesGenerator::finished, this, &DocumentWindow::componentPreviewImagesReady);
m_componentPreviewImagesGenerator->process();
QTimer::singleShot(10, this, [this]() {
connect(m_componentPreviewImagesGenerator, &MeshPreviewImagesGenerator::finished, this, &DocumentWindow::componentPreviewImagesReady);
m_componentPreviewImagesGenerator->process();
});
}
}

Expand Down
5 changes: 5 additions & 0 deletions application/sources/model_opengl_program.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@ void ModelOpenGLProgram::load(bool isCoreProfile)
addShaderFromResource(QOpenGLShader::Vertex, ":/shaders/model_core.vert");
addShaderFromResource(QOpenGLShader::Fragment, ":/shaders/model_core.frag");
} else {
#if defined(Q_OS_WASM)
addShaderFromResource(QOpenGLShader::Vertex, ":/shaders/model_wasm.vert");
addShaderFromResource(QOpenGLShader::Fragment, ":/shaders/model_wasm.frag");
#else
addShaderFromResource(QOpenGLShader::Vertex, ":/shaders/model.vert");
addShaderFromResource(QOpenGLShader::Fragment, ":/shaders/model.frag");
#endif
}
bindAttributeLocation("vertex", 0);
bindAttributeLocation("normal", 1);
Expand Down
5 changes: 5 additions & 0 deletions application/sources/monochrome_opengl_program.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ void MonochromeOpenGLProgram::load(bool isCoreProfile)
addShaderFromResource(QOpenGLShader::Vertex, ":/shaders/monochrome_core.vert");
addShaderFromResource(QOpenGLShader::Fragment, ":/shaders/monochrome_core.frag");
} else {
#if defined(Q_OS_WASM)
addShaderFromResource(QOpenGLShader::Vertex, ":/shaders/monochrome_wasm.vert");
addShaderFromResource(QOpenGLShader::Fragment, ":/shaders/monochrome_wasm.frag");
#else
addShaderFromResource(QOpenGLShader::Vertex, ":/shaders/monochrome.vert");
addShaderFromResource(QOpenGLShader::Fragment, ":/shaders/monochrome.frag");
#endif
}
bindAttributeLocation("vertex", 0);
bindAttributeLocation("color", 1);
Expand Down

0 comments on commit 0c3e6b4

Please sign in to comment.