Skip to content

Commit 0ca472e

Browse files
committed
feat: add auto const offset & id
1 parent 7ac4ac7 commit 0ca472e

File tree

3 files changed

+48
-36
lines changed

3 files changed

+48
-36
lines changed

extensions/pl_graphics_metal.m

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,10 +1228,14 @@
12281228
MTLFunctionConstantValues* ptConstantValues = [MTLFunctionConstantValues new];
12291229

12301230
const char* pcConstantData = ptDescription->pTempConstantData;
1231+
uint32_t uConstantOffset = 0;
12311232
for(uint32_t i = 0; i < ptShader->tDesc._uConstantCount; i++)
12321233
{
12331234
const plSpecializationConstant* ptConstant = &ptShader->tDesc.atConstants[i];
1234-
[ptConstantValues setConstantValue:&pcConstantData[ptConstant->uOffset] type:pl__metal_data_type(ptConstant->tType) atIndex:ptConstant->uID];
1235+
const uint32_t uConstantIndex = ptConstant->uID == 0 ? i : ptConstant->uID;
1236+
const uint32_t uAutoConstantOffset = ptConstant->uOffset == 0 ? uConstantOffset : ptConstant->uOffset;
1237+
[ptConstantValues setConstantValue:&pcConstantData[uAutoConstantOffset] type:pl__metal_data_type(ptConstant->tType) atIndex:uConstantIndex];
1238+
uConstantOffset += (uint32_t)pl__get_data_type_size(ptConstant->tType);
12351239
}
12361240

12371241
id<MTLFunction> computeFunction = [ptMetalShader->library newFunctionWithName:entryFunc constantValues:ptConstantValues error:&error];
@@ -1383,10 +1387,14 @@
13831387
MTLFunctionConstantValues* ptConstantValues = [MTLFunctionConstantValues new];
13841388

13851389
const char* pcConstantData = ptDescription->pTempConstantData;
1390+
uint32_t uConstantOffset = 0;
13861391
for(uint32_t i = 0; i < ptShader->tDesc._uConstantCount; i++)
13871392
{
13881393
const plSpecializationConstant* ptConstant = &ptShader->tDesc.atConstants[i];
1389-
[ptConstantValues setConstantValue:&pcConstantData[ptConstant->uOffset] type:pl__metal_data_type(ptConstant->tType) atIndex:ptConstant->uID];
1394+
const uint32_t uConstantIndex = ptConstant->uID == 0 ? i : ptConstant->uID;
1395+
const uint32_t uAutoConstantOffset = ptConstant->uOffset == 0 ? uConstantOffset : ptConstant->uOffset;
1396+
[ptConstantValues setConstantValue:&pcConstantData[uAutoConstantOffset] type:pl__metal_data_type(ptConstant->tType) atIndex:uConstantIndex];
1397+
uConstantOffset += (uint32_t)pl__get_data_type_size(ptConstant->tType);
13901398
}
13911399

13921400
id<MTLFunction> vertexFunction = [ptMetalShader->tVertexLibrary newFunctionWithName:vertexEntry constantValues:ptConstantValues error:&error];

extensions/pl_graphics_vulkan.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,13 +1369,15 @@ pl_create_compute_shader(plDevice* ptDevice, const plComputeShaderDesc* ptDescri
13691369
// setup & count specilization constants
13701370
ptShader->tDesc._uConstantCount = 0;
13711371
ptVulkanShader->szSpecializationSize = 0;
1372+
uint32_t uConstantOffset = 0;
13721373
for (uint32_t i = 0; i < PL_MAX_SHADER_SPECIALIZATION_CONSTANTS; i++)
13731374
{
13741375
const plSpecializationConstant* ptConstant = &ptShader->tDesc.atConstants[i];
13751376
if(ptConstant->tType == PL_DATA_TYPE_UNSPECIFIED)
13761377
break;
1377-
ptVulkanShader->atSpecializationEntries[i].constantID = ptConstant->uID;
1378-
ptVulkanShader->atSpecializationEntries[i].offset = ptConstant->uOffset;
1378+
ptVulkanShader->atSpecializationEntries[i].constantID = ptConstant->uID == 0 ? ptShader->tDesc._uConstantCount : ptConstant->uID;
1379+
ptVulkanShader->atSpecializationEntries[i].offset = ptConstant->uOffset == 0 ? uConstantOffset : ptConstant->uOffset;
1380+
uConstantOffset += (uint32_t)pl__get_data_type_size(ptConstant->tType);
13791381
ptVulkanShader->atSpecializationEntries[i].size = pl__get_data_type_size(ptConstant->tType);
13801382
ptVulkanShader->szSpecializationSize += ptVulkanShader->atSpecializationEntries[i].size;
13811383
ptShader->tDesc._uConstantCount++;
@@ -1574,13 +1576,15 @@ pl_create_shader(plDevice* ptDevice, const plShaderDesc* ptDescription)
15741576
// setup & count specilization constants
15751577
ptShader->tDesc._uConstantCount = 0;
15761578
ptVulkanShader->szSpecializationSize = 0;
1579+
uint32_t uConstantOffset = 0;
15771580
for (uint32_t i = 0; i < PL_MAX_SHADER_SPECIALIZATION_CONSTANTS; i++)
15781581
{
15791582
const plSpecializationConstant* ptConstant = &ptShader->tDesc.atConstants[i];
15801583
if(ptConstant->tType == PL_DATA_TYPE_UNSPECIFIED)
15811584
break;
1582-
ptVulkanShader->atSpecializationEntries[i].constantID = ptConstant->uID;
1583-
ptVulkanShader->atSpecializationEntries[i].offset = ptConstant->uOffset;
1585+
ptVulkanShader->atSpecializationEntries[i].constantID = ptConstant->uID == 0 ? ptShader->tDesc._uConstantCount : ptConstant->uID;
1586+
ptVulkanShader->atSpecializationEntries[i].offset = ptConstant->uOffset == 0 ? uConstantOffset : ptConstant->uOffset;
1587+
uConstantOffset += (uint32_t)pl__get_data_type_size(ptConstant->tType);
15841588
ptVulkanShader->atSpecializationEntries[i].size = pl__get_data_type_size(ptConstant->tType);
15851589
ptVulkanShader->szSpecializationSize += ptVulkanShader->atSpecializationEntries[i].size;
15861590
ptShader->tDesc._uConstantCount++;

shaders/shaders.pls

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@
4747
"pcName": "panorama_to_cubemap",
4848
"tShader": { "file": "panorama_to_cubemap.comp", "entry": "main" },
4949
"atConstants": [
50-
{ "uID": 0, "uOffset": 0, "tType": "PL_DATA_TYPE_INT" },
51-
{ "uID": 1, "uOffset": 4, "tType": "PL_DATA_TYPE_INT" },
52-
{ "uID": 2, "uOffset": 8, "tType": "PL_DATA_TYPE_INT" }
50+
{ "tType": "PL_DATA_TYPE_INT" },
51+
{ "tType": "PL_DATA_TYPE_INT" },
52+
{ "tType": "PL_DATA_TYPE_INT" }
5353
],
5454
"atBindGroupLayouts": [
5555
{
@@ -104,10 +104,10 @@
104104
"pcName": "skinning",
105105
"tShader": { "file": "skinning.comp", "entry": "main" },
106106
"atConstants": [
107-
{ "uID": 0, "uOffset": 0, "tType": "PL_DATA_TYPE_INT" },
108-
{ "uID": 1, "uOffset": 4, "tType": "PL_DATA_TYPE_INT" },
109-
{ "uID": 2, "uOffset": 8, "tType": "PL_DATA_TYPE_INT" },
110-
{ "uID": 3, "uOffset": 12, "tType": "PL_DATA_TYPE_INT" }
107+
{ "tType": "PL_DATA_TYPE_INT" },
108+
{ "tType": "PL_DATA_TYPE_INT" },
109+
{ "tType": "PL_DATA_TYPE_INT" },
110+
{ "tType": "PL_DATA_TYPE_INT" }
111111
],
112112
"atBindGroupLayouts": [
113113
{
@@ -236,11 +236,11 @@
236236
{ "bBlendEnabled": false }
237237
],
238238
"atConstants": [
239-
{ "uID": 0, "uOffset": 0, "tType": "PL_DATA_TYPE_INT" },
240-
{ "uID": 1, "uOffset": 4, "tType": "PL_DATA_TYPE_INT" },
241-
{ "uID": 2, "uOffset": 8, "tType": "PL_DATA_TYPE_INT" },
242-
{ "uID": 3, "uOffset": 12, "tType": "PL_DATA_TYPE_INT" },
243-
{ "uID": 4, "uOffset": 16, "tType": "PL_DATA_TYPE_INT" }
239+
{ "tType": "PL_DATA_TYPE_INT" },
240+
{ "tType": "PL_DATA_TYPE_INT" },
241+
{ "tType": "PL_DATA_TYPE_INT" },
242+
{ "tType": "PL_DATA_TYPE_INT" },
243+
{ "tType": "PL_DATA_TYPE_INT" }
244244
],
245245
"atBindGroupLayouts": [
246246
{ "pcName": "global" },
@@ -275,13 +275,13 @@
275275
}
276276
],
277277
"atConstants": [
278-
{ "uID": 0, "uOffset": 0, "tType": "PL_DATA_TYPE_INT" },
279-
{ "uID": 1, "uOffset": 4, "tType": "PL_DATA_TYPE_INT" },
280-
{ "uID": 2, "uOffset": 8, "tType": "PL_DATA_TYPE_INT" },
281-
{ "uID": 3, "uOffset": 12, "tType": "PL_DATA_TYPE_INT" },
282-
{ "uID": 4, "uOffset": 16, "tType": "PL_DATA_TYPE_INT" },
283-
{ "uID": 5, "uOffset": 20, "tType": "PL_DATA_TYPE_INT" },
284-
{ "uID": 6, "uOffset": 24, "tType": "PL_DATA_TYPE_INT" }
278+
{ "tType": "PL_DATA_TYPE_INT" },
279+
{ "tType": "PL_DATA_TYPE_INT" },
280+
{ "tType": "PL_DATA_TYPE_INT" },
281+
{ "tType": "PL_DATA_TYPE_INT" },
282+
{ "tType": "PL_DATA_TYPE_INT" },
283+
{ "tType": "PL_DATA_TYPE_INT" },
284+
{ "tType": "PL_DATA_TYPE_INT" }
285285
],
286286
"atBindGroupLayouts": [
287287
{ "pcName": "global" },
@@ -395,10 +395,10 @@
395395
}
396396
],
397397
"atConstants": [
398-
{ "uID": 0, "uOffset": 0, "tType": "PL_DATA_TYPE_INT" },
399-
{ "uID": 1, "uOffset": 4, "tType": "PL_DATA_TYPE_INT" },
400-
{ "uID": 2, "uOffset": 8, "tType": "PL_DATA_TYPE_INT" },
401-
{ "uID": 3, "uOffset": 12, "tType": "PL_DATA_TYPE_INT" }
398+
{ "tType": "PL_DATA_TYPE_INT" },
399+
{ "tType": "PL_DATA_TYPE_INT" },
400+
{ "tType": "PL_DATA_TYPE_INT" },
401+
{ "tType": "PL_DATA_TYPE_INT" }
402402
],
403403
"atBindGroupLayouts": [
404404
{ "pcName": "global" },
@@ -442,10 +442,10 @@
442442
}
443443
],
444444
"atConstants": [
445-
{ "uID": 0, "uOffset": 0, "tType": "PL_DATA_TYPE_INT" },
446-
{ "uID": 1, "uOffset": 4, "tType": "PL_DATA_TYPE_INT" },
447-
{ "uID": 2, "uOffset": 8, "tType": "PL_DATA_TYPE_INT" },
448-
{ "uID": 3, "uOffset": 12, "tType": "PL_DATA_TYPE_INT" }
445+
{ "tType": "PL_DATA_TYPE_INT" },
446+
{ "tType": "PL_DATA_TYPE_INT" },
447+
{ "tType": "PL_DATA_TYPE_INT" },
448+
{ "tType": "PL_DATA_TYPE_INT" }
449449
],
450450
"atBindGroupLayouts": [
451451
{ "pcName": "global" },
@@ -483,9 +483,9 @@
483483
{ "bBlendEnabled": false }
484484
],
485485
"atConstants": [
486-
{ "uID": 0, "uOffset": 0, "tType": "PL_DATA_TYPE_INT" },
487-
{ "uID": 1, "uOffset": 4, "tType": "PL_DATA_TYPE_INT" },
488-
{ "uID": 2, "uOffset": 8, "tType": "PL_DATA_TYPE_INT" }
486+
{ "tType": "PL_DATA_TYPE_INT" },
487+
{ "tType": "PL_DATA_TYPE_INT" },
488+
{ "tType": "PL_DATA_TYPE_INT" }
489489
],
490490
"atBindGroupLayouts": [
491491
{ "pcName": "global" },

0 commit comments

Comments
 (0)