Skip to content

Commit 7c906a3

Browse files
committed
mantle: add support for VK_EXT_extended_dynamic_state3
1 parent fecef95 commit 7c906a3

File tree

5 files changed

+106
-35
lines changed

5 files changed

+106
-35
lines changed

src/mantle/mantle_cmd_buf.c

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,11 @@ GR_VOID GR_STDCALL grCmdBindStateObject(
512512
VKD.vkCmdSetFrontFaceEXT(grCmdBuffer->commandBuffer, rasterState->frontFace);
513513
VKD.vkCmdSetDepthBias(grCmdBuffer->commandBuffer, rasterState->depthBiasConstantFactor,
514514
rasterState->depthBiasClamp, rasterState->depthBiasSlopeFactor);
515-
516-
if (grCmdBuffer->grRasterState == NULL ||
517-
rasterState->polygonMode != grCmdBuffer->grRasterState->polygonMode) {
515+
if (grDevice->dynamicPolygonModeSupported) {
516+
VKD.vkCmdSetPolygonModeEXT(grCmdBuffer->commandBuffer, rasterState->polygonMode);
517+
}
518+
if ((grCmdBuffer->grRasterState == NULL ||
519+
rasterState->polygonMode != grCmdBuffer->grRasterState->polygonMode) && !grDevice->dynamicPolygonModeSupported) {
518520
bindPoint->dirtyFlags |= FLAG_DIRTY_PIPELINE;
519521
}
520522

@@ -570,10 +572,18 @@ GR_VOID GR_STDCALL grCmdBindStateObject(
570572
}
571573

572574
VKD.vkCmdSetBlendConstants(grCmdBuffer->commandBuffer, colorBlendState->blendConstants);
573-
574-
if (grCmdBuffer->grColorBlendState == NULL ||
575-
memcmp(colorBlendState->states, grCmdBuffer->grColorBlendState->states,
576-
sizeof(colorBlendState->states)) != 0) {
575+
if (grDevice->dynamicBlendEnableSupported) {
576+
VKD.vkCmdSetColorBlendEnableEXT(grCmdBuffer->commandBuffer, 0, GR_MAX_COLOR_TARGETS, colorBlendState->blendEnableStates);
577+
}
578+
if (grDevice->dynamicBlendEquationSupported) {
579+
VKD.vkCmdSetColorBlendEquationEXT(grCmdBuffer->commandBuffer, 0, GR_MAX_COLOR_TARGETS, colorBlendState->equationStates);
580+
}
581+
if (!(grDevice->dynamicBlendEnableSupported && grDevice->dynamicBlendEquationSupported) &&
582+
(grCmdBuffer->grColorBlendState == NULL ||
583+
(!grDevice->dynamicBlendEquationSupported && memcmp(colorBlendState->equationStates, grCmdBuffer->grColorBlendState->equationStates,
584+
sizeof(colorBlendState->equationStates)) != 0) ||
585+
(!grDevice->dynamicBlendEnableSupported && memcmp(colorBlendState->blendEnableStates, grCmdBuffer->grColorBlendState->blendEnableStates,
586+
sizeof(colorBlendState->blendEnableStates)) != 0))) {
577587
bindPoint->dirtyFlags |= FLAG_DIRTY_PIPELINE;
578588
}
579589

@@ -585,9 +595,16 @@ GR_VOID GR_STDCALL grCmdBindStateObject(
585595
break;
586596
}
587597

588-
if (grCmdBuffer->grMsaaState == NULL ||
589-
msaaState->sampleCountFlags != grCmdBuffer->grMsaaState->sampleCountFlags ||
590-
msaaState->sampleMask != grCmdBuffer->grMsaaState->sampleMask) {
598+
if (grDevice->dynamicSampleMaskSupported) {
599+
VKD.vkCmdSetSampleMaskEXT(grCmdBuffer->commandBuffer, msaaState->sampleCountFlags, &msaaState->sampleMask);
600+
}
601+
if (grDevice->dynamicRasterizationSamplesSupported) {
602+
VKD.vkCmdSetRasterizationSamplesEXT(grCmdBuffer->commandBuffer, msaaState->sampleCountFlags);
603+
}
604+
if (!(grDevice->dynamicSampleMaskSupported && grDevice->dynamicRasterizationSamplesSupported) &&
605+
(grCmdBuffer->grMsaaState == NULL ||
606+
(!grDevice->dynamicSampleMaskSupported && msaaState->sampleCountFlags != grCmdBuffer->grMsaaState->sampleCountFlags) ||
607+
(!grDevice->dynamicRasterizationSamplesSupported && msaaState->sampleMask != grCmdBuffer->grMsaaState->sampleMask))) {
591608
bindPoint->dirtyFlags |= FLAG_DIRTY_PIPELINE;
592609
}
593610

src/mantle/mantle_init_device.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,16 @@ GR_RESULT GR_STDCALL grCreateDevice(
764764
goto bail;
765765
}
766766

767+
VkPhysicalDeviceExtendedDynamicState3FeaturesEXT queriedDynamicState3Features = {
768+
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_3_FEATURES_EXT,
769+
.pNext = NULL,
770+
};
771+
VkPhysicalDeviceFeatures2 queriedDeviceFeatures = {
772+
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2,
773+
.pNext = &queriedDynamicState3Features,
774+
};
775+
vki.vkGetPhysicalDeviceFeatures2(grPhysicalGpu->physicalDevice, &queriedDeviceFeatures);
776+
767777
VkPhysicalDeviceCustomBorderColorFeaturesEXT customBorderColor = {
768778
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CUSTOM_BORDER_COLOR_FEATURES_EXT,
769779
.pNext = NULL,
@@ -800,9 +810,18 @@ GR_RESULT GR_STDCALL grCreateDevice(
800810
.pNext = &mutableDescriptorFeaturesEXT,
801811
.descriptorBuffer = VK_TRUE,
802812
};
813+
VkPhysicalDeviceExtendedDynamicState3FeaturesEXT dynamicState3Features = {
814+
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_3_FEATURES_EXT,
815+
.pNext = &descriptorBufferFeatures,
816+
.extendedDynamicState3ColorBlendEnable = queriedDynamicState3Features.extendedDynamicState3ColorBlendEnable,
817+
.extendedDynamicState3ColorBlendEquation = queriedDynamicState3Features.extendedDynamicState3ColorBlendEquation,
818+
.extendedDynamicState3SampleMask = queriedDynamicState3Features.extendedDynamicState3SampleMask,
819+
.extendedDynamicState3RasterizationSamples = queriedDynamicState3Features.extendedDynamicState3RasterizationSamples,
820+
.extendedDynamicState3PolygonMode = queriedDynamicState3Features.extendedDynamicState3PolygonMode,
821+
};
803822
VkPhysicalDeviceVulkan12Features vulkan12DeviceFeatures = {
804823
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES,
805-
.pNext = &descriptorBufferFeatures,
824+
.pNext = &dynamicState3Features,
806825
.runtimeDescriptorArray = VK_TRUE,
807826
.bufferDeviceAddress = VK_TRUE,
808827
.descriptorBindingVariableDescriptorCount = VK_TRUE,
@@ -869,16 +888,18 @@ GR_RESULT GR_STDCALL grCreateDevice(
869888
VK_EXT_MUTABLE_DESCRIPTOR_TYPE_EXTENSION_NAME,
870889
VK_VALVE_MUTABLE_DESCRIPTOR_TYPE_EXTENSION_NAME,
871890
NULL,
891+
NULL,
872892
};
873893

874-
unsigned deviceExtensionCount = COUNT_OF(deviceExtensions) - 1;
894+
unsigned deviceExtensionCount = COUNT_OF(deviceExtensions) - 2;
875895
bool descriptorBufferSupported = false;
876896

877897
for (unsigned i = 0; i < supportedExtensionCount; i++) {
878898
if (strcmp(extensionProperties[i].extensionName, VK_EXT_DESCRIPTOR_BUFFER_EXTENSION_NAME) == 0) {
879899
descriptorBufferSupported = true; // TODO: also check the extension properties
880900
deviceExtensions[deviceExtensionCount++] = VK_EXT_DESCRIPTOR_BUFFER_EXTENSION_NAME;
881-
break;
901+
} else if (strcmp(extensionProperties[i].extensionName, VK_EXT_EXTENDED_DYNAMIC_STATE_3_EXTENSION_NAME) == 0) {
902+
deviceExtensions[deviceExtensionCount++] = VK_EXT_EXTENDED_DYNAMIC_STATE_3_EXTENSION_NAME;
882903
}
883904
}
884905

@@ -962,6 +983,16 @@ GR_RESULT GR_STDCALL grCreateDevice(
962983
.maxMutableUniformDescriptorSize = 0, // Initialized below
963984
.maxMutableStorageDescriptorSize = 0, // Initialized below
964985
.maxMutableDescriptorSize = 0, // Initialized below
986+
.dynamicBlendEnableSupported = queriedDynamicState3Features.extendedDynamicState3ColorBlendEnable,
987+
.dynamicBlendEquationSupported = queriedDynamicState3Features.extendedDynamicState3ColorBlendEquation,
988+
.dynamicSampleMaskSupported = queriedDynamicState3Features.extendedDynamicState3SampleMask,
989+
.dynamicRasterizationSamplesSupported = queriedDynamicState3Features.extendedDynamicState3RasterizationSamples,
990+
.dynamicPolygonModeSupported = queriedDynamicState3Features.extendedDynamicState3PolygonMode,
991+
.singleSlotGraphicsPipelineSupported = queriedDynamicState3Features.extendedDynamicState3ColorBlendEnable &&
992+
queriedDynamicState3Features.extendedDynamicState3ColorBlendEquation &&
993+
queriedDynamicState3Features.extendedDynamicState3SampleMask &&
994+
queriedDynamicState3Features.extendedDynamicState3RasterizationSamples &&
995+
queriedDynamicState3Features.extendedDynamicState3PolygonMode,
965996
};
966997

967998
if (grDevice->descriptorBufferSupported) {

src/mantle/mantle_object.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ typedef struct _GrCmdBuffer {
201201

202202
typedef struct _GrColorBlendStateObject {
203203
GrObject grObj;
204-
VkPipelineColorBlendAttachmentState states[GR_MAX_COLOR_TARGETS];
204+
VkBool32 blendEnableStates[GR_MAX_COLOR_TARGETS];
205+
VkColorBlendEquationEXT equationStates[GR_MAX_COLOR_TARGETS];
205206
float blendConstants[4];
206207
} GrColorBlendStateObject;
207208

@@ -282,6 +283,12 @@ typedef struct _GrDevice {
282283
uint32_t maxMutableUniformDescriptorSize;
283284
uint32_t maxMutableStorageDescriptorSize;
284285
uint32_t maxMutableDescriptorSize;
286+
bool dynamicBlendEnableSupported;
287+
bool dynamicBlendEquationSupported;
288+
bool dynamicSampleMaskSupported;
289+
bool dynamicRasterizationSamplesSupported;
290+
bool dynamicPolygonModeSupported;
291+
bool singleSlotGraphicsPipelineSupported;
285292
} GrDevice;
286293

287294
typedef struct _GrEvent {

src/mantle/mantle_shader_pipeline.c

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ static VkPipeline getVkPipeline(
419419
.flags = 0,
420420
.depthClampEnable = VK_TRUE,
421421
.rasterizerDiscardEnable = VK_FALSE,
422-
.polygonMode = grRasterState->polygonMode,
422+
.polygonMode = grDevice->dynamicPolygonModeSupported ? 0 : grRasterState->polygonMode, // Optional dynamic state
423423
.cullMode = 0, // Dynamic state
424424
.frontFace = 0, // Dynamic state
425425
.depthBiasEnable = VK_TRUE,
@@ -433,10 +433,10 @@ static VkPipeline getVkPipeline(
433433
.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
434434
.pNext = NULL,
435435
.flags = 0,
436-
.rasterizationSamples = grMsaaState->sampleCountFlags,
436+
.rasterizationSamples = grDevice->dynamicRasterizationSamplesSupported ? VK_SAMPLE_COUNT_1_BIT : grMsaaState->sampleCountFlags,
437437
.sampleShadingEnable = VK_FALSE,
438438
.minSampleShading = 0.f,
439-
.pSampleMask = &grMsaaState->sampleMask,
439+
.pSampleMask = grDevice->dynamicSampleMaskSupported ? NULL : &grMsaaState->sampleMask,
440440
.alphaToCoverageEnable = createInfo->alphaToCoverageEnable,
441441
.alphaToOneEnable = VK_FALSE,
442442
};
@@ -462,15 +462,16 @@ static VkPipeline getVkPipeline(
462462
assert(colorFormatCount < GR_MAX_COLOR_TARGETS);
463463

464464
for (unsigned i = 0; i < GR_MAX_COLOR_TARGETS; i++) {
465-
const VkPipelineColorBlendAttachmentState* blendState = &grColorBlendState->states[i];
465+
const VkColorBlendEquationEXT* blendState = &grColorBlendState->equationStates[i];
466+
VkBool32 blendEnabled = grColorBlendState->blendEnableStates[i];
466467
VkColorComponentFlags colorWriteMask = createInfo->colorWriteMasks[i];
467468

468469
if (colorWriteMask == ~0u) {
469470
continue;
470471
}
471472

472473
attachments[attachmentCount] = (VkPipelineColorBlendAttachmentState) {
473-
.blendEnable = blendState->blendEnable,
474+
.blendEnable = blendEnabled,
474475
.srcColorBlendFactor = blendState->srcColorBlendFactor,
475476
.dstColorBlendFactor = blendState->dstColorBlendFactor,
476477
.colorBlendOp = blendState->colorBlendOp,
@@ -508,7 +509,7 @@ static VkPipeline getVkPipeline(
508509
.blendConstants = { 0.f }, // Dynamic state
509510
};
510511

511-
const VkDynamicState dynamicStates[] = {
512+
VkDynamicState dynamicStates[] = {
512513
VK_DYNAMIC_STATE_DEPTH_BIAS,
513514
VK_DYNAMIC_STATE_BLEND_CONSTANTS,
514515
VK_DYNAMIC_STATE_DEPTH_BOUNDS,
@@ -525,13 +526,29 @@ static VkPipeline getVkPipeline(
525526
VK_DYNAMIC_STATE_DEPTH_BOUNDS_TEST_ENABLE_EXT,
526527
VK_DYNAMIC_STATE_STENCIL_TEST_ENABLE_EXT,
527528
VK_DYNAMIC_STATE_STENCIL_OP_EXT,
529+
0, 0, 0, 0, 0
528530
};
529-
531+
unsigned dynamicStateCount = COUNT_OF(dynamicStates) - 5;
532+
if (grDevice->dynamicBlendEnableSupported) {
533+
dynamicStates[dynamicStateCount++] = VK_DYNAMIC_STATE_COLOR_BLEND_ENABLE_EXT;
534+
}
535+
if (grDevice->dynamicBlendEquationSupported) {
536+
dynamicStates[dynamicStateCount++] = VK_DYNAMIC_STATE_COLOR_BLEND_EQUATION_EXT;
537+
}
538+
if (grDevice->dynamicSampleMaskSupported) {
539+
dynamicStates[dynamicStateCount++] = VK_DYNAMIC_STATE_SAMPLE_MASK_EXT;
540+
}
541+
if (grDevice->dynamicRasterizationSamplesSupported) {
542+
dynamicStates[dynamicStateCount++] = VK_DYNAMIC_STATE_RASTERIZATION_SAMPLES_EXT;
543+
}
544+
if (grDevice->dynamicPolygonModeSupported) {
545+
dynamicStates[dynamicStateCount++] = VK_DYNAMIC_STATE_POLYGON_MODE_EXT;
546+
}
530547
const VkPipelineDynamicStateCreateInfo dynamicStateCreateInfo = {
531548
.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
532549
.pNext = NULL,
533550
.flags = 0,
534-
.dynamicStateCount = COUNT_OF(dynamicStates),
551+
.dynamicStateCount = dynamicStateCount,
535552
.pDynamicStates = dynamicStates,
536553
};
537554

@@ -587,16 +604,17 @@ VkPipeline grPipelineFindOrCreateVkPipeline(
587604
const VkFormat* colorFormats,
588605
VkFormat depthStencilFormat)
589606
{
607+
GrDevice* grDevice = GET_OBJ_DEVICE(grPipeline);
590608
VkPipeline vkPipeline = VK_NULL_HANDLE;
591609

592610
AcquireSRWLockShared(&grPipeline->pipelineSlotsLock);
593611
// Assume that the attachment formats never change to reduce pipeline lookup overhead
594612
for (unsigned i = 0; i < grPipeline->pipelineSlotCount; i++) {
595613
const PipelineSlot* slot = &grPipeline->pipelineSlots[i];
596614

597-
if (grColorBlendState == slot->grColorBlendState &&
598-
grMsaaState == slot->grMsaaState &&
599-
grRasterState == slot->grRasterState) {
615+
if (((grDevice->dynamicBlendEnableSupported && grDevice->dynamicBlendEquationSupported) || grColorBlendState == slot->grColorBlendState) &&
616+
((grDevice->dynamicSampleMaskSupported && grDevice->dynamicRasterizationSamplesSupported) || grMsaaState == slot->grMsaaState) &&
617+
(grDevice->dynamicPolygonModeSupported || grRasterState == slot->grRasterState)) {
600618
vkPipeline = slot->pipeline;
601619
break;
602620
}
@@ -612,9 +630,9 @@ VkPipeline grPipelineFindOrCreateVkPipeline(
612630
for (unsigned i = 0; i < grPipeline->pipelineSlotCount; i++) {
613631
const PipelineSlot* slot = &grPipeline->pipelineSlots[i];
614632

615-
if (grColorBlendState == slot->grColorBlendState &&
616-
grMsaaState == slot->grMsaaState &&
617-
grRasterState == slot->grRasterState) {
633+
if (((grDevice->dynamicBlendEnableSupported && grDevice->dynamicBlendEquationSupported) || grColorBlendState == slot->grColorBlendState) &&
634+
((grDevice->dynamicSampleMaskSupported && grDevice->dynamicRasterizationSamplesSupported) || grMsaaState == slot->grMsaaState) &&
635+
(grDevice->dynamicPolygonModeSupported || grRasterState == slot->grRasterState)) {
618636
vkPipeline = slot->pipeline;
619637
break;
620638
}

src/mantle/mantle_state_object.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ GR_RESULT GR_STDCALL grCreateColorBlendState(
132132

133133
*grColorBlendStateObject = (GrColorBlendStateObject) {
134134
.grObj = { GR_OBJ_TYPE_COLOR_BLEND_STATE_OBJECT, grDevice },
135-
.states = { { 0 } }, // Initialized below
135+
.equationStates = { { 0 } }, // Initialized below
136+
.blendEnableStates = { false }, // Initialized below
136137
.blendConstants = {
137138
pCreateInfo->blendConst[0], pCreateInfo->blendConst[1],
138139
pCreateInfo->blendConst[2], pCreateInfo->blendConst[3],
@@ -142,27 +143,24 @@ GR_RESULT GR_STDCALL grCreateColorBlendState(
142143
for (unsigned i = 0; i < GR_MAX_COLOR_TARGETS; i++) {
143144
const GR_COLOR_TARGET_BLEND_STATE* blendState = &pCreateInfo->target[i];
144145

146+
grColorBlendStateObject->blendEnableStates[i] = blendState->blendEnable ? VK_TRUE : VK_FALSE;
145147
if (blendState->blendEnable) {
146-
grColorBlendStateObject->states[i] = (VkPipelineColorBlendAttachmentState) {
147-
.blendEnable = VK_TRUE,
148+
grColorBlendStateObject->equationStates[i] = (VkColorBlendEquationEXT) {
148149
.srcColorBlendFactor = getVkBlendFactor(blendState->srcBlendColor),
149150
.dstColorBlendFactor = getVkBlendFactor(blendState->destBlendColor),
150151
.colorBlendOp = getVkBlendOp(blendState->blendFuncColor),
151152
.srcAlphaBlendFactor = getVkBlendFactor(blendState->srcBlendAlpha),
152153
.dstAlphaBlendFactor = getVkBlendFactor(blendState->destBlendAlpha),
153154
.alphaBlendOp = getVkBlendOp(blendState->blendFuncAlpha),
154-
.colorWriteMask = 0, // Defined at pipeline creation
155155
};
156156
} else {
157-
grColorBlendStateObject->states[i] = (VkPipelineColorBlendAttachmentState) {
158-
.blendEnable = VK_FALSE,
157+
grColorBlendStateObject->equationStates[i] = (VkColorBlendEquationEXT) {
159158
.srcColorBlendFactor = 0, // Ignored
160159
.dstColorBlendFactor = 0, // Ignored
161160
.colorBlendOp = 0, // Ignored
162161
.srcAlphaBlendFactor = 0, // Ignored
163162
.dstAlphaBlendFactor = 0, // Ignored
164163
.alphaBlendOp = 0, // Ignored
165-
.colorWriteMask = 0, // Defined at pipeline creation
166164
};
167165
}
168166
}

0 commit comments

Comments
 (0)