Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
glslang
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
glslang
Commits
b16a4bc4
Commit
b16a4bc4
authored
Jul 25, 2019
by
Kai Ninomiya
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make glslang.js easy to use
parent
a91561d5
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
78 additions
and
25 deletions
+78
-25
CMakeLists.txt
glslang/CMakeLists.txt
+2
-1
glslang.js.cpp
glslang/glslang.js.cpp
+32
-24
glslang.pre.js
glslang/glslang.pre.js
+44
-0
No files found.
glslang/CMakeLists.txt
View file @
b16a4bc4
...
@@ -131,6 +131,7 @@ if(ENABLE_GLSLANG_WEB)
...
@@ -131,6 +131,7 @@ if(ENABLE_GLSLANG_WEB)
set_target_properties
(
glslang.js PROPERTIES
set_target_properties
(
glslang.js PROPERTIES
OUTPUT_NAME
"glslang"
OUTPUT_NAME
"glslang"
SUFFIX
".js"
SUFFIX
".js"
LINK_FLAGS
"--bind"
)
LINK_FLAGS
"--bind -s EXPORT_NAME=
\"
glslangModule
\"
"
)
em_link_pre_js
(
glslang.js
${
CMAKE_CURRENT_SOURCE_DIR
}
/glslang.pre.js
)
endif
(
EMSCRIPTEN
)
endif
(
EMSCRIPTEN
)
endif
(
ENABLE_GLSLANG_WEB
)
endif
(
ENABLE_GLSLANG_WEB
)
glslang/glslang.js.cpp
View file @
b16a4bc4
...
@@ -34,6 +34,8 @@
...
@@ -34,6 +34,8 @@
//
//
#include <cstdio>
#include <cstdio>
#include <cstdint>
#ifdef __EMSCRIPTEN__
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten.h>
#endif // __EMSCRIPTEN__
#endif // __EMSCRIPTEN__
...
@@ -43,6 +45,10 @@
...
@@ -43,6 +45,10 @@
#include "../SPIRV/doc.h"
#include "../SPIRV/doc.h"
#include "./../glslang/Public/ShaderLang.h"
#include "./../glslang/Public/ShaderLang.h"
#ifndef EMSCRIPTEN_KEEPALIVE
#define EMSCRIPTEN_KEEPALIVE
#endif
const
TBuiltInResource
DefaultTBuiltInResource
=
{
const
TBuiltInResource
DefaultTBuiltInResource
=
{
/* .MaxLights = */
32
,
/* .MaxLights = */
32
,
/* .MaxClipPlanes = */
6
,
/* .MaxClipPlanes = */
6
,
...
@@ -149,6 +155,8 @@ const TBuiltInResource DefaultTBuiltInResource = {
...
@@ -149,6 +155,8 @@ const TBuiltInResource DefaultTBuiltInResource = {
/* .generalConstantMatrixVectorIndexing = */
1
,
/* .generalConstantMatrixVectorIndexing = */
1
,
}};
}};
extern
"C"
{
/*
/*
* Takes in a GLSL shader as a string and converts it to SPIR-V in binary form.
* Takes in a GLSL shader as a string and converts it to SPIR-V in binary form.
*
*
...
@@ -158,8 +166,8 @@ const TBuiltInResource DefaultTBuiltInResource = {
...
@@ -158,8 +166,8 @@ const TBuiltInResource DefaultTBuiltInResource = {
* |shader_type| Magic number indicating the type of shader being processed.
* |shader_type| Magic number indicating the type of shader being processed.
* Legal values are as follows:
* Legal values are as follows:
* Vertex = 0
* Vertex = 0
* Geometry = 3
* Fragment = 4
* Fragment = 4
* Compute = 5
* |spirv| Pointer to an output buffer that will be updated with the
* |spirv| Pointer to an output buffer that will be updated with the
* resulting SPIR-V shader.
* resulting SPIR-V shader.
* This buffer must be destroyed using destroy_output_buffer.
* This buffer must be destroyed using destroy_output_buffer.
...
@@ -169,10 +177,8 @@ const TBuiltInResource DefaultTBuiltInResource = {
...
@@ -169,10 +177,8 @@ const TBuiltInResource DefaultTBuiltInResource = {
*
*
* Return 0 on success, non-0 on failure.
* Return 0 on success, non-0 on failure.
*/
*/
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
EMSCRIPTEN_KEEPALIVE
#endif // __EMSCRIPTEN__
int
convert_glsl_to_spirv
(
const
char
*
glsl
,
int
shader_type
,
uint32_t
**
spirv
,
size_t
*
spirv_len
,
bool
gen_debug
)
int
convert_glsl_to_spirv
(
const
char
*
glsl
,
int
shader_type
,
unsigned
int
**
spirv
,
size_t
*
spirv_len
,
bool
gen_debug
)
{
{
int
ret_val
=
0
;
int
ret_val
=
0
;
if
(
glsl
==
nullptr
||
spirv
==
nullptr
)
{
if
(
glsl
==
nullptr
||
spirv
==
nullptr
)
{
...
@@ -180,27 +186,34 @@ int convert_glsl_to_spirv(const char* glsl, int shader_type, unsigned int** spir
...
@@ -180,27 +186,34 @@ int convert_glsl_to_spirv(const char* glsl, int shader_type, unsigned int** spir
}
}
*
spirv
=
nullptr
;
*
spirv
=
nullptr
;
if
(
shader_type
!=
0
&&
shader_type
!=
3
&&
shader_type
!=
4
)
{
if
(
shader_type
!=
0
&&
shader_type
!=
4
&&
shader_type
!=
5
)
{
return
2
;
return
2
;
}
}
EShLanguage
shader_
lang
=
static_cast
<
EShLanguage
>
(
shader_type
);
EShLanguage
shader_
stage
=
static_cast
<
EShLanguage
>
(
shader_type
);
glslang
::
InitializeProcess
();
glslang
::
InitializeProcess
();
{
{
glslang
::
TShader
shader
(
shader_
lang
);
glslang
::
TShader
shader
(
shader_
stage
);
shader
.
setStrings
(
&
glsl
,
1
);
shader
.
setStrings
(
&
glsl
,
1
);
shader
.
setEnvInput
(
glslang
::
EShSourceGlsl
,
shader_
lang
,
glslang
::
EShClientOpenGL
,
100
);
shader
.
setEnvInput
(
glslang
::
EShSourceGlsl
,
shader_
stage
,
glslang
::
EShClientVulkan
,
100
);
shader
.
setEnvClient
(
glslang
::
EShClientVulkan
,
glslang
::
EShTargetVulkan_1_1
);
shader
.
setEnvClient
(
glslang
::
EShClientVulkan
,
glslang
::
EShTargetVulkan_1_1
);
shader
.
setEnvTarget
(
glslang
::
EShTargetSpv
,
glslang
::
EShTargetSpv_1_3
);
shader
.
setEnvTarget
(
glslang
::
EShTargetSpv
,
glslang
::
EShTargetSpv_1_3
);
shader
.
parse
(
&
DefaultTBuiltInResource
,
100
,
true
,
EShMsgDefault
);
if
(
!
shader
.
parse
(
&
DefaultTBuiltInResource
,
100
,
true
,
EShMsgDefault
))
{
fprintf
(
stderr
,
"Parse failed
\n
"
);
fprintf
(
stderr
,
"%s
\n
"
,
shader
.
getInfoLog
());
fprintf
(
stderr
,
"%s
\n
"
,
shader
.
getInfoDebugLog
());
}
glslang
::
TProgram
program
;
glslang
::
TProgram
program
;
program
.
addShader
(
&
shader
);
program
.
addShader
(
&
shader
);
program
.
link
(
EShMsgDefault
);
if
(
!
program
.
link
(
EShMsgDefault
))
{
fprintf
(
stderr
,
"Link failed
\n
"
);
fprintf
(
stderr
,
"%s
\n
"
,
program
.
getInfoLog
());
fprintf
(
stderr
,
"%s
\n
"
,
program
.
getInfoDebugLog
());
}
std
::
vector
<
unsigned
int
>
output
;
std
::
vector
<
uint32_t
>
output
;
std
::
string
warningsErrors
;
glslang
::
SpvOptions
spvOptions
;
glslang
::
SpvOptions
spvOptions
;
spvOptions
.
generateDebugInfo
=
gen_debug
;
spvOptions
.
generateDebugInfo
=
gen_debug
;
spvOptions
.
disableOptimizer
=
false
;
spvOptions
.
disableOptimizer
=
false
;
...
@@ -208,12 +221,12 @@ int convert_glsl_to_spirv(const char* glsl, int shader_type, unsigned int** spir
...
@@ -208,12 +221,12 @@ int convert_glsl_to_spirv(const char* glsl, int shader_type, unsigned int** spir
spvOptions
.
disassemble
=
false
;
spvOptions
.
disassemble
=
false
;
spvOptions
.
validate
=
false
;
spvOptions
.
validate
=
false
;
glslang
::
GlslangToSpv
(
*
program
.
getIntermediate
(
EShLangFragment
),
output
,
nullptr
,
&
spvOptions
);
glslang
::
GlslangToSpv
(
*
program
.
getIntermediate
(
shader_stage
),
output
,
nullptr
,
&
spvOptions
);
*
spirv_len
=
output
.
size
();
*
spirv_len
=
output
.
size
();
*
spirv
=
static_cast
<
u
nsigned
int
*>
(
malloc
(
*
spirv_len
*
sizeof
(
unsigned
in
t
)));
*
spirv
=
static_cast
<
u
int32_t
*>
(
malloc
(
*
spirv_len
*
sizeof
(
uint32_
t
)));
if
(
*
spirv
!=
nullptr
)
{
if
(
*
spirv
!=
nullptr
)
{
memcpy
(
*
spirv
,
output
.
data
(),
*
spirv_len
);
memcpy
(
*
spirv
,
output
.
data
(),
*
spirv_len
*
sizeof
(
uint32_t
)
);
}
else
{
}
else
{
ret_val
=
3
;
ret_val
=
3
;
}
}
...
@@ -227,17 +240,13 @@ int convert_glsl_to_spirv(const char* glsl, int shader_type, unsigned int** spir
...
@@ -227,17 +240,13 @@ int convert_glsl_to_spirv(const char* glsl, int shader_type, unsigned int** spir
*
*
* Must be destroyed later using destroy_input_buffer.
* Must be destroyed later using destroy_input_buffer.
*/
*/
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
EMSCRIPTEN_KEEPALIVE
#endif // __EMSCRIPTEN__
char
*
create_input_buffer
(
int
count
)
{
return
static_cast
<
char
*>
(
malloc
(
count
*
sizeof
(
char
)));
}
char
*
create_input_buffer
(
int
count
)
{
return
static_cast
<
char
*>
(
malloc
(
count
*
sizeof
(
char
)));
}
/*
/*
* Destroys a buffer created by create_input_buffer
* Destroys a buffer created by create_input_buffer
*/
*/
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
EMSCRIPTEN_KEEPALIVE
#endif // __EMSCRIPTEN__
void
destroy_input_buffer
(
char
*
p
)
void
destroy_input_buffer
(
char
*
p
)
{
{
if
(
p
!=
nullptr
)
if
(
p
!=
nullptr
)
...
@@ -247,15 +256,14 @@ void destroy_input_buffer(char* p)
...
@@ -247,15 +256,14 @@ void destroy_input_buffer(char* p)
/*
/*
* Destroys a buffer created by convert_glsl_to_spirv
* Destroys a buffer created by convert_glsl_to_spirv
*/
*/
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_KEEPALIVE
EMSCRIPTEN_KEEPALIVE
#endif // __EMSCRIPTEN__
void
destroy_output_buffer
(
uint32_t
*
p
)
void
destroy_ouput_buffer
(
unsigned
int
*
p
)
{
{
if
(
p
!=
nullptr
)
if
(
p
!=
nullptr
)
free
(
p
);
free
(
p
);
}
}
}
// extern "C"
/*
/*
* For non-Emscripten builds we supply a generic main, so that the glslang.js
* For non-Emscripten builds we supply a generic main, so that the glslang.js
...
@@ -271,7 +279,7 @@ int main() {
...
@@ -271,7 +279,7 @@ int main() {
void main() { })"
;
void main() { })"
;
char
*
input
;
char
*
input
;
u
nsigned
in
t
*
output
;
u
int32_
t
*
output
;
size_t
output_len
;
size_t
output_len
;
input
=
create_input_buffer
(
sizeof
(
input_text
));
input
=
create_input_buffer
(
sizeof
(
input_text
));
...
@@ -279,7 +287,7 @@ void main() { })";
...
@@ -279,7 +287,7 @@ void main() { })";
memcpy
(
input
,
input_text
,
sizeof
(
input_text
));
memcpy
(
input
,
input_text
,
sizeof
(
input_text
));
convert_glsl_to_spirv
(
input
,
4
,
&
output
,
&
output_len
,
false
);
convert_glsl_to_spirv
(
input
,
4
,
&
output
,
&
output_len
,
false
);
destroy_ouput_buffer
(
output
);
destroy_ou
t
put_buffer
(
output
);
destroy_input_buffer
(
input
);
destroy_input_buffer
(
input
);
return
0
;
return
0
;
}
}
...
...
glslang/glslang.pre.js
0 → 100644
View file @
b16a4bc4
Module
[
'compileGLSLZeroCopy'
]
=
function
(
glsl
,
shader_stage
,
gen_debug
)
{
gen_debug
=
!!
gen_debug
;
var
shader_stage_int
;
if
(
shader_stage
===
'vertex'
)
{
shader_stage_int
=
0
;
}
else
if
(
shader_stage
===
'fragment'
)
{
shader_stage_int
=
4
;
}
else
if
(
shader_stage
===
'compute'
)
{
shader_stage_int
=
5
;
}
else
{
throw
new
Error
(
"shader_stage must be 'vertex', 'fragment', or 'compute'"
);
}
var
p_output
=
Module
[
'_malloc'
](
4
);
var
p_output_len
=
Module
[
'_malloc'
](
4
);
var
err
=
ccall
(
'convert_glsl_to_spirv'
,
'number'
,
[
'string'
,
'number'
,
'number'
,
'number'
,
'boolean'
],
[
glsl
,
shader_stage_int
,
p_output
,
p_output_len
,
gen_debug
]);
var
output
=
getValue
(
p_output
,
'i32'
);
var
output_len
=
getValue
(
p_output_len
,
'i32'
);
Module
[
'_free'
](
p_output
);
Module
[
'_free'
](
p_output_len
);
if
(
err
!==
0
||
output_len
===
0
)
{
throw
new
Error
(
'GLSL compilation failed'
);
}
var
ret
=
{};
ret
.
data
=
Module
[
'HEAPU32'
].
subarray
(
output
/
4
,
output
/
4
+
output_len
);
ret
.
free
=
function
()
{
Module
[
'_destroy_output_buffer'
](
output
);
};
return
ret
;
};
Module
[
'compileGLSL'
]
=
function
(
glsl
,
shader_stage
,
gen_debug
)
{
var
compiled
=
Module
[
'compileGLSLZeroCopy'
](
glsl
,
shader_stage
,
gen_debug
);
var
ret
=
compiled
.
data
.
slice
()
compiled
.
free
();
return
ret
;
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment