Skip to content

Commit d7da968

Browse files
wip
1 parent f8df173 commit d7da968

File tree

1 file changed

+78
-14
lines changed

1 file changed

+78
-14
lines changed

examples/example_gfx_2.c

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,59 @@ Index of this file:
4747
// [SECTION] structs
4848
//-----------------------------------------------------------------------------
4949

50+
typedef int DXGI_FORMAT;
51+
typedef int D3D10_RESOURCE_DIMENSION;
52+
53+
typedef struct _plDssPixelFormat
54+
{
55+
uint32_t uSize;
56+
uint32_t uFlags;
57+
uint32_t uFourCC;
58+
uint32_t uRGBBitCount;
59+
uint32_t uRBitMask;
60+
uint32_t uGBitMask;
61+
uint32_t uBBitMask;
62+
uint32_t uABitMask;
63+
} plDssPixelFormat;
64+
65+
typedef struct _plDdsInternalHeader
66+
{
67+
uint32_t uSize;
68+
uint32_t uFlags;
69+
uint32_t uHeight;
70+
uint32_t uWidth;
71+
uint32_t uPitchOrLinearSize;
72+
uint32_t uDepth;
73+
uint32_t uMipMapCount;
74+
uint32_t auReserved1[11];
75+
plDssPixelFormat ddspf;
76+
uint32_t uCaps;
77+
uint32_t uCaps2;
78+
uint32_t uCaps3;
79+
uint32_t uCaps4;
80+
uint32_t uReserved2;
81+
} plDdsInternalHeader;
82+
83+
typedef struct _plDdsHeaderDxt10
84+
{
85+
DXGI_FORMAT tDxgiFormat;
86+
D3D10_RESOURCE_DIMENSION tResourceDimension;
87+
uint32_t uMiscFlag;
88+
uint32_t uArraySize;
89+
uint32_t uMiscFlags2;
90+
} plDdsHeaderDxt10;
91+
92+
typedef struct _plDdsHeader
93+
{
94+
uint32_t uMagic;
95+
plDdsInternalHeader tHeader;
96+
plDdsHeaderDxt10 tHeader10;
97+
} plDdsHeader;
98+
99+
//-----------------------------------------------------------------------------
100+
// [SECTION] public api
101+
//-----------------------------------------------------------------------------
102+
50103
typedef struct _plAppData
51104
{
52105
// window
@@ -254,7 +307,7 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
254307
// create vertex buffer
255308
const plBufferDesc tStagingBufferDesc = {
256309
.tUsage = PL_BUFFER_USAGE_STAGING,
257-
.szByteSize = 1280000,
310+
.szByteSize = 1280000 * 2,
258311
.pcDebugName = "staging buffer"
259312
};
260313
ptAppData->tStagingBuffer = gptGfx->create_buffer(ptDevice, &tStagingBufferDesc, NULL);
@@ -284,24 +337,26 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
284337
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~textures~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
285338

286339
// load image file from disk
287-
size_t szImageFileSize = gptVfs->get_file_size_str("/assets/textures/SpriteMapExample.png");
288-
plVfsFileHandle tSpriteSheet = gptVfs->open_file("/assets/textures/SpriteMapExample.png", PL_VFS_FILE_MODE_READ);
340+
size_t szImageFileSize = gptVfs->get_file_size_str("/assets/textures/sprite.dds");
341+
plVfsFileHandle tSpriteSheet = gptVfs->open_file("/assets/textures/sprite.dds", PL_VFS_FILE_MODE_READ);
289342
gptVfs->read_file(tSpriteSheet, NULL, &szImageFileSize);
290343
unsigned char* pucBuffer = malloc(szImageFileSize);
291344
gptVfs->read_file(tSpriteSheet, pucBuffer, &szImageFileSize);
292345
gptVfs->close_file(tSpriteSheet);
293346

347+
plDdsHeader* ptHeader = (plDdsHeader*)pucBuffer;
348+
294349
// load actual data from file data
295-
int iImageWidth = 0;
296-
int iImageHeight = 0;
297-
int _unused;
298-
unsigned char* pucImageData = gptImage->load(pucBuffer, (int)szImageFileSize, &iImageWidth, &iImageHeight, &_unused, 4);
299-
free(pucBuffer);
350+
// int iImageWidth = 0;
351+
// int iImageHeight = 0;
352+
// int _unused;
353+
// unsigned char* pucImageData = gptImage->load(pucBuffer, (int)szImageFileSize, &iImageWidth, &iImageHeight, &_unused, 4);
354+
// free(pucBuffer);
300355

301356
// create texture
302357
const plTextureDesc tTextureDesc = {
303-
.tDimensions = { (float)iImageWidth, (float)iImageHeight, 1},
304-
.tFormat = PL_FORMAT_R8G8B8A8_UNORM,
358+
.tDimensions = { (float)ptHeader->tHeader.uWidth, (float)ptHeader->tHeader.uHeight, 1},
359+
.tFormat = PL_FORMAT_BC2_UNORM,
305360
.uLayers = 1,
306361
.uMips = 1,
307362
.tType = PL_TEXTURE_TYPE_2D,
@@ -327,11 +382,20 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
327382
gptGfx->set_texture_usage(ptEncoder, ptAppData->tTexture, PL_TEXTURE_USAGE_SAMPLED, 0);
328383

329384
// copy memory to mapped staging buffer
330-
memcpy(&ptStagingBuffer->tMemoryAllocation.pHostMapped[2048], pucImageData, iImageWidth * iImageHeight * 4);
385+
// memcpy(&ptStagingBuffer->tMemoryAllocation.pHostMapped[2048], &pucBuffer[sizeof(plDdsHeader)], ptHeader->tHeader.uWidth * ptHeader->tHeader.uHeight * 4);
386+
387+
size_t uPitch = pl_max( 1, ((ptHeader->tHeader.uWidth+3)/4) ) * 4;
388+
// size_t uPitch = (ptHeader->tHeader.uWidth + 4 - 1) / 4;
389+
// uPitch = uPitch * 128 / 8;
390+
391+
for(uint32_t i = 0; i < ptHeader->tHeader.uHeight; i++)
392+
{
393+
memcpy(&ptStagingBuffer->tMemoryAllocation.pHostMapped[2048 + i * uPitch], &pucBuffer[sizeof(plDdsHeader) + i * uPitch], uPitch);
394+
}
331395

332396
const plBufferImageCopy tBufferImageCopy = {
333-
.uImageWidth = (uint32_t)iImageWidth,
334-
.uImageHeight = (uint32_t)iImageHeight,
397+
.uImageWidth = ptHeader->tHeader.uWidth,
398+
.uImageHeight = ptHeader->tHeader.uHeight,
335399
.uImageDepth = 1,
336400
.uLayerCount = 1,
337401
.szBufferOffset = 2048
@@ -342,7 +406,7 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
342406
gptStarter->return_blit_encoder(ptEncoder);
343407

344408
// free image data
345-
gptImage->free(pucImageData);
409+
// gptImage->free(pucImageData);
346410

347411
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~samplers~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
348412

0 commit comments

Comments
 (0)