Giter Site home page Giter Site logo

Comments (4)

maierfelix avatar maierfelix commented on May 22, 2024

This is already possible:

let buf: ArrayBuffer = new ArrayBuffer(VkInstanceCreateInfo.byteLength);
let instanceInfo: VkInstanceCreateInfo = new VkInstanceCreateInfo({
  $memoryBuffer: buf,
  $memoryOffset: 0x0
});

I've added this when adding support for sub-structures - e.g. VkRect2D, where offset and extent are VkOffset2D structs using the same memory as VkRect2D, but with a different offset.

You can check the Default Properties section in the docs, for example here, which shows the underlying methods and properties for low-level access.

What you're probably most interested in are the $memoryBuffer and $memoryOffset properties and the reflect method, which synchronizes the JS Object with the ArrayBuffer.

Structs also come with a memoryLayout object, which describes the byteOffset and byteLength of each struct member, e.g.:

VkInstanceCreateInfo.byteLength = 0x40;
VkInstanceCreateInfo.memoryLayout = {
  sType: {
    byteOffset: 0x0,
    byteLength: 0x4
  },
  pNext: {
    byteOffset: 0x8,
    byteLength: 0x8
  },
  ...
};

Also, if you're interested about how nvk works internally, checkout this file.

Edit:
I'm not sure your question is exactly about this, but I've also created a package callced nvk-struct-cache to mimic stack allocation when working with nvk, to reduce heap pressure.

from nvk.

 avatar commented on May 22, 2024

I'm getting some skills and experience for professional purposes. Just interesting how work these worlds together. So, I trying to combine NVK with AssemblyScript (and run from that script language)…

UPDATE! Needs getAddressFromArrayBuffer and getArrayBufferFromAddress a public, because natively WebAssembly can't handle alike C++ pointers (i.e. addressing "limited" local-memory). Ah, I found that way...

from nvk.

maierfelix avatar maierfelix commented on May 22, 2024

nvk extends the ArrayBuffer Object with additional low-level methods - see the docs here

Note that one minor problem with Wasm is that it is in a fixed 32bit world, while NAPI is 64bit, so when you fill a Vulkan struct in Wasm, the memory doesn't align correctly.

I don't know your Wasm setup, but you might do it like this (pseudo code):

Setup:

let heap = new ArrayBuffer(2<<15);
let heapU32 = new Uint32Array(heap);
let module = new WebAssembly.Module(buffer);
let instance = new WebAssembly.Instance(module);

In C or AssemblyScript:

int test() {
  VkInstanceCreateInfo* info = (VkInstanceCreateInfo*) malloc(sizeof(VkInstanceCreateInfo));
  return (int) info;
}

In JS:

let infoAddr = instance.test();
let infoAddrView = new VkInstanceCreateInfo({
  $memoryBuffer: heap,
  $memoryOffset: infoAddr
});

from nvk.

 avatar commented on May 22, 2024

I already tried to use with AssemblyScript and custom methods (with structure's). I found some critical bugs when create VkInstanceCreateInfo from WebAssembly memory with offset: no extension layers lists, application crash with that structure. I checked alignment, but has correct. Probably, there is internal "get" and "set" issues. Also in WASM structures I used globalify hack for pointers.

from nvk.

Related Issues (20)

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.