Skip to content
Snippets Groups Projects
Commit 7907bd0d authored by Jaswant Panchumarti (Kitware)'s avatar Jaswant Panchumarti (Kitware)
Browse files

Remove ConeWebGPU example

- preparing to add a multi-backend example.
parent 6d778979
No related branches found
No related tags found
No related merge requests found
cmake_minimum_required(VERSION 3.13)
project(ConeWebGPU)
# -----------------------------------------------------------------------------
# EMSCRIPTEN only
# -----------------------------------------------------------------------------
if (NOT EMSCRIPTEN)
message("Skipping example: This needs to run inside an Emscripten build environment")
return ()
endif ()
# -----------------------------------------------------------------------------
# Handle VTK dependency
# -----------------------------------------------------------------------------
find_package(VTK
COMPONENTS
FiltersSources # VTK pipeline
InteractionStyle # Mouse handling
RenderingWebGPU # For Rendering
RenderingUI # For SDL2 Window
)
if (NOT VTK_FOUND)
message("Skipping example: ${VTK_NOT_FOUND_MESSAGE}")
return ()
endif ()
# -----------------------------------------------------------------------------
# WebAssembly build options
# -----------------------------------------------------------------------------
set(emscripten_options)
list(APPEND emscripten_options
"--bind"
"-g3"
"SHELL:-s EXPORT_NAME=vtkApp"
"SHELL:-s ALLOW_MEMORY_GROWTH=1"
"SHELL:-s DEMANGLE_SUPPORT=1"
"SHELL:-s EMULATE_FUNCTION_POINTER_CASTS=0"
"SHELL:-s ERROR_ON_UNDEFINED_SYMBOLS=0"
"SHELL:-s MODULARIZE=1"
"SHELL:-s USE_PTHREADS=0"
"SHELL:-s WASM=1"
)
# -----------------------------------------------------------------------------
# Build options
# -----------------------------------------------------------------------------
set(OPTIMIZE "NO_OPTIMIZATION" CACHE STRING "Emscripten optimization")
set_property(CACHE OPTIMIZE PROPERTY
STRINGS
NO_OPTIMIZATION # -O0
LITTLE # -O1
MORE # -O2
BEST # -O3
SMALL # -Os
SMALLEST # -Oz
SMALLEST_WITH_CLOSURE # -Oz --closure 1
)
if(OPTIMIZE STREQUAL "NO_OPTIMIZATION")
# ConeWebGPU.js 659K
# ConeWebGPU.wasm 4.9M
# time => 4 minutes 3 seconds
list(APPEND emscripten_options
"-O0"
)
elseif(OPTIMIZE STREQUAL "LITTLE")
# ConeWebGPU.js 529K
# ConeWebGPU.wasm 5.9M
list(APPEND emscripten_options
"-O1"
)
elseif(OPTIMIZE STREQUAL "MORE")
# ConeWebGPU.js 529K
# ConeWebGPU.wasm 5.3M
list(APPEND emscripten_options
"-O2"
)
elseif(OPTIMIZE STREQUAL "BEST")
# ConeWebGPU.js 529K
# ConeWebGPU.wasm 4.9M
# time => 4 minutes 7 seconds
list(APPEND emscripten_options
"-O3"
)
elseif(OPTIMIZE STREQUAL "SMALL")
# ConeWebGPU.js 529K
# ConeWebGPU.wasm 4.9M
list(APPEND emscripten_options
"-Os"
)
elseif(OPTIMIZE STREQUAL "SMALLEST")
# ConeWebGPU.js 659K
# ConeWebGPU.wasm 4.9M
list(APPEND emscripten_options
"-Oz"
)
elseif(OPTIMIZE STREQUAL "SMALLEST_WITH_CLOSURE")
# ConeWebGPU.js 659K
# ConeWebGPU.wasm 4.9M
list(APPEND emscripten_options
"-Oz"
"SHELL:--closure 1"
)
endif()
# -----------------------------------------------------------------------------
# Compile example code
# -----------------------------------------------------------------------------
add_executable(ConeWebGPU ConeWebGPU.cxx)
target_link_libraries(ConeWebGPU
PRIVATE
VTK::FiltersSources
VTK::InteractionStyle
VTK::RenderingWebGPU
VTK::RenderingUI
)
target_compile_options(ConeWebGPU
PUBLIC
${emscripten_options}
)
target_link_options(ConeWebGPU
PUBLIC
${emscripten_options}
)
# -----------------------------------------------------------------------------
# VTK modules initialization
# -----------------------------------------------------------------------------
vtk_module_autoinit(
TARGETS ConeWebGPU
MODULES ${VTK_LIBRARIES}
)
# -----------------------------------------------------------------------------
# Copy HTML to build directory
# -----------------------------------------------------------------------------
add_custom_command(
TARGET ConeWebGPU
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy_if_different
"${CMAKE_CURRENT_SOURCE_DIR}/index.html"
$<TARGET_FILE_DIR:ConeWebGPU>
)
/*=========================================================================
Program: Visualization Toolkit
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkActor.h"
#include "vtkCellData.h"
#include "vtkConeSource.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkMinimalStandardRandomSequence.h"
#include "vtkNew.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
//------------------------------------------------------------------------------
// Main
//------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
vtkNew<vtkRenderWindow> renWin;
renWin->SetWindowName(__func__);
renWin->SetMultiSamples(0);
vtkNew<vtkRenderer> renderer;
renWin->AddRenderer(renderer);
double x = 0.0, y = 0.0, z = 0.0;
vtkNew<vtkMinimalStandardRandomSequence> seq;
double spacingX = 2.0, spacingY = 2.0, spacingZ = 2.0;
int nx = 100, ny = 10, nz = 10;
for (int k = 0; k < nz; ++k)
{
for (int j = 0; j < ny; ++j)
{
for (int i = 0; i < nx; ++i)
{
vtkNew<vtkConeSource> coneSrc;
coneSrc->SetResolution(10);
coneSrc->SetCenter(x, y, z);
x += spacingX;
coneSrc->Update();
vtkPolyData* cone = coneSrc->GetOutput();
seq->SetSeed(k * ny * nx + j * nx + i);
vtkNew<vtkUnsignedCharArray> colors;
colors->SetNumberOfComponents(4);
for (vtkIdType cellId = 0; cellId < cone->GetNumberOfPolys(); ++cellId)
{
double red = seq->GetNextRangeValue(0, 255.);
double green = seq->GetNextRangeValue(0, 255.);
double blue = seq->GetNextRangeValue(0, 255.);
colors->InsertNextTuple4(red, green, blue, 255);
}
cone->GetCellData()->SetScalars(colors);
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputData(cone);
mapper->Update();
mapper->SetStatic(true);
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
actor->GetProperty()->SetEdgeVisibility(1);
actor->GetProperty()->SetEdgeColor(1.0, 1.0, 1.0);
mapper->Update();
actor->SetOrigin(x, y, z);
actor->RotateZ(i * j);
renderer->AddActor(actor);
}
x = 0.0;
y += spacingY;
}
y = 0.0;
z += spacingZ;
}
renderer->ResetCamera();
renderer->SetBackground(0.2, 0.3, 0.4);
renWin->Render();
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin);
vtkNew<vtkInteractorStyleTrackballCamera> style;
iren->SetInteractorStyle(style);
style->SetDefaultRenderer(renderer);
renWin->Render();
iren->Start();
return 0;
}
# WebAssembly Cone Example with WebGPU
This example aims to provide a base example on how to write a VTK viewer for
WebAssembly.
## Compiling example against VTK
We assume inside the `work/` directory to find the source of VTK under `src/`
and its build tree under `build-vtk-wasm`.
If VTK is not built yet, please follow the guide `../README.md`.
Let's create the build directory for our example
```
mkdir -p work/build-cone-webgpu
```
Start docker inside that working directory
```
docker run --rm --entrypoint /bin/bash -v $PWD:/work -p 8000:8000 -it dockcross/web-wasm:20230222-162287d
cd /work/build-cone-webgpu
cmake \
-G Ninja \
-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} \
-DVTK_DIR=/work/build-vtk-wasm \
-DOPTIMIZE=BEST \
/work/src/Examples/Emscripten/Cxx/ConeWebGPU
cmake --build .
```
## Serve and test generated code
```
cd work/build-cone-webgpu
python3 -m http.server 8000
```
Open your browser to http://localhost:8000
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<canvas class="emscripten" id="canvas" style="position: absolute; left: 0; top: 0;"></canvas>
<script type="text/javascript" src="ConeWebGPU.js"></script>
<script type='text/javascript'>
var Module = {
canvas: (function () {
var canvas = document.getElementById('canvas');
return canvas;
})(),
print: (function () {
return function (text) {
text = Array.prototype.slice.call(arguments).join(' ');
console.info(text);
};
})(),
printErr: function (text) {
text = Array.prototype.slice.call(arguments).join(' ');
console.error(text);
},
onRuntimeInitialized: function () {
console.log('initialized');
},
};
navigator.gpu.requestAdapter().then((adapter) => {
console.log("Found an adapter");
adapter.requestDevice().then((device) => {
console.log("Obtained device");
Module.preinitializedWebGPUDevice = device;
var app = vtkApp(Module);
console.log('App created');
});
});
</script>
</body>
</html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment