Skip to content

Fix for gh issue: 21031, use proper size for WGPUTextureFormat #21034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

warvstar
Copy link
Contributor

@warvstar warvstar commented Jan 8, 2024

Fix for #21031

  • Use proper size for WGPUTextureFormat U64 -> U32

Fix for emscripten-core#21031
- Use proper size for WGPUTextureFormat U64 -> U32
Copy link
Collaborator

@sbc100 sbc100 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@kainino0x would it be hard to add tests for this?

@@ -912,7 +912,7 @@ var LibraryWebGPU = {
var viewFormatCount = {{{ gpu.makeGetU32('descriptor', C_STRUCTS.WGPUTextureDescriptor.viewFormatCount) }}};
if (viewFormatCount) {
var viewFormatsPtr = {{{ makeGetValue('descriptor', C_STRUCTS.WGPUTextureDescriptor.viewFormats, '*') }}};
desc["viewFormats"] = Array.from({{{ makeHEAPView(`${POINTER_BITS}`, 'viewFormatsPtr', `viewFormatsPtr + viewFormatCount * ${POINTER_SIZE}`) }}},
desc["viewFormats"] = Array.from({{{ makeHEAPView(`32`, 'viewFormatsPtr', `viewFormatsPtr + viewFormatCount * 4`) }}},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps a comment along the lines of // viewFormatsPtr pointer to an array of TextureFormat which is an enum of size uint32_t

@@ -912,7 +912,7 @@ var LibraryWebGPU = {
var viewFormatCount = {{{ gpu.makeGetU32('descriptor', C_STRUCTS.WGPUTextureDescriptor.viewFormatCount) }}};
if (viewFormatCount) {
var viewFormatsPtr = {{{ makeGetValue('descriptor', C_STRUCTS.WGPUTextureDescriptor.viewFormats, '*') }}};
desc["viewFormats"] = Array.from({{{ makeHEAPView(`${POINTER_BITS}`, 'viewFormatsPtr', `viewFormatsPtr + viewFormatCount * ${POINTER_SIZE}`) }}},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find, thank you! Glad to find that these bindings work in wasm64 at all :)

We have the same problem in

desc["requiredFeatures"] = Array.from({{{ makeHEAPView(`${POINTER_BITS}`, 'requiredFeaturesPtr', `requiredFeaturesPtr + requiredFeaturesCount * ${POINTER_SIZE}`) }}},
which takes WGPUFeatureName which is 32-bit.

@@ -912,7 +912,7 @@ var LibraryWebGPU = {
var viewFormatCount = {{{ gpu.makeGetU32('descriptor', C_STRUCTS.WGPUTextureDescriptor.viewFormatCount) }}};
if (viewFormatCount) {
var viewFormatsPtr = {{{ makeGetValue('descriptor', C_STRUCTS.WGPUTextureDescriptor.viewFormats, '*') }}};
desc["viewFormats"] = Array.from({{{ makeHEAPView(`${POINTER_BITS}`, 'viewFormatsPtr', `viewFormatsPtr + viewFormatCount * ${POINTER_SIZE}`) }}},
desc["viewFormats"] = Array.from({{{ makeHEAPView(`32`, 'viewFormatsPtr', `viewFormatsPtr + viewFormatCount * 4`) }}},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re: testing, I think we could patch a test into this existing test:

wgpu::TextureDescriptor descriptor{};

         wgpu::TextureDescriptor descriptor{};
         descriptor.usage = wgpu::TextureUsage::RenderAttachment | wgpu::TextureUsage::CopySrc;
         descriptor.size = {1, 1, 1};
         descriptor.format = wgpu::TextureFormat::BGRA8Unorm;
+        // Test for viewFormats binding
+        std::array<wgpu::TextureFormat, 2> viewFormats =
+            { wgpu::TextureFormat::BGRA8Unorm, wgpu::TextureFormat::BGRA8Unorm };
+        descriptor.viewFormatCount = viewFormats.size();
+        descriptor.viewFormats = viewFormats.data();
         readbackTexture = device.CreateTexture(&descriptor);

or something like that. The resulting JS call will be:

// before bugfix, throws TypeError:
d.createTexture({ size: [4, 4], usage: GPUTextureUsage.COPY_SRC, format: 'bgra8unorm',
    viewFormats: [undefined, undefined /* or possibly something else due to buffer overrun */] });
// after bugfix, OK:
d.createTexture({ size: [4, 4], usage: GPUTextureUsage.COPY_SRC, format: 'bgra8unorm',
    viewFormats: ['bgra8unorm', 'bgra8unorm'] });

Unfortunately it wouldn't be quite so easy to test the requiredFeatures one.

@kainino0x kainino0x linked an issue Jan 13, 2024 that may be closed by this pull request
@kainino0x kainino0x enabled auto-merge (squash) January 13, 2024 03:57
@kainino0x kainino0x merged commit 6faed83 into emscripten-core:main Jan 13, 2024
@kainino0x
Copy link
Collaborator

I've addressed the remaining work in #21078

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

memory64 and webgpu texture creation failure.
3 participants