Compare commits
7 Commits
v1.61.0
...
ebridgewat
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
31f96c9065 | ||
|
|
1305303f2e | ||
|
|
8532511107 | ||
|
|
b762ccb78c | ||
|
|
3296eb153c | ||
|
|
ae6d1028f7 | ||
|
|
92579b6307 |
@@ -204,23 +204,15 @@ public:
|
||||
}, new T(std::forward<T>(functor))
|
||||
};
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Computes the size in bytes needed to fit an image of given dimensions and format
|
||||
* Computes the size in bytes for a pixel of given dimensions and format
|
||||
*
|
||||
* @param format Format of the image pixels
|
||||
* @param type Type of the image pixels
|
||||
* @param stride Stride of a row in pixels
|
||||
* @param height Height of the image in rows
|
||||
* @param alignment Alignment in bytes of pixel rows
|
||||
* @return The buffer size needed to fit this image in bytes
|
||||
* @return The size of the specified pixel in bytes
|
||||
*/
|
||||
static constexpr size_t computeDataSize(PixelDataFormat format, PixelDataType type,
|
||||
size_t stride, size_t height, size_t alignment) noexcept {
|
||||
assert_invariant(alignment);
|
||||
|
||||
static constexpr size_t computePixelSize(PixelDataFormat format, PixelDataType type) noexcept {
|
||||
if (type == PixelDataType::COMPRESSED) {
|
||||
return 0;
|
||||
}
|
||||
@@ -242,7 +234,7 @@ public:
|
||||
case PixelDataFormat::RGB_INTEGER:
|
||||
n = 3;
|
||||
break;
|
||||
case PixelDataFormat::UNUSED: // shouldn't happen (used to be rgbm)
|
||||
case PixelDataFormat::UNUSED:// shouldn't happen (used to be rgbm)
|
||||
case PixelDataFormat::RGBA:
|
||||
case PixelDataFormat::RGBA_INTEGER:
|
||||
n = 4;
|
||||
@@ -251,7 +243,7 @@ public:
|
||||
|
||||
size_t bpp = n;
|
||||
switch (type) {
|
||||
case PixelDataType::COMPRESSED: // Impossible -- to squash the IDE warnings
|
||||
case PixelDataType::COMPRESSED:// Impossible -- to squash the IDE warnings
|
||||
case PixelDataType::UBYTE:
|
||||
case PixelDataType::BYTE:
|
||||
// nothing to do
|
||||
@@ -282,16 +274,35 @@ public:
|
||||
bpp = 2;
|
||||
break;
|
||||
}
|
||||
return bpp;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Computes the size in bytes needed to fit an image of given dimensions and format
|
||||
*
|
||||
* @param format Format of the image pixels
|
||||
* @param type Type of the image pixels
|
||||
* @param stride Stride of a row in pixels
|
||||
* @param height Height of the image in rows
|
||||
* @param alignment Alignment in bytes of pixel rows
|
||||
* @return The buffer size needed to fit this image in bytes
|
||||
*/
|
||||
static constexpr size_t computeDataSize(PixelDataFormat format, PixelDataType type,
|
||||
size_t stride, size_t height, size_t alignment) noexcept {
|
||||
assert_invariant(alignment);
|
||||
|
||||
size_t bpp = computePixelSize(format, type);
|
||||
size_t const bpr = bpp * stride;
|
||||
size_t const bprAligned = (bpr + (alignment - 1)) & (~alignment + 1);
|
||||
return bprAligned * height;
|
||||
}
|
||||
|
||||
//! left coordinate in pixels
|
||||
uint32_t left = 0;
|
||||
uint32_t left = 0;
|
||||
//! top coordinate in pixels
|
||||
uint32_t top = 0;
|
||||
uint32_t top = 0;
|
||||
union {
|
||||
struct {
|
||||
//! stride in pixels
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
#include <backend/DriverEnums.h>
|
||||
#include <backend/Handle.h>
|
||||
#include <backend/TargetBufferInfo.h>
|
||||
|
||||
#include <private/backend/BackendUtils.h>
|
||||
#include <math/mat3.h>
|
||||
#include <utils/CString.h>
|
||||
#include <utils/Panic.h>
|
||||
@@ -39,6 +39,7 @@
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
@@ -353,6 +354,17 @@ void WebGPUDriver::destroyProgram(Handle<HwProgram> ph) {
|
||||
}
|
||||
|
||||
void WebGPUDriver::destroyRenderTarget(Handle<HwRenderTarget> rth) {
|
||||
if (rth) {
|
||||
WGPURenderTarget* rt = handleCast<WGPURenderTarget>(rth);
|
||||
if (rt == mDefaultRenderTarget) {
|
||||
mDefaultRenderTarget = nullptr;
|
||||
}
|
||||
// WGPURenderTarget destructor is trivial.
|
||||
// The HwTexture handles stored within WGPURenderTarget (via MRT, TargetBufferInfo)
|
||||
// are not owned by WGPURenderTarget, so they are not destroyed here.
|
||||
// They are destroyed via WebGPUDriver::destroyTexture.
|
||||
destructHandle<WGPURenderTarget>(rth);
|
||||
}
|
||||
}
|
||||
|
||||
void WebGPUDriver::destroySwapChain(Handle<HwSwapChain> sch) {
|
||||
@@ -579,7 +591,21 @@ void WebGPUDriver::createDefaultRenderTargetR(Handle<HwRenderTarget> rth, int) {
|
||||
|
||||
void WebGPUDriver::createRenderTargetR(Handle<HwRenderTarget> rth, TargetBufferFlags targets,
|
||||
uint32_t width, uint32_t height, uint8_t samples, uint8_t layerCount, MRT color,
|
||||
TargetBufferInfo depth, TargetBufferInfo stencil) {}
|
||||
TargetBufferInfo depth, TargetBufferInfo stencil) {
|
||||
// The `targets` flags indicate which of the `color`, `depth`, `stencil` TargetBufferInfo
|
||||
// are actually active for this render target.
|
||||
// We'll pass all TargetBufferInfo to WGPURenderTarget; it will use them if their handles are valid.
|
||||
|
||||
// Ensure that textures intended for use as attachments were created with
|
||||
// wgpu::TextureUsage::RenderAttachment. This check should ideally be in createTextureR
|
||||
// or validated here if possible.
|
||||
|
||||
// The `layerCount` parameter might be for creating array textures that this RT targets.
|
||||
// Individual attachments (color[i].layer, depth.layer, stencil.layer) specify which layer
|
||||
// of an array texture to bind. For now, we assume textures are pre-configured.
|
||||
|
||||
constructHandle<WGPURenderTarget>(rth, width, height, samples, color, depth, stencil);
|
||||
}
|
||||
|
||||
void WebGPUDriver::createFenceR(Handle<HwFence> fh, int) {}
|
||||
|
||||
@@ -752,11 +778,72 @@ void WebGPUDriver::setVertexBufferObject(Handle<HwVertexBuffer> vbh, uint32_t in
|
||||
vertexBuffer->buffers[index] = bufferObject->getBuffer();
|
||||
}
|
||||
|
||||
void WebGPUDriver::update3DImage(Handle<HwTexture> th,
|
||||
uint32_t level, uint32_t xoffset, uint32_t yoffset, uint32_t zoffset,
|
||||
uint32_t width, uint32_t height, uint32_t depth,
|
||||
PixelBufferDescriptor&& data) {
|
||||
scheduleDestroy(std::move(data));
|
||||
void WebGPUDriver::update3DImage(Handle<HwTexture> th, uint32_t level, uint32_t xoffset,
|
||||
uint32_t yoffset, uint32_t zoffset, uint32_t width, uint32_t height, uint32_t depth,
|
||||
PixelBufferDescriptor&& p) {
|
||||
PixelBufferDescriptor* data = &p;
|
||||
PixelBufferDescriptor reshapedData;
|
||||
if (reshape(p, reshapedData)) {
|
||||
data = &reshapedData;
|
||||
}
|
||||
auto texture = handleCast<WGPUTexture>(th);
|
||||
|
||||
// TODO: Writing to a depth texture is illegal and errors. I'm not sure why Filament is trying
|
||||
// to do so, but early returning is working?
|
||||
if(texture->getAspect() == wgpu::TextureAspect::DepthOnly){
|
||||
scheduleDestroy(std::move(p));
|
||||
return;
|
||||
}
|
||||
size_t blockWidth = texture->getBlockWidth();
|
||||
size_t blockHeight = texture->getBlockHeight();
|
||||
// WebGPU specification requires that for compressed textures, the x and y offsets
|
||||
// must be a multiple of the compressed texture format's block width and height.
|
||||
// See: https://www.w3.org/TR/webgpu/#abstract-opdef-validating-gputexelcopytextureinfo
|
||||
if (blockWidth > 1 || blockHeight > 1) {
|
||||
FILAMENT_CHECK_PRECONDITION(xoffset % blockWidth == 0)
|
||||
<< "xoffset must be aligned to blockwidth, but offset is " << blockWidth
|
||||
<< "and offset is " << xoffset;
|
||||
FILAMENT_CHECK_PRECONDITION(yoffset % blockHeight == 0)
|
||||
<< "yoffset must be aligned to blockHeight, but offset is " << blockHeight
|
||||
<< "and offset is " << yoffset;
|
||||
}
|
||||
|
||||
auto copyInfo = wgpu::TexelCopyTextureInfo{ .texture = texture->getTexture(),
|
||||
.mipLevel = level,
|
||||
.origin = { .x = xoffset, .y = yoffset, .z = zoffset },
|
||||
.aspect = texture->getAspect() };
|
||||
uint32_t bytesPerRow = static_cast<uint32_t>(
|
||||
PixelBufferDescriptor::computePixelSize(data->format, data->type) * width);
|
||||
auto extent = wgpu::Extent3D{ .width = width, .height = height, .depthOrArrayLayers = depth };
|
||||
|
||||
const uint8_t* dataBuff = static_cast<const uint8_t*>(data->buffer);
|
||||
size_t dataSize = data->size;
|
||||
std::unique_ptr<uint8_t[]> paddedBuffer;
|
||||
|
||||
if (bytesPerRow % 256 != 0) {
|
||||
uint32_t padding = 256 - (bytesPerRow % 256);
|
||||
uint32_t paddedBytesPerRow = bytesPerRow + padding;
|
||||
|
||||
size_t paddedBufferSize = static_cast<size_t>(paddedBytesPerRow) * height * depth;
|
||||
paddedBuffer = std::make_unique<uint8_t[]>(paddedBufferSize);
|
||||
uint8_t* dest = paddedBuffer.get();
|
||||
|
||||
for (uint32_t z = 0; z < depth; ++z) {
|
||||
for (uint32_t y = 0; y < height; ++y) {
|
||||
std::memcpy(dest, dataBuff, bytesPerRow);
|
||||
dest += paddedBytesPerRow;
|
||||
dataBuff += bytesPerRow;
|
||||
}
|
||||
}
|
||||
dataBuff = paddedBuffer.get();
|
||||
dataSize = paddedBufferSize;
|
||||
bytesPerRow = paddedBytesPerRow;
|
||||
}
|
||||
|
||||
auto layout = wgpu::TexelCopyBufferLayout{ .bytesPerRow = bytesPerRow, .rowsPerImage = height };
|
||||
|
||||
mQueue.WriteTexture(©Info, dataBuff, dataSize, &layout, &extent);
|
||||
scheduleDestroy(std::move(p));
|
||||
}
|
||||
|
||||
void WebGPUDriver::setupExternalImage(void* image) {
|
||||
@@ -783,35 +870,74 @@ void WebGPUDriver::compilePrograms(CompilerPriorityQueue priority,
|
||||
|
||||
void WebGPUDriver::beginRenderPass(Handle<HwRenderTarget> rth, RenderPassParams const& params) {
|
||||
assert_invariant(mCommandEncoder);
|
||||
|
||||
auto* renderTarget = handleCast<WGPURenderTarget>(rth);
|
||||
// if (renderTarget == mDefaultRenderTarget) {
|
||||
// FWGPU_LOGW << "Default render target"
|
||||
// << utils::io::endl;
|
||||
// } else {
|
||||
// FWGPU_LOGW << "Non Default render target"
|
||||
// << utils::io::endl;
|
||||
// }
|
||||
wgpu::RenderPassDescriptor renderPassDescriptor;
|
||||
wgpu::RenderPassDepthStencilAttachment depthStencilAttachment{
|
||||
.view = mSwapChain->getDepthTextureView(),
|
||||
.depthLoadOp = WGPURenderTarget::getLoadOperation(params, TargetBufferFlags::DEPTH),
|
||||
.depthStoreOp = WGPURenderTarget::getStoreOperation(params, TargetBufferFlags::DEPTH),
|
||||
.depthClearValue = static_cast<float>(params.clearDepth),
|
||||
.depthReadOnly = (params.readOnlyDepthStencil & RenderPassParams::READONLY_DEPTH) > 0,
|
||||
.stencilLoadOp = WGPURenderTarget::getLoadOperation(params, TargetBufferFlags::STENCIL),
|
||||
.stencilStoreOp = WGPURenderTarget::getStoreOperation(params, TargetBufferFlags::STENCIL),
|
||||
.stencilClearValue = params.clearStencil,
|
||||
.stencilReadOnly = (params.readOnlyDepthStencil & RenderPassParams::READONLY_STENCIL) > 0
|
||||
};
|
||||
renderTarget->setUpRenderPassAttachments(renderPassDescriptor, mTextureView, params);
|
||||
renderPassDescriptor.depthStencilAttachment = &depthStencilAttachment;
|
||||
|
||||
assert_invariant(mTextureView);
|
||||
wgpu::RenderPassDescriptor renderPassDescriptor{};
|
||||
wgpu::TextureView defaultColorView = nullptr;
|
||||
wgpu::TextureView defaultDepthStencilView = nullptr;
|
||||
|
||||
std::array<wgpu::TextureView, MRT::MAX_SUPPORTED_RENDER_TARGET_COUNT> customColorViews{};
|
||||
uint32_t customColorViewCount = 0;
|
||||
wgpu::TextureView customDepthView = nullptr;
|
||||
wgpu::TextureView customStencilView = nullptr;
|
||||
|
||||
if (renderTarget->isDefaultRenderTarget()) {
|
||||
assert_invariant(mSwapChain && mTextureView);
|
||||
defaultColorView = mTextureView;
|
||||
defaultDepthStencilView = mSwapChain->getDepthTextureView();
|
||||
} else {
|
||||
// Resolve views for custom render target
|
||||
const auto& colorInfos = renderTarget->getColorAttachmentInfos();
|
||||
for (int i = 0; i < MRT::MAX_SUPPORTED_RENDER_TARGET_COUNT; ++i) {
|
||||
if (colorInfos[i].handle) {
|
||||
auto* hwTexture = handleCast<WGPUTexture>(colorInfos[i].handle);
|
||||
if (hwTexture) {
|
||||
// TODO: Consider colorInfos[i].level and colorInfos[i].layer for view creation
|
||||
// if WGPUTexture::getTexView() isn't sufficient or needs parameters.
|
||||
customColorViews[customColorViewCount++] = hwTexture->getTexView();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const auto& depthInfo = renderTarget->getDepthAttachmentInfo();
|
||||
if (depthInfo.handle) {
|
||||
auto* hwTexture = handleCast<WGPUTexture>(depthInfo.handle);
|
||||
if (hwTexture) {
|
||||
customDepthView = hwTexture->getTexView();
|
||||
}
|
||||
}
|
||||
|
||||
const auto& stencilInfo = renderTarget->getStencilAttachmentInfo();
|
||||
if (stencilInfo.handle) {
|
||||
// If depth and stencil use the same texture handle, this will re-cast but that's fine.
|
||||
auto* hwTexture = handleCast<WGPUTexture>(stencilInfo.handle);
|
||||
if (hwTexture) {
|
||||
customStencilView = hwTexture->getTexView();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
renderTarget->setUpRenderPassAttachments(renderPassDescriptor,
|
||||
params,
|
||||
defaultColorView,
|
||||
defaultDepthStencilView,
|
||||
customColorViews.data(),
|
||||
customColorViewCount,
|
||||
customDepthView,
|
||||
customStencilView);
|
||||
|
||||
mRenderPassEncoder = mCommandEncoder.BeginRenderPass(&renderPassDescriptor);
|
||||
mRenderPassEncoder.SetViewport(params.viewport.left, params.viewport.bottom,
|
||||
params.viewport.width, params.viewport.height, params.depthRange.near, params.depthRange.far);
|
||||
// Ensure viewport dimensions are positive
|
||||
uint32_t viewportWidth = params.viewport.width > 0 ? params.viewport.width : 1;
|
||||
uint32_t viewportHeight = params.viewport.height > 0 ? params.viewport.height : 1;
|
||||
|
||||
mRenderPassEncoder.SetViewport(
|
||||
static_cast<float>(params.viewport.left),
|
||||
static_cast<float>(params.viewport.bottom),
|
||||
static_cast<float>(viewportWidth),
|
||||
static_cast<float>(viewportHeight),
|
||||
params.depthRange.near,
|
||||
params.depthRange.far);
|
||||
}
|
||||
|
||||
void WebGPUDriver::endRenderPass(int /* dummy */) {
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
|
||||
#include <utils/BitmaskEnum.h>
|
||||
#include <utils/Panic.h>
|
||||
|
||||
#include <private/backend/BackendUtils.h>
|
||||
#include <webgpu/webgpu_cpp.h>
|
||||
|
||||
#include <algorithm>
|
||||
@@ -68,7 +68,7 @@ wgpu::VertexFormat getVertexFormat(filament::backend::ElementType type, bool nor
|
||||
case ElementType::BYTE4: return VertexFormat::Snorm8x4;
|
||||
case ElementType::UBYTE4: return VertexFormat::Unorm8x4;
|
||||
case ElementType::SHORT4: return VertexFormat::Snorm16x4;
|
||||
case ElementType::USHORT4: return VertexFormat::Unorm8x4;
|
||||
case ElementType::USHORT4: return VertexFormat::Unorm16x4;
|
||||
default:
|
||||
FILAMENT_CHECK_POSTCONDITION(false) << "Normalized format does not exist.";
|
||||
return VertexFormat::Float32x3;
|
||||
@@ -502,6 +502,8 @@ WGPUTexture::WGPUTexture(SamplerType target, uint8_t levels, TextureFormat forma
|
||||
mUsage = fToWGPUTextureUsage(usage);
|
||||
mFormat = fToWGPUTextureFormat(format);
|
||||
mAspect = fToWGPUTextureViewAspect(usage, format);
|
||||
mBlockWidth = filament::backend::getBlockWidth(format);
|
||||
mBlockHeight = filament::backend::getBlockHeight(format);
|
||||
wgpu::TextureDescriptor textureDescriptor{
|
||||
.label = getUserTextureLabel(target),
|
||||
.usage = mUsage,
|
||||
@@ -548,6 +550,10 @@ WGPUTexture::WGPUTexture(SamplerType target, uint8_t levels, TextureFormat forma
|
||||
|
||||
WGPUTexture::WGPUTexture(WGPUTexture* src, uint8_t baseLevel, uint8_t levelCount) noexcept {
|
||||
mTexture = src->mTexture;
|
||||
mAspect = src->mAspect;
|
||||
mBlockWidth = src->mBlockWidth;
|
||||
mBlockHeight = src->mBlockHeight;
|
||||
|
||||
mTexView = makeTextureView(baseLevel, levelCount, target);
|
||||
}
|
||||
|
||||
@@ -917,58 +923,147 @@ wgpu::TextureView WGPUTexture::makeTextureView(const uint8_t& baseLevel, const u
|
||||
return textureView;
|
||||
}
|
||||
|
||||
WGPURenderTarget::Attachment WGPURenderTarget::getDrawColorAttachment(size_t index) {
|
||||
assert_invariant( index < MRT::MAX_SUPPORTED_RENDER_TARGET_COUNT);
|
||||
auto result = color[index];
|
||||
if (index == 0 && defaultRenderTarget) {
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
WGPURenderTarget::WGPURenderTarget(uint32_t width, uint32_t height, uint8_t samples,
|
||||
const MRT& colorAttachmentsMRT,
|
||||
const Attachment& depthAttachmentInfo,
|
||||
const Attachment& stencilAttachmentInfo)
|
||||
: HwRenderTarget(width, height),
|
||||
defaultRenderTarget(false),
|
||||
samples(samples),
|
||||
mColorAttachments(colorAttachmentsMRT),
|
||||
mDepthAttachment(depthAttachmentInfo),
|
||||
mStencilAttachment(stencilAttachmentInfo) {
|
||||
mColorAttachmentDescriptors.reserve(MRT::MAX_SUPPORTED_RENDER_TARGET_COUNT);
|
||||
}
|
||||
|
||||
// Static helper to map MRT index to TargetBufferFlags
|
||||
TargetBufferFlags WGPURenderTarget::getTargetBufferFlagsAt(int mrtIndex) {
|
||||
if (mrtIndex < 0 || mrtIndex >= MRT::MAX_SUPPORTED_RENDER_TARGET_COUNT) {
|
||||
return TargetBufferFlags::NONE;
|
||||
}
|
||||
// This mapping assumes TargetBufferFlags::COLOR_0, COLOR_1 etc. are contiguous
|
||||
return static_cast<TargetBufferFlags>(
|
||||
static_cast<uint32_t>(TargetBufferFlags::COLOR0) << mrtIndex);
|
||||
}
|
||||
|
||||
// Corrected getLoadOperation
|
||||
wgpu::LoadOp WGPURenderTarget::getLoadOperation(RenderPassParams const& params,
|
||||
TargetBufferFlags buffer) {
|
||||
auto clearFlags = params.flags.clear;
|
||||
auto discardStartFlags = params.flags.discardStart;
|
||||
if (any(clearFlags & buffer)) {
|
||||
return wgpu::LoadOp::Clear;
|
||||
} else if (any(discardStartFlags & buffer)) {
|
||||
TargetBufferFlags bufferToOperateOn) {
|
||||
if (any(params.flags.clear & bufferToOperateOn)) {
|
||||
return wgpu::LoadOp::Clear;
|
||||
}
|
||||
if (any(params.flags.discardStart & bufferToOperateOn)) {
|
||||
return wgpu::LoadOp::Clear; // Or wgpu::LoadOp::Undefined if clear is not desired on discard
|
||||
}
|
||||
return wgpu::LoadOp::Load;
|
||||
}
|
||||
|
||||
// Corrected getStoreOperation
|
||||
wgpu::StoreOp WGPURenderTarget::getStoreOperation(RenderPassParams const& params,
|
||||
TargetBufferFlags buffer) {
|
||||
const auto discardEndFlags = params.flags.discardEnd;
|
||||
if (any(discardEndFlags & buffer)) {
|
||||
TargetBufferFlags bufferToOperateOn) {
|
||||
if (any(params.flags.discardEnd & bufferToOperateOn)) {
|
||||
return wgpu::StoreOp::Discard;
|
||||
}
|
||||
return wgpu::StoreOp::Store;
|
||||
}
|
||||
void WGPURenderTarget::setUpRenderPassAttachments(wgpu::RenderPassDescriptor& descriptor,
|
||||
wgpu::TextureView const& textureView, RenderPassParams const& params) {
|
||||
// auto discardFlags = params.flags.discardEnd;
|
||||
// (void) discardFlags;
|
||||
// std::vector<wgpu::RenderPassColorAttachment> colorAttachments;
|
||||
colorAttachments.clear();
|
||||
for (size_t i = 0; i < 1/*MRT::MAX_SUPPORTED_RENDER_TARGET_COUNT*/; i++) {
|
||||
// auto attachment = getDrawColorAttachment(i);
|
||||
// if (attachment) {
|
||||
wgpu::RenderPassColorAttachment colorAttachment;
|
||||
colorAttachment.view = textureView;
|
||||
colorAttachment.loadOp = getLoadOperation(params, getTargetBufferFlagsAt(i));
|
||||
colorAttachment.storeOp = getStoreOperation(params, getTargetBufferFlagsAt(i));
|
||||
colorAttachment.clearValue = { params.clearColor.r, params.clearColor.g, params.clearColor.b, params.clearColor.a };
|
||||
colorAttachments.emplace_back(colorAttachment);
|
||||
// }
|
||||
|
||||
void WGPURenderTarget::setUpRenderPassAttachments(
|
||||
wgpu::RenderPassDescriptor& descriptor,
|
||||
RenderPassParams const& params,
|
||||
wgpu::TextureView const& defaultColorTextureView,
|
||||
wgpu::TextureView const& defaultDepthStencilTextureView,
|
||||
wgpu::TextureView const* customColorTextureViews,
|
||||
uint32_t customColorTextureViewCount,
|
||||
wgpu::TextureView const& customDepthTextureView,
|
||||
wgpu::TextureView const& customStencilTextureView) {
|
||||
mColorAttachmentDescriptors.clear();
|
||||
mHasDepthStencilAttachment = false;
|
||||
|
||||
if (defaultRenderTarget) {
|
||||
assert_invariant(defaultColorTextureView);
|
||||
wgpu::RenderPassColorAttachment colorAttDesc{};
|
||||
colorAttDesc.view = defaultColorTextureView;
|
||||
colorAttDesc.resolveTarget = nullptr;
|
||||
colorAttDesc.loadOp = WGPURenderTarget::getLoadOperation(params, TargetBufferFlags::COLOR0);
|
||||
colorAttDesc.storeOp = WGPURenderTarget::getStoreOperation(params, TargetBufferFlags::COLOR0);
|
||||
colorAttDesc.clearValue = {params.clearColor.r, params.clearColor.g,
|
||||
params.clearColor.b, params.clearColor.a};
|
||||
mColorAttachmentDescriptors.push_back(colorAttDesc);
|
||||
|
||||
if (defaultDepthStencilTextureView) {
|
||||
mDepthStencilAttachmentDescriptor = {
|
||||
.view = defaultDepthStencilTextureView,
|
||||
.depthLoadOp = WGPURenderTarget::getLoadOperation(params, TargetBufferFlags::DEPTH),
|
||||
.depthStoreOp = WGPURenderTarget::getStoreOperation(params, TargetBufferFlags::DEPTH),
|
||||
.depthClearValue = static_cast<float>(params.clearDepth),
|
||||
.depthReadOnly = (params.readOnlyDepthStencil & RenderPassParams::READONLY_DEPTH) > 0,
|
||||
.stencilLoadOp = WGPURenderTarget::getLoadOperation(params, TargetBufferFlags::STENCIL),
|
||||
.stencilStoreOp = WGPURenderTarget::getStoreOperation(params, TargetBufferFlags::STENCIL),
|
||||
.stencilClearValue = params.clearStencil,
|
||||
.stencilReadOnly = (params.readOnlyDepthStencil & RenderPassParams::READONLY_STENCIL) > 0,
|
||||
};
|
||||
mHasDepthStencilAttachment = true;
|
||||
}
|
||||
} else { // Custom Render Target
|
||||
for (uint32_t i = 0; i < customColorTextureViewCount; ++i) {
|
||||
if (customColorTextureViews[i]) {
|
||||
wgpu::RenderPassColorAttachment colorAttDesc{};
|
||||
colorAttDesc.view = customColorTextureViews[i];
|
||||
colorAttDesc.resolveTarget = nullptr; // TODO: MSAA resolve for custom RT
|
||||
colorAttDesc.loadOp = WGPURenderTarget::getLoadOperation(params, getTargetBufferFlagsAt(i));
|
||||
colorAttDesc.storeOp = WGPURenderTarget::getStoreOperation(params, getTargetBufferFlagsAt(i));
|
||||
colorAttDesc.clearValue = {params.clearColor.r, params.clearColor.g,
|
||||
params.clearColor.b, params.clearColor.a};
|
||||
mColorAttachmentDescriptors.push_back(colorAttDesc);
|
||||
}
|
||||
}
|
||||
|
||||
wgpu::TextureView combinedDsView = customDepthTextureView ? customDepthTextureView : customStencilTextureView;
|
||||
|
||||
if (combinedDsView) {
|
||||
mDepthStencilAttachmentDescriptor = {};
|
||||
mDepthStencilAttachmentDescriptor.view = combinedDsView;
|
||||
|
||||
if (customDepthTextureView) {
|
||||
mDepthStencilAttachmentDescriptor.depthLoadOp = WGPURenderTarget::getLoadOperation(params, TargetBufferFlags::DEPTH);
|
||||
mDepthStencilAttachmentDescriptor.depthStoreOp = WGPURenderTarget::getStoreOperation(params, TargetBufferFlags::DEPTH);
|
||||
mDepthStencilAttachmentDescriptor.depthClearValue = static_cast<float>(params.clearDepth);
|
||||
mDepthStencilAttachmentDescriptor.depthReadOnly = (params.readOnlyDepthStencil & RenderPassParams::READONLY_DEPTH) > 0;
|
||||
} else {
|
||||
mDepthStencilAttachmentDescriptor.depthLoadOp = wgpu::LoadOp::Undefined;
|
||||
mDepthStencilAttachmentDescriptor.depthStoreOp = wgpu::StoreOp::Undefined;
|
||||
mDepthStencilAttachmentDescriptor.depthReadOnly = true;
|
||||
}
|
||||
|
||||
if (customStencilTextureView) {
|
||||
mDepthStencilAttachmentDescriptor.stencilLoadOp = WGPURenderTarget::getLoadOperation(params, TargetBufferFlags::STENCIL);
|
||||
mDepthStencilAttachmentDescriptor.stencilStoreOp = WGPURenderTarget::getStoreOperation(params, TargetBufferFlags::STENCIL);
|
||||
mDepthStencilAttachmentDescriptor.stencilClearValue = params.clearStencil;
|
||||
mDepthStencilAttachmentDescriptor.stencilReadOnly = (params.readOnlyDepthStencil & RenderPassParams::READONLY_STENCIL) > 0;
|
||||
} else {
|
||||
mDepthStencilAttachmentDescriptor.stencilLoadOp = wgpu::LoadOp::Undefined;
|
||||
mDepthStencilAttachmentDescriptor.stencilStoreOp = wgpu::StoreOp::Undefined;
|
||||
mDepthStencilAttachmentDescriptor.stencilReadOnly = true;
|
||||
}
|
||||
mHasDepthStencilAttachment = true;
|
||||
}
|
||||
}
|
||||
descriptor.colorAttachments = colorAttachments.data();
|
||||
descriptor.colorAttachmentCount = colorAttachments.size();
|
||||
descriptor.depthStencilAttachment = nullptr;
|
||||
descriptor.timestampWrites = nullptr;
|
||||
|
||||
descriptor.colorAttachmentCount = mColorAttachmentDescriptors.size();
|
||||
descriptor.colorAttachments = mColorAttachmentDescriptors.data();
|
||||
descriptor.depthStencilAttachment = mHasDepthStencilAttachment ? &mDepthStencilAttachmentDescriptor : nullptr;
|
||||
// descriptor.sampleCount was removed from the core spec. If your webgpu.h still has it,
|
||||
// and your Dawn version expects it, you might need to set it here based on this->samples.
|
||||
// e.g., descriptor.sampleCount = this->samples;
|
||||
}
|
||||
|
||||
math::uint2 WGPURenderTarget::getAttachmentSize() const noexcept {
|
||||
if (!defaultRenderTarget) {
|
||||
return {width, height};
|
||||
}
|
||||
// For default RT, size is dynamic and usually obtained from the swapchain.
|
||||
// The caller (WebGPUDriver::beginRenderPass) should know the current swapchain size.
|
||||
return {0,0};
|
||||
}
|
||||
|
||||
}// namespace filament::backend
|
||||
|
||||
@@ -161,6 +161,9 @@ public:
|
||||
wgpu::Device const& device) noexcept;
|
||||
|
||||
WGPUTexture(WGPUTexture* src, uint8_t baseLevel, uint8_t levelCount) noexcept;
|
||||
wgpu::TextureAspect getAspect() const { return mAspect; }
|
||||
size_t getBlockWidth() const { return mBlockWidth; }
|
||||
size_t getBlockHeight() const { return mBlockHeight; }
|
||||
|
||||
[[nodiscard]] const wgpu::Texture& getTexture() const { return mTexture; }
|
||||
[[nodiscard]] const wgpu::TextureView& getTexView() const { return mTexView; }
|
||||
@@ -184,6 +187,8 @@ private:
|
||||
uint32_t mArrayLayerCount = 1;
|
||||
wgpu::TextureView mTexView = nullptr;
|
||||
wgpu::TextureUsage fToWGPUTextureUsage(filament::backend::TextureUsage const& fUsage);
|
||||
size_t mBlockWidth;
|
||||
size_t mBlockHeight;
|
||||
};
|
||||
|
||||
struct WGPURenderPrimitive : public HwRenderPrimitive {
|
||||
@@ -192,57 +197,66 @@ struct WGPURenderPrimitive : public HwRenderPrimitive {
|
||||
WGPUIndexBuffer* indexBuffer = nullptr;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class WGPURenderTarget : public HwRenderTarget {
|
||||
public:
|
||||
class Attachment {
|
||||
public:
|
||||
friend class WGPURenderTarget;
|
||||
|
||||
Attachment() = default;
|
||||
Attachment(WGPUTexture* gpuTexture, uint8_t level = 0, uint16_t layer = 0)
|
||||
: level(level),
|
||||
layer(layer),
|
||||
texture(gpuTexture->getTexture()),
|
||||
mWGPUTexture(gpuTexture) {}
|
||||
operator bool() const {
|
||||
return mWGPUTexture != nullptr;
|
||||
}
|
||||
|
||||
uint8_t level = 0;
|
||||
uint16_t layer = 0;
|
||||
|
||||
private:
|
||||
wgpu::Texture texture = nullptr;
|
||||
WGPUTexture* mWGPUTexture = nullptr;
|
||||
};
|
||||
using Attachment = TargetBufferInfo; // Using TargetBufferInfo directly for attachments
|
||||
|
||||
WGPURenderTarget(uint32_t width, uint32_t height, uint8_t samples,
|
||||
Attachment colorAttachments[MRT::MAX_SUPPORTED_RENDER_TARGET_COUNT]);
|
||||
const MRT& colorAttachments,
|
||||
const Attachment& depthAttachment,
|
||||
const Attachment& stencilAttachment);
|
||||
|
||||
// Default constructor for the default render target
|
||||
WGPURenderTarget()
|
||||
: HwRenderTarget(0, 0),
|
||||
defaultRenderTarget(true) {}
|
||||
defaultRenderTarget(true),
|
||||
samples(1) {}
|
||||
|
||||
void setUpRenderPassAttachments(wgpu::RenderPassDescriptor& descriptor,
|
||||
wgpu::TextureView const& textureView, RenderPassParams const& params);
|
||||
// Updated signature: takes resolved views for custom RTs, and default views for default RT
|
||||
void setUpRenderPassAttachments(
|
||||
wgpu::RenderPassDescriptor& descriptor,
|
||||
RenderPassParams const& params,
|
||||
// For default render target:
|
||||
wgpu::TextureView const& defaultColorTextureView,
|
||||
wgpu::TextureView const& defaultDepthStencilTextureView,
|
||||
// For custom render targets:
|
||||
wgpu::TextureView const* customColorTextureViews, // Array of views
|
||||
uint32_t customColorTextureViewCount,
|
||||
wgpu::TextureView const& customDepthTextureView,
|
||||
wgpu::TextureView const& customStencilTextureView);
|
||||
|
||||
math::uint2 getAttachmentSize() noexcept;
|
||||
|
||||
math::uint2 getAttachmentSize() const noexcept;
|
||||
|
||||
bool isDefaultRenderTarget() const { return defaultRenderTarget; }
|
||||
uint8_t getSamples() const { return samples; }
|
||||
|
||||
Attachment getDrawColorAttachment(size_t index);
|
||||
Attachment getReadColorAttachment(size_t index);
|
||||
// Accessors for the driver to get stored attachment info
|
||||
const MRT& getColorAttachmentInfos() const { return mColorAttachments; }
|
||||
const Attachment& getDepthAttachmentInfo() const { return mDepthAttachment; }
|
||||
const Attachment& getStencilAttachmentInfo() const { return mStencilAttachment; }
|
||||
|
||||
// Static helpers for load/store operations
|
||||
static wgpu::LoadOp getLoadOperation(const RenderPassParams& params, TargetBufferFlags buffer);
|
||||
static wgpu::StoreOp getStoreOperation(const RenderPassParams& params, TargetBufferFlags buffer);
|
||||
|
||||
private:
|
||||
// Helper to map MRT index to TargetBufferFlags
|
||||
static TargetBufferFlags getTargetBufferFlagsAt(int mrtIndex);
|
||||
|
||||
bool defaultRenderTarget = false;
|
||||
uint8_t samples = 1;
|
||||
|
||||
Attachment color[MRT::MAX_SUPPORTED_RENDER_TARGET_COUNT] = {};
|
||||
math::uint2 attachmentSize = {};
|
||||
std::vector<wgpu::RenderPassColorAttachment> colorAttachments{};
|
||||
};
|
||||
MRT mColorAttachments{};
|
||||
Attachment mDepthAttachment{};
|
||||
Attachment mStencilAttachment{};
|
||||
|
||||
// Cached descriptors for the render pass
|
||||
std::vector<wgpu::RenderPassColorAttachment> mColorAttachmentDescriptors;
|
||||
wgpu::RenderPassDepthStencilAttachment mDepthStencilAttachmentDescriptor{};
|
||||
bool mHasDepthStencilAttachment = false;
|
||||
};
|
||||
}// namespace filament::backend
|
||||
#endif// TNT_FILAMENT_BACKEND_WEBGPUHANDLES_H
|
||||
|
||||
Reference in New Issue
Block a user