Skip to content

Commit d4efab5

Browse files
committed
Fix layered texture uploads.
1 parent 2e59603 commit d4efab5

1 file changed

Lines changed: 44 additions & 13 deletions

File tree

Sources/ObjectivelyGPU/Texture.c

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,50 @@ static void uploadPixels(Texture *self, const void *pixels, Uint32 size, Uint32
7676
CommandBuffer *commands = $(self->device, acquireCommandBuffer);
7777
CopyPass *copyPass = $(commands, beginCopyPass);
7878

79-
$(copyPass, uploadTexture,
80-
&(SDL_GPUTextureTransferInfo) {
81-
.transfer_buffer = tbuf,
82-
.pixels_per_row = pixelsPerRow,
83-
.rows_per_layer = (Uint32) self->size.h,
84-
},
85-
&(SDL_GPUTextureRegion) {
86-
.texture = self->texture,
87-
.w = (Uint32) self->size.w,
88-
.h = (Uint32) self->size.h,
89-
.d = self->layerCountOrDepth,
90-
},
91-
false);
79+
if (self->type == SDL_GPU_TEXTURETYPE_3D) {
80+
81+
// A 3D texture's slices are addressed by depth (region.z / .d), not by layer,
82+
// so upload the whole volume in one region.
83+
$(copyPass, uploadTexture,
84+
&(SDL_GPUTextureTransferInfo) {
85+
.transfer_buffer = tbuf,
86+
.pixels_per_row = pixelsPerRow,
87+
.rows_per_layer = (Uint32) self->size.h,
88+
},
89+
&(SDL_GPUTextureRegion) {
90+
.texture = self->texture,
91+
.w = (Uint32) self->size.w,
92+
.h = (Uint32) self->size.h,
93+
.d = self->layerCountOrDepth,
94+
},
95+
false);
96+
} else {
97+
98+
// Everything else (2D, 2D array, cube) is addressed by layer: upload one layer
99+
// per call from its layer-major slice of the transfer buffer. A single region
100+
// with d = layerCount only populates layer 0, so we must loop; a plain 2D
101+
// texture is simply one iteration.
102+
const Uint32 layers = self->layerCountOrDepth ? self->layerCountOrDepth : 1;
103+
const Uint32 layerBytes = size / layers;
104+
105+
for (Uint32 layer = 0; layer < layers; layer++) {
106+
$(copyPass, uploadTexture,
107+
&(SDL_GPUTextureTransferInfo) {
108+
.transfer_buffer = tbuf,
109+
.offset = layer * layerBytes,
110+
.pixels_per_row = pixelsPerRow,
111+
.rows_per_layer = (Uint32) self->size.h,
112+
},
113+
&(SDL_GPUTextureRegion) {
114+
.texture = self->texture,
115+
.layer = layer,
116+
.w = (Uint32) self->size.w,
117+
.h = (Uint32) self->size.h,
118+
.d = 1,
119+
},
120+
false);
121+
}
122+
}
92123

93124
release(copyPass);
94125
$(commands, submit);

0 commit comments

Comments
 (0)