28void patchImage(std::vector<u8>& image,
u32 imageWidth,
u32 imageHeight,
u32 bpp,
29 i32 x,
i32 y,
u32 width,
u32 height,
const u8* pixels,
bool flipY) {
30 if (x < 0 || y < 0 ||
static_cast<u32>(x) + width > imageWidth
31 ||
static_cast<u32>(y) + height > imageHeight) {
34 const usize rowBytes =
static_cast<usize>(width) * bpp;
35 for (
u32 row = 0; row < height; ++row) {
36 const u32 source = flipY ? height - 1 - row : row;
37 std::memcpy(&image[(
static_cast<usize>(y) + row) * imageWidth * bpp +
static_cast<usize>(x) * bpp],
38 pixels +
static_cast<usize>(source) * rowBytes, rowBytes);
42std::vector<u8> uploadOrderImage(
const TextureDesc& desc,
const void* pixels) {
44 std::vector<u8> image(
static_cast<usize>(desc.width) * desc.height * bpp * std::max(1u, desc.layers), 0);
45 if (pixels && desc.layered()) {
46 std::memcpy(image.data(), pixels, image.size());
48 patchImage(image, desc.width, desc.height, bpp, 0, 0, desc.width, desc.height,
49 static_cast<const u8*
>(pixels), desc.flipY);
64 compute_job_open_ =
false;
67 device_info_.reason = reason;
68 device_info_.identity = identity_;
69 device_info_.message = std::move(message);
70 device_info_.context = std::move(context);
71 device_info_.frame = device_frame_;
75 for (
auto& [
id, record] : registry_.buffers.all()) { record.realized =
false; record.owed =
false; }
76 for (
auto& [
id, record] : registry_.textures.all()) { record.realized =
false; record.owed =
false; }
77 for (
auto& [
id, record] : registry_.programs.all()) record.realized =
false;
78 for (
auto& [
id, record] : registry_.computes.all()) record.realized =
false;
79 for (
auto& [
id, record] : registry_.framebuffers.all()) record.realized =
false;
80 for (
auto& [
id, record] : registry_.timerQueries.all()) record.realized =
false;
82 native_locations_.clear();
85 if (device_lost_handler_) device_lost_handler_(device_info_);
100 ++device_generation_;
107void GfxDevice::realizeRegistry() {
108 for (
auto& [
id, record] : registry_.
programs.all()) {
109 std::vector<GfxAttribBinding> bindings;
110 bindings.reserve(record.attribBindings.size());
111 for (
const auto& [index, name] : record.attribBindings) bindings.push_back({index, name.c_str()});
112 const GfxShaderSource source{record.language, record.vertex.c_str(), record.fragment.c_str()};
116 static_cast<u32>(bindings.size()), &log, &stage);
117 if (record.realized) {
118 realizeProgramState(
id, record);
123 ES_LOG_ERROR(
"Device recovery: program {} did not rebuild: {}",
id, log);
127 for (
auto& [
id, record] : registry_.computes.all()) {
131 if (!record.realized)
ES_LOG_ERROR(
"Device recovery: compute program {} did not rebuild: {}",
id, log);
134 for (
auto& [
id, record] : registry_.buffers.all()) {
135 const bool hasBytes = !record.bytes.empty();
136 record.realized =
backendCreateBuffer(
id, record.desc, hasBytes ? record.bytes.data() :
nullptr);
138 if (record.owed) ++owed_count_;
142 for (
auto& [
id, record] : registry_.textures.all()) {
143 const bool hasBytes = !record.bytes.empty();
144 if (record.compressed && hasBytes) {
146 id, record.desc, record.compressedFormat, record.bytes.data(),
147 static_cast<u32>(record.bytes.size()), record.mipLevels);
149 TextureDesc desc = record.desc;
155 if (record.owed) ++owed_count_;
157 if (!record.realized)
ES_LOG_ERROR(
"Device recovery: texture {} did not rebuild",
id);
160 for (
auto& [
id, record] : registry_.framebuffers.all()) {
163 for (
auto& [
id, record] : registry_.timerQueries.all()) {
169 const bool anyValue = std::any_of(record.uniformValues.begin(), record.uniformValues.end(),
170 [](
const GfxUniformValue& v) { return v.type != GfxUniformValue::Type::None; });
173 for (
usize i = 0; i < record.uniformValues.size(); ++i) {
175 const i32 native = nativeUniformLocation(record,
id,
static_cast<i32>(i));
179 for (
usize i = 0; i < record.blockNames.size(); ++i) {
188void GfxDevice::settleRecovery() {
191 device_info_ = GfxDeviceLostInfo{};
197 if (!record.owed)
return;
207 if (
auto* record = registry_.buffers.find(owed.
id); record && record->owed) {
208 record->owed =
false;
211 }
else if (
auto* record = registry_.textures.find(owed.
id); record && record->owed) {
212 record->owed =
false;
220 if (
auto* record = registry_.textures.find(owed.
id); record && record->owed) {
221 payTexture(owed.
id, *record);
232void GfxDevice::keepBytes(std::vector<u8>& slot, std::vector<u8> bytes) {
233 retained_bytes_ -= slot.size();
234 retained_bytes_ += bytes.size();
235 const usize grownBy = bytes.size() > slot.size() ? bytes.size() - slot.size() : 0;
236 slot = std::move(bytes);
237 judgeRetainedBudget(grownBy);
240void GfxDevice::dropBytes(std::vector<u8>& slot) {
241 retained_bytes_ -= slot.size();
243 judgeRetainedBudget(0);
246std::vector<u8> GfxDevice::takeBytes(std::vector<u8>& slot) {
247 retained_bytes_ -= slot.size();
248 judgeRetainedBudget(0);
249 return std::exchange(slot, {});
252void GfxDevice::judgeRetainedBudget(
usize grownBy) {
253 if (retained_budget_ == 0 || retained_bytes_ <= retained_budget_) {
254 over_retained_budget_ =
false;
257 if (over_retained_budget_ || grownBy == 0)
return;
258 over_retained_budget_ =
true;
259 constexpr f64 MiB = 1024.0 * 1024.0;
260 ES_LOG_WARN(
"The graphics device keeps {:.2f} MiB of CPU copies to restore content after a GPU loss, "
261 "past its {:.2f} MiB budget (the last copy was {} bytes). Content that can be loaded "
262 "or drawn again should say who refills it instead of being retained.",
263 static_cast<f64>(retained_bytes_) / MiB,
static_cast<f64>(retained_budget_) / MiB, grownBy);
266void GfxDevice::eraseBuffer(
u32 id) {
267 if (
auto* record = registry_.buffers.find(
id)) dropBytes(record->bytes);
268 registry_.buffers.erase(
id);
271void GfxDevice::eraseTexture(
u32 id) {
272 if (
auto* record = registry_.textures.find(
id)) dropBytes(record->bytes);
273 registry_.textures.erase(
id);
285 std::vector<u8> bytes(desc.
size, 0);
286 if (initialData && desc.
size > 0) std::memcpy(bytes.data(), initialData, desc.
size);
287 keepBytes(record.
bytes, std::move(bytes));
289 const u32 id = registry_.buffers.insert(std::move(record));
295 registry_.buffers.find(
id)->realized =
true;
300 const u32 id =
static_cast<u32>(buffer);
301 auto* record = registry_.buffers.find(
id);
304 if (record->owed) --owed_count_;
310 const u32 id =
static_cast<u32>(buffer);
311 auto* record = registry_.buffers.find(
id);
312 if (!record || !data || sizeBytes == 0)
return;
313 countGeometryWrite(record->desc);
314 const bool whole = offsetBytes == 0 && sizeBytes >= record->desc.size;
315 if (!record->bytes.empty() &&
static_cast<usize>(offsetBytes) + sizeBytes <= record->bytes.size()) {
316 std::memcpy(record->bytes.data() + offsetBytes, data, sizeBytes);
317 }
else if (!record->realized && whole) {
318 const auto* first =
static_cast<const u8*
>(data);
319 keepBytes(record->bytes, std::vector<u8>(first, first + record->desc.size));
322 if (record->owed && whole) {
323 record->owed =
false;
330 const u32 id =
static_cast<u32>(buffer);
331 auto* record = registry_.buffers.find(
id);
333 countGeometryWrite(record->desc);
334 record->desc.size = sizeBytes;
336 std::vector<u8> bytes(sizeBytes, 0);
337 if (data && sizeBytes > 0) std::memcpy(bytes.data(), data, sizeBytes);
338 keepBytes(record->bytes, std::move(bytes));
340 dropBytes(record->bytes);
343 if (record->owed && data) {
344 record->owed =
false;
351 const auto* record = registry_.buffers.find(
static_cast<u32>(buffer));
352 return record ? &record->desc :
nullptr;
367 const u32 id =
static_cast<u32>(layout);
368 if (!registry_.layouts.find(
id))
return;
370 registry_.layouts.erase(
id);
374 const auto* record = registry_.layouts.find(
static_cast<u32>(layout));
375 return record ? &record->desc :
nullptr;
384 const auto* record = registry_.pipelines.find(
static_cast<u32>(handle));
385 return record ? &record->desc :
nullptr;
390 const auto* record = registry_.pipelines.find(
static_cast<u32>(handle));
392 current_pipeline_ = handle;
393 current_program_ = record->desc.
program;
410 ES_LOG_ERROR(
"GfxDevice::createTexture: a texture array or volume is sampled colour only");
416 ES_LOG_ERROR(
"GfxDevice::createTexture: an attachment-only texture cannot keep its contents");
418 keepBytes(record.
bytes, uploadOrderImage(desc, pixels));
421 const u32 id = registry_.textures.insert(std::move(record));
427 registry_.textures.find(
id)->realized =
true;
433 u32 byteLength,
u32 mipLevels) {
440 record.
mipLevels = mipLevels ? mipLevels : 1;
442 const auto* first =
static_cast<const u8*
>(data);
443 keepBytes(record.
bytes, std::vector<u8>(first, first + byteLength));
445 const u32 id = registry_.textures.insert(std::move(record));
451 registry_.textures.find(
id)->realized =
true;
456 const void* data,
u32 byteLength,
u32 mipLevels) {
458 const u32 id =
static_cast<u32>(texture);
459 auto* record = registry_.textures.find(
id);
462 record->compressed =
true;
463 record->compressedFormat = format;
464 record->mipLevels = mipLevels ? mipLevels : 1;
466 const auto* first =
static_cast<const u8*
>(data);
467 keepBytes(record->bytes, std::vector<u8>(first, first + byteLength));
471 if (record->realized) payTexture(
id, *record);
472 return record->realized;
479 ES_LOG_ERROR(
"GfxDevice::importExternalTexture: the device never saw these pixels to keep them");
483 registry_.textures.find(
id)->externallyWritable =
true;
489 registry_.textures.find(
id)->realized =
true;
495 const u32 id =
static_cast<u32>(texture);
496 auto* record = registry_.textures.find(
id);
499 record->externallyWritable =
true;
502 if (record->realized) payTexture(
id, *record);
503 return record->realized;
508 const u32 intoId =
static_cast<u32>(into);
509 const u32 fromId =
static_cast<u32>(from);
510 auto* target = registry_.textures.find(intoId);
511 auto* source = registry_.textures.find(fromId);
512 if (!target || !source || intoId == fromId || !source->realized)
return false;
514 ES_LOG_ERROR(
"GfxDevice::adoptTextureContent: texture {} keeps its bytes and {} has none to give",
521 target->desc = source->desc;
522 target->externallyWritable |= source->externallyWritable;
523 target->compressed = source->compressed;
524 target->compressedFormat = source->compressedFormat;
525 target->mipLevels = source->mipLevels;
526 target->realized =
true;
528 std::vector<u8> sourceBytes = takeBytes(source->bytes);
530 keepBytes(target->bytes, std::move(sourceBytes));
532 dropBytes(target->bytes);
534 if (source->owed) --owed_count_;
535 const bool owed = target->owed;
536 eraseTexture(fromId);
538 target->owed =
false;
547 const u32 id =
static_cast<u32>(texture);
548 auto* record = registry_.textures.find(
id);
551 if (record->owed) --owed_count_;
557 const void* pixels,
bool flipY) {
558 const u32 id =
static_cast<u32>(texture);
559 auto* record = registry_.textures.find(
id);
560 if (!record || !pixels || record->compressed)
return;
561 if (record->desc.layered()) {
562 ES_LOG_ERROR(
"GfxDevice::updateTexture: texture {} is an array or volume, whose slices are fixed at creation",
id);
566 const bool whole = x == 0 && y == 0 && width >= desc.
width && height >= desc.
height;
567 if (!record->bytes.empty()) {
569 x, y, width, height,
static_cast<const u8*
>(pixels), flipY);
570 }
else if (!record->realized && whole) {
572 upload.
flipY = flipY;
573 keepBytes(record->bytes, uploadOrderImage(upload, pixels));
578 const bool rebuildChain = record->realized && desc.
mipmaps && !record->owed;
579 if (whole) payTexture(
id, *record);
585 const u32 id =
static_cast<u32>(texture);
586 auto* record = registry_.textures.find(
id);
588 record->desc.minFilter = min;
589 record->desc.magFilter = mag;
590 record->desc.wrapS = wrapS;
591 record->desc.wrapT = wrapT;
597 const u32 id =
static_cast<u32>(texture);
598 auto* record = registry_.textures.find(
id);
600 record->desc.mipmaps =
true;
606 const auto* record = registry_.textures.find(
static_cast<u32>(texture));
607 return record ? record->revision : 0;
611 const auto* record = registry_.textures.find(
static_cast<u32>(texture));
612 return record && record->realized && !record->desc.storage && !record->externallyWritable ? record->revision : 0;
616 const auto* record = registry_.textures.find(
static_cast<u32>(texture));
617 return record ? &record->desc :
nullptr;
628 if (outLog) *outLog =
"device dead";
635 for (
u32 i = 0; i < bindingCount; ++i) {
636 record.
attribBindings.emplace_back(bindings[i].index, bindings[i].name ? bindings[i].name :
"");
638 const u32 id = registry_.programs.insert(std::move(record));
641 registry_.programs.erase(
id);
644 registry_.programs.find(
id)->realized =
true;
649 const u32 id =
static_cast<u32>(program);
650 auto* record = registry_.programs.find(
id);
651 if (!record || !callback)
return;
653 record->onLinked = std::move(callback);
660 auto* record = registry_.programs.find(
id);
661 if (!record || !record->onLinked)
return;
662 auto callback = std::move(record->onLinked);
663 record->onLinked =
nullptr;
667 current_program_ = bound;
672 const u32 id =
static_cast<u32>(program);
673 auto* record = registry_.programs.find(
id);
676 registry_.programs.erase(
id);
677 native_locations_.erase(
id);
683 if (outLog) *outLog =
"device dead";
687 if (outLog) *outLog =
"this device has no compute stage";
691 if (outLog) *outLog =
"compute is WGSL only";
697 const u32 id = registry_.computes.insert(std::move(record));
700 registry_.computes.erase(
id);
703 registry_.computes.find(
id)->realized =
true;
708 const u32 id =
static_cast<u32>(program);
709 auto* record = registry_.computes.find(
id);
712 registry_.computes.erase(
id);
717 async_compute_enabled_ = enabled;
725 compute_job_open_ =
true;
726 compute_priority_ = priority;
732 compute_job_open_ =
false;
733 return pending_compute_fence_;
749 const u32 id =
static_cast<u32>(program);
750 const auto* record = registry_.computes.find(
id);
751 if (!record || !record->realized)
return false;
752 if (groupsX == 0 || groupsY == 0 || groupsZ == 0)
return false;
753 if (bindingCount > 0 && !bindings)
return false;
756 next_compute_fence_ < INT32_MAX &&
758 pending_compute_fence_ = ++next_compute_fence_;
765 const u32 id =
static_cast<u32>(program);
766 current_program_ = program;
768 const auto* record = registry_.programs.find(
id);
771 }
else if (record->realized) {
777 auto& natives = native_locations_[id];
779 i32& native = natives[
static_cast<usize>(location)];
785 const u32 id =
static_cast<u32>(program);
786 auto* record = registry_.programs.find(
id);
787 if (!record || !name)
return -1;
799 const u32 id =
static_cast<u32>(program);
800 const auto* record = registry_.programs.find(
id);
805 const u32 id =
static_cast<u32>(current_program_);
806 auto* record = registry_.
programs.find(
id);
807 if (!record || location < 0 ||
static_cast<usize>(location) >= record->
uniformValues.size())
return;
811 const i32 native = nativeUniformLocation(*record,
id, location);
819 setUniformValue(location, v);
826 setUniformValue(location, v);
834 setUniformValue(location, v);
843 setUniformValue(location, v);
853 setUniformValue(location, v);
860 std::memcpy(v.
f, data, 9 *
sizeof(
f32));
861 setUniformValue(location, v);
868 std::memcpy(v.
f, data, 16 *
sizeof(
f32));
869 setUniformValue(location, v);
873 const u32 id =
static_cast<u32>(program);
874 auto* record = registry_.programs.find(
id);
875 if (!record || !record->
realized)
return {};
878 const i32 native = info.location;
880 if (info.location >= 0) {
881 auto& natives = native_locations_[id];
883 natives[
static_cast<usize>(info.location)] = native;
890 const u32 id =
static_cast<u32>(program);
891 auto* record = registry_.programs.find(
id);
904 const u32 id =
static_cast<u32>(program);
905 auto* record = registry_.programs.find(
id);
906 if (!record || blockIndex >= record->
blockNames.size())
return;
921 if (
auto* texture = registry_.textures.find(
static_cast<u32>(attachment))) texture->externallyWritable =
true;
926 registry_.framebuffers.erase(
id);
929 registry_.framebuffers.find(
id)->realized =
true;
934 const u32 id =
static_cast<u32>(framebuffer);
935 auto* record = registry_.framebuffers.find(
id);
938 registry_.framebuffers.erase(
id);
942 const auto* record = registry_.framebuffers.find(
static_cast<u32>(framebuffer));
943 return record ? &record->desc :
nullptr;
950 registry_.timerQueries.erase(
id);
953 registry_.timerQueries.find(
id)->realized =
true;
960 for (
const auto& [
id, buffer] : registry_.buffers.all()) {
961 if (buffer.realized) bytes += buffer.desc.size;
964 for (
const auto& [
id, texture] : registry_.textures.all()) {
965 if (!texture.realized)
continue;
966 u32 width = texture.desc.width, height = texture.desc.height;
967 if (!width || !height)
continue;
969 const u32 levels = texture.compressed ? texture.mipLevels
970 : texture.desc.mipmaps && texture.desc.samples <= 1 && !isDepthFormat(texture.desc.format) ? 32 : 1;
971 for (
u32 level = 0; level < levels; ++level) {
972 if (texture.compressed) {
973 bytes += ((
static_cast<u64>(width) + block.blockWidth - 1) / block.blockWidth)
974 * ((
static_cast<u64>(height) + block.blockHeight - 1) / block.blockHeight) * block.bytesPerBlock;
977 * std::max(1u, texture.desc.samples) * std::max(1u, texture.desc.layers);
979 if (width == 1 && height == 1)
break;
980 width = std::max(1u, width / 2); height = std::max(1u, height / 2);
988 registry_.buffers.size(),
989 registry_.textures.size(),
990 registry_.programs.size(),
991 registry_.layouts.size(),
992 registry_.pipelines.size(),
993 registry_.framebuffers.size(),
Abstract graphics device interface.
Logging utilities for ESEngine.
#define ES_LOG_WARN(...)
Logs a warning message.
#define ES_LOG_ERROR(...)
Logs an error message.
A content policy. provider names who refills a Sourced object and key what it refills; both are the p...
constexpr GfxContentKind kind() const
virtual bool backendAcceptsVertexLayout(const VertexLayoutDesc &desc)
False for a layout this backend has no way to express.
bool markDeviceLost(GfxDeviceLostReason reason, std::string message={}, std::string context={})
The single transition into Lost. Backends call this on detection.
void deleteCompute(ComputeHandle program)
Deletes a compute program.
void deleteVertexLayout(VertexLayoutHandle layout)
Deletes a vertex layout (no pipeline may reference it afterwards).
void updateTexture(TextureHandle texture, i32 x, i32 y, u32 width, u32 height, const void *pixels, bool flipY)
Uploads pixels to a sub-rectangle of a texture (transfer format from its desc).
virtual bool backendCreateBuffer(u32 id, const BufferDesc &desc, const void *data)=0
virtual bool backendCreateTexture(u32 id, const TextureDesc &desc, const void *pixels)=0
virtual bool backendCreateCompute(u32 id, const GfxComputeSource &source, std::string *outLog)
void setUniform3f(i32 location, f32 x, f32 y, f32 z)
Sets a vec3 uniform.
virtual void backendSetAsyncComputeEnabled(bool)
virtual i32 backendUniformLocation(u32 program, const char *name)=0
The backend's own location this generation, -1 when absent.
void uniformBlockBinding(ShaderHandle program, u32 blockIndex, u32 bindingPoint)
Links a program's uniform block to an indexed binding slot.
virtual bool supportsAsyncCompute() const
Hardware with an independent queue. Individual bindings can still require the graphics queue.
virtual void backendUseProgram(u32 id)=0
BufferHandle createBuffer(const BufferDesc &desc, GfxContent content, const void *initialData)
Creates a buffer with fixed capacity and optional initial contents.
i32 getUniformLocation(ShaderHandle program, const char *name)
Gets a uniform location by name, -1 when the linked program has none.
virtual void backendUpdateTexture(u32 id, i32 x, i32 y, u32 width, u32 height, const void *pixels, bool flipY)=0
void setUniform2f(i32 location, f32 x, f32 y)
Sets a vec2 uniform.
virtual bool backendCreateFramebuffer(u32 id, const FramebufferDesc &desc)=0
void invalidatePipelineCache()
Forces the next setPipeline to re-apply, dropping the cached current pipeline.
virtual u64 estimatedGpuBytes() const
void forgoContent(const GfxOwedContent &owed)
Gives up on one owed object's contents: it keeps its placeholder.
void setUniform4f(i32 location, f32 x, f32 y, f32 z, f32 w)
Sets a vec4 uniform.
void useProgram(ShaderHandle program)
Binds a program directly, for setup-time uniform seeding.
void generateMipmaps(TextureHandle texture)
Generates mipmaps for a texture.
virtual bool backendCreateTimerQuery(u32 id)=0
FramebufferHandle createFramebuffer(const FramebufferDesc &desc)
Creates a framebuffer from its attachments and validates completeness.
const VertexLayoutDesc * vertexLayoutDesc(VertexLayoutHandle layout) const
A layout's description, or null for a handle that names none.
u32 createTimerQuery()
Creates a GPU elapsed-time query, or 0 when the backend cannot time GPU work.
virtual bool recreateDevice()
Backend half of recoverDevice: a working device again, with every id-to-native map and derived cache ...
virtual void backendUpdateBuffer(u32 id, u32 offsetBytes, const void *data, u32 sizeBytes)=0
virtual void captureDeviceIdentity()
Records who this backend is, so a later loss report can name it.
TextureHandle importExternalTexture(u32 nativeId, const TextureDesc &desc, GfxContent content)
Adopts a texture a host created with the device's own API (a JS-side WebGL upload).
virtual bool backendCreateCompressedTexture(u32 id, const TextureDesc &desc, GfxCompressedFormat format, const void *data, u32 byteLength, u32 mipLevels)=0
AsyncComputeFence endAsyncComputeJob()
A fence for submitted work, or -1 when it is already ordered on the graphics queue.
virtual void backendUniformBlockBinding(u32 program, u32 nativeBlockIndex, u32 bindingPoint)=0
virtual void backendWaitAsyncCompute()
virtual void backendResizeBuffer(u32 id, const BufferDesc &desc, const void *data)=0
i32 getAttribLocation(ShaderHandle program, const char *name)
Gets a vertex attribute location by name (-1 if not found).
virtual void backendDeleteProgram(u32 id)=0
virtual void backendInvalidatePipelineCache()=0
virtual u32 backendReadbackCount() const =0
Readbacks in flight, the one live object the registry does not hold.
void notifyProgramLinked(u32 id)
For a backend finishing a deferred program: runs its onLinked callback, leaving the bound program as ...
virtual void backendMoveTexture(u32 into, u32 from)=0
Re-keys from's native texture under into; into holds none on entry.
virtual i32 backendAttribLocation(u32 program, const char *name)=0
std::vector< GfxUniformInfo > getActiveUniforms(ShaderHandle program)
Enumerates all active uniforms of a linked shader program.
u64 trackedTextureRevision(TextureHandle texture) const
Zero for content that can change without an upload, including render targets and host textures.
virtual bool backendProgramPending(u32 id) const
True while a program created with deferLink has not been waited for.
bool restoreCompressedTexture(TextureHandle texture, GfxCompressedFormat format, const void *data, u32 byteLength, u32 mipLevels)
Puts compressed contents back behind an owed texture's handle.
void restoreContent(const GfxOwedContent &owed)
Settles one owed object whose provider wrote its contents itself.
void deleteTexture(TextureHandle texture)
Deletes a texture.
virtual bool backendAdoptTexture(u32 id, u32 nativeId, const TextureDesc &desc)=0
Takes over a native texture the host created; false when this backend cannot.
virtual void backendGenerateMipmaps(u32 id)=0
void setAsyncComputeEnabled(bool enabled)
virtual bool backendComputeScopeAvailable() const
void setUniformMat3(i32 location, const f32 *data)
Sets a mat3 uniform.
bool isDeviceUsable() const
Whether work may be submitted — the one check the guards make.
virtual bool backendCreateProgram(u32 id, const GfxShaderSource &source, const GfxAttribBinding *bindings, u32 bindingCount, std::string *outLog, GfxShaderStage *outFailedStage)=0
void setUniform1f(i32 location, f32 value)
Sets a float uniform.
TextureHandle createTexture(const TextureDesc &desc, GfxContent content, const void *pixels)
Creates a 2D texture; uploads pixels (tightly packed, desc-sized) when non-null.
void setPipeline(PipelineHandle handle)
Binds a pipeline: applies its program, blend, depth, stencil compare/op and culling.
ComputeHandle createCompute(const GfxComputeSource &source, std::string *outLog=nullptr)
Compiles a compute program.
void graphicsWaitOnAsyncComputeJob(AsyncComputeFence fence)
virtual void backendSetUniform(i32 nativeLocation, const GfxUniformValue &value)=0
void setUniformMat4(i32 location, const f32 *data)
Sets a mat4 uniform.
u32 getUniformBlockIndex(ShaderHandle program, const char *name)
Returns a program's uniform-block index by name, or GFX_INVALID_UNIFORM_BLOCK if absent.
void deleteBuffer(BufferHandle buffer)
Deletes a buffer.
VertexLayoutHandle createVertexLayout(const VertexLayoutDesc &desc)
Registers an immutable vertex layout; pipelines reference it by handle.
virtual void backendSetTextureParams(u32 id, const TextureDesc &desc)=0
ShaderHandle createProgram(const GfxShaderSource &source, const GfxAttribBinding *bindings, u32 bindingCount, std::string *outLog, GfxShaderStage *outFailedStage)
Compiles and links a GPU program from GLSL sources.
virtual bool backendDispatchCompute(u32 id, const GfxComputeBinding *bindings, u32 bindingCount, u32 groupsX, u32 groupsY, u32 groupsZ)
virtual void onDeviceLost()
Release what only this backend can name, while the dead device is still the current one.
const FramebufferDesc * framebufferDesc(FramebufferHandle framebuffer) const
A framebuffer's description, or null for Default and unknown handles.
PipelineHandle createPipeline(const PipelineDesc &desc)
Resolves a pipeline description to a cached handle (creating it on first use).
const BufferDesc * bufferDesc(BufferHandle buffer) const
A buffer's description as it stands, or null for a handle that names none.
virtual void backendDeleteFramebuffer(u32 id)=0
void manualComputeBarrier()
Order dispatches across queues. Each inline dispatch is already a complete compute pass.
virtual void backendDeleteTexture(u32 id)=0
bool beginAsyncComputeJob(AsyncComputePriority priority=AsyncComputePriority::Default)
Opens a compute-only scope, including on single-queue devices. Scopes cannot nest.
virtual void backendSetPipeline(u32 id, const PipelineDesc &desc)=0
void setTextureParams(TextureHandle texture, TextureFilter min, TextureFilter mag, TextureWrap wrapS, TextureWrap wrapT)
Sets texture filtering and wrap parameters.
virtual bool supportsCompute() const
Whether this device runs compute shaders.
virtual void backendIntUniformSet(u32 program)
bool adoptTextureContent(TextureHandle into, TextureHandle from)
Moves from's storage and description behind into, and ends from.
bool restoreTextureFromNative(TextureHandle texture, u32 nativeId)
Replaces the native texture behind texture with one the host created again.
virtual bool backendDispatchAsyncCompute(AsyncComputePriority, u32, const GfxComputeBinding *, u32, u32, u32, u32)
u64 textureRevision(TextureHandle texture) const
A texture's description as it stands, or null for a handle that names none.
TextureHandle createCompressedTexture(const TextureDesc &desc, GfxContent content, GfxCompressedFormat format, const void *data, u32 byteLength, u32 mipLevels)
Creates a texture from pre-compressed GPU block data.
const PipelineDesc * pipelineDesc(PipelineHandle handle) const
A pipeline's description, or null for a handle that names none.
GfxLiveObjects liveObjects() const
GPU objects alive right now (see GfxLiveObjects), read off the registry.
void deleteFramebuffer(FramebufferHandle framebuffer)
Deletes a framebuffer (its attachment textures are owned by the caller).
virtual u32 backendUniformBlockIndex(u32 program, const char *name)=0
virtual void backendDeleteBuffer(u32 id)=0
bool recoverDevice()
Rebuilds the device and every object it issued, behind the same handles.
void updateBuffer(BufferHandle buffer, u32 offsetBytes, const void *data, u32 sizeBytes)
Updates a sub-range of a buffer; must fit within its capacity.
void resizeBuffer(BufferHandle buffer, u32 sizeBytes, const void *data)
Re-specifies a buffer's store with a new capacity, discarding prior contents.
const TextureDesc * textureDesc(TextureHandle texture) const
void setUniform1i(i32 location, i32 value)
Sets an integer uniform of the bound program.
virtual void backendDeleteCompute(u32 id)
void deleteProgram(ShaderHandle program)
Deletes a shader program.
bool dispatchCompute(ComputeHandle program, const GfxComputeBinding *bindings, u32 bindingCount, u32 groupsX, u32 groupsY=1, u32 groupsZ=1)
Runs program over groupsX x groupsY x groupsZ workgroups, with these resources bound.
virtual void backendDeleteVertexLayout(u32 id)=0
Drops what the backend derived from a layout (a GL VAO); the desc stays here.
virtual std::vector< GfxUniformInfo > backendActiveUniforms(u32 program)=0
void onProgramLinked(ShaderHandle program, std::function< void()> callback)
Runs callback when program has linked, once: now if it already has, at the first thing that needs it ...
GfxRecordTable< GfxProgramRecord > programs
FramebufferHandle
Handle 0 is the default framebuffer (backbuffer); it cannot be created or deleted.
constexpr AsyncComputeFence kNoAsyncComputeFence
static constexpr u32 GFX_INVALID_UNIFORM_BLOCK
Sentinel returned by getUniformBlockIndex when a program has no such block (GL_INVALID_INDEX).
std::int32_t i32
Signed 32-bit integer.
std::uint64_t u64
Unsigned 64-bit integer.
GfxDeviceLostReason
Why the device was lost. The report leads with this.
std::size_t usize
Size type for indexing and sizes.
ComputeHandle
A compute program (GfxDevice::createCompute); only a device that supportsCompute() issues one.
GfxShaderStage
Which pipeline stage rejected a program during createProgram().
PipelineHandle
Opaque handle to a cached pipeline; PipelineHandle{0} is the null/invalid handle.
GfxCompressedFormat
GPU-compressed texture internal formats for createCompressedTexture.
@ Dead
Unrecoverable — the renderer will not draw again this run.
@ Recovering
A replacement device is being built; old handles stay dead.
@ Lost
Every handle issued before is dead; submission has stopped.
std::uint32_t u32
Unsigned 32-bit integer.
std::uint8_t u8
Unsigned 8-bit integer.
u32 gfxBytesPerPixel(GfxPixelFormat fmt)
Bytes one pixel occupies in the CALLER's upload buffer — the single source both backends size a read ...
double f64
64-bit floating point
constexpr u32 GFX_UNBOUND_BLOCK
GfxBlockInfo gfxCompressedBlockInfo(GfxCompressedFormat fmt)
@ Retained
The device keeps the last bytes it was given and uploads them again.
@ Sourced
A provider outside the device refills it; until then it is owed.
u64 nextRevision()
A revision no content has had before. Never 0, which reads as "none".
float f32
32-bit floating point
Immutable creation parameters of a GPU buffer.
Attachments of an offscreen render target. The attach point of depthStencil follows its texture's pix...
TextureHandle resolveColor2
TextureHandle resolveColor0
TextureHandle resolveDepthStencil
TextureHandle resolveColor1
Its single-sample twin, when the target is multisampled.
TextureHandle depthStencil
Vertex attribute location binding applied before a program is linked.
std::vector< u8 > bytes
Retained content, or the initial bytes of a buffer created while the device was lost.
One resource a compute dispatch binds, at @group(0) @binding(binding).
A compute program's source. Compute is WGSL only: GLSL ES 3.00 has no compute stage.
GfxShaderLanguage language
const char * entry
The @compute entry point.
GPU objects the device has created and not yet destroyed.
A Sourced object that has storage again and is waiting for its contents.
std::vector< std::string > uniformNames
A location handed out is an index here; the backend's own is resolved per generation.
std::vector< GfxUniformValue > uniformValues
std::vector< std::string > blockNames
std::vector< std::pair< u32, std::string > > attribBindings
GfxShaderLanguage language
std::vector< u32 > blockBindings
Binding point per block name, or GFX_UNBOUND_BLOCK.
A program's source pair, tagged with the language it is written in.
GfxShaderLanguage language
GfxCompressedFormat compressedFormat
Immutable description of a draw pipeline. Equality keys the pipeline cache, so two draws with the sam...
Immutable creation parameters of a 2D texture or 2D texture array.
bool layered() const
An array or a volume: pixels given slice by slice, fixed at creation, never an attachment.
Describes how vertex-buffer bytes map to shader attributes.