Code Monkey home page Code Monkey logo

spvm's Introduction

SPVM

Tiny SPIR-V virtual machine (interpreter), can be used for shader debugging.

License CMake Linux CMake MacOS CMake Windows

Opcodes support status:

The project is still in progress ...

Spvm-ShaderToy

Spvm-ShaderToy implements shader effects without GPU.

Example

GLSL fragment shader :

#version 450

layout (location = 0) in vec3 inColor;
layout (location = 0) out vec4 outFragColor;

void main()
{
    outFragColor = vec4(inColor.yxz, 1.0f);
}

run with spvm :

#define HEAP_SIZE 128 * 1024
const char *SPV_PATH = "shaders/simple.frag.spv";

int main(int argc, char *argv[]) {
  SPVM::SpvmModule module;
  SPVM::Runtime runtime;

  // decode spir-v file
  bool success = SPVM::Decoder::decodeFile(SPV_PATH, &module);
  if (!success) {
    std::cout << "error decode spir-v file";
    return -1;
  }

  // init module
  success = runtime.initWithModule(&module, HEAP_SIZE);
  if (!success) {
    std::cout << "error init module";
    return -1;
  }

  // get uniform locations
  SPVM::SpvmWord inColorLoc = runtime.getLocationByName("inColor");
  SPVM::SpvmWord outFragColorLoc = runtime.getLocationByName("outFragColor");

  // write input
  float inColor[3]{0.2f, 0.3f, 0.4f};
  runtime.writeInput(inColor, inColorLoc);

  // execute shader entry function 'main'
  runtime.execEntryPoint();

  // read output
  float outFragColor[4];
  runtime.readOutput(outFragColor, outFragColorLoc);

  std::cout << "outFragColor[0]: " << outFragColor[0] << std::endl;
  std::cout << "outFragColor[1]: " << outFragColor[1] << std::endl;
  std::cout << "outFragColor[2]: " << outFragColor[2] << std::endl;
  std::cout << "outFragColor[3]: " << outFragColor[3] << std::endl;

  return 0;
}

Clone

git clone [email protected]:keith2018/spvm.git
cd spvm
git submodule update --init --recursive

Build

mkdir build
cmake -B ./build -DCMAKE_BUILD_TYPE=Release
cmake --build ./build --config Release

Test

cd build
ctest

License

This code is licensed under the MIT License (see LICENSE).

spvm's People

Contributors

keith2018 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.