| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #include "RenderAPI/BsGpuPipelineState.h" |
| 4 | #include "RenderAPI/BsRasterizerState.h" |
| 5 | #include "RenderAPI/BsBlendState.h" |
| 6 | #include "RenderAPI/BsDepthStencilState.h" |
| 7 | #include "RenderAPI/BsGpuProgram.h" |
| 8 | #include "RenderAPI/BsGpuParamDesc.h" |
| 9 | #include "RenderAPI/BsGpuPipelineParamInfo.h" |
| 10 | #include "Managers/BsRenderStateManager.h" |
| 11 | |
| 12 | namespace bs |
| 13 | { |
| 14 | /** Converts a sim thread pipeline state descriptor to a core thread one. */ |
| 15 | void convertPassDesc(const PIPELINE_STATE_DESC& input, ct::PIPELINE_STATE_DESC& output) |
| 16 | { |
| 17 | output.blendState = input.blendState != nullptr ? input.blendState->getCore() : nullptr; |
| 18 | output.rasterizerState = input.rasterizerState != nullptr ? input.rasterizerState->getCore() : nullptr; |
| 19 | output.depthStencilState = input.depthStencilState != nullptr ? input.depthStencilState->getCore() : nullptr; |
| 20 | output.vertexProgram = input.vertexProgram != nullptr ? input.vertexProgram->getCore() : nullptr; |
| 21 | output.fragmentProgram = input.fragmentProgram != nullptr ? input.fragmentProgram->getCore() : nullptr; |
| 22 | output.geometryProgram = input.geometryProgram != nullptr ? input.geometryProgram->getCore() : nullptr; |
| 23 | output.hullProgram = input.hullProgram != nullptr ? input.hullProgram->getCore() : nullptr; |
| 24 | output.domainProgram = input.domainProgram != nullptr ? input.domainProgram->getCore() : nullptr; |
| 25 | } |
| 26 | |
| 27 | template<bool Core> |
| 28 | TGraphicsPipelineState<Core>::TGraphicsPipelineState(const StateDescType& data) |
| 29 | :mData(data) |
| 30 | { } |
| 31 | |
| 32 | template class TGraphicsPipelineState < false > ; |
| 33 | template class TGraphicsPipelineState < true >; |
| 34 | |
| 35 | GraphicsPipelineState::GraphicsPipelineState(const PIPELINE_STATE_DESC& desc) |
| 36 | :TGraphicsPipelineState(desc) |
| 37 | { |
| 38 | GPU_PIPELINE_PARAMS_DESC paramsDesc; |
| 39 | if (desc.vertexProgram != nullptr) |
| 40 | { |
| 41 | desc.vertexProgram->blockUntilCoreInitialized(); |
| 42 | paramsDesc.vertexParams = desc.vertexProgram->getParamDesc(); |
| 43 | } |
| 44 | |
| 45 | if (desc.fragmentProgram != nullptr) |
| 46 | { |
| 47 | desc.fragmentProgram->blockUntilCoreInitialized(); |
| 48 | paramsDesc.fragmentParams = desc.fragmentProgram->getParamDesc(); |
| 49 | } |
| 50 | |
| 51 | if (desc.geometryProgram != nullptr) |
| 52 | { |
| 53 | desc.geometryProgram->blockUntilCoreInitialized(); |
| 54 | paramsDesc.geometryParams = desc.geometryProgram->getParamDesc(); |
| 55 | } |
| 56 | |
| 57 | if (desc.hullProgram != nullptr) |
| 58 | { |
| 59 | desc.hullProgram->blockUntilCoreInitialized(); |
| 60 | paramsDesc.hullParams = desc.hullProgram->getParamDesc(); |
| 61 | } |
| 62 | |
| 63 | if (desc.domainProgram != nullptr) |
| 64 | { |
| 65 | desc.domainProgram->blockUntilCoreInitialized(); |
| 66 | paramsDesc.domainParams = desc.domainProgram->getParamDesc(); |
| 67 | } |
| 68 | |
| 69 | mParamInfo = GpuPipelineParamInfo::create(paramsDesc); |
| 70 | } |
| 71 | |
| 72 | SPtr<ct::GraphicsPipelineState> GraphicsPipelineState::getCore() const |
| 73 | { |
| 74 | return std::static_pointer_cast<ct::GraphicsPipelineState>(mCoreSpecific); |
| 75 | } |
| 76 | |
| 77 | SPtr<ct::CoreObject> GraphicsPipelineState::createCore() const |
| 78 | { |
| 79 | ct::PIPELINE_STATE_DESC desc; |
| 80 | convertPassDesc(mData, desc); |
| 81 | |
| 82 | return ct::RenderStateManager::instance()._createGraphicsPipelineState(desc); |
| 83 | } |
| 84 | |
| 85 | SPtr<GraphicsPipelineState> GraphicsPipelineState::create(const PIPELINE_STATE_DESC& desc) |
| 86 | { |
| 87 | return RenderStateManager::instance().createGraphicsPipelineState(desc); |
| 88 | } |
| 89 | |
| 90 | template<bool Core> |
| 91 | TComputePipelineState<Core>::TComputePipelineState() |
| 92 | { } |
| 93 | |
| 94 | template<bool Core> |
| 95 | TComputePipelineState<Core>::TComputePipelineState(const GpuProgramType& program) |
| 96 | :mProgram(program) |
| 97 | { } |
| 98 | |
| 99 | template class TComputePipelineState < false >; |
| 100 | template class TComputePipelineState < true >; |
| 101 | |
| 102 | ComputePipelineState::ComputePipelineState(const SPtr<GpuProgram>& program) |
| 103 | :TComputePipelineState(program) |
| 104 | { |
| 105 | GPU_PIPELINE_PARAMS_DESC paramsDesc; |
| 106 | program->blockUntilCoreInitialized(); |
| 107 | paramsDesc.computeParams = program->getParamDesc(); |
| 108 | |
| 109 | mParamInfo = GpuPipelineParamInfo::create(paramsDesc); |
| 110 | } |
| 111 | |
| 112 | SPtr<ct::ComputePipelineState> ComputePipelineState::getCore() const |
| 113 | { |
| 114 | return std::static_pointer_cast<ct::ComputePipelineState>(mCoreSpecific); |
| 115 | } |
| 116 | |
| 117 | SPtr<ct::CoreObject> ComputePipelineState::createCore() const |
| 118 | { |
| 119 | return ct::RenderStateManager::instance()._createComputePipelineState(mProgram->getCore()); |
| 120 | } |
| 121 | |
| 122 | SPtr<ComputePipelineState> ComputePipelineState::create(const SPtr<GpuProgram>& program) |
| 123 | { |
| 124 | return RenderStateManager::instance().createComputePipelineState(program); |
| 125 | } |
| 126 | |
| 127 | namespace ct |
| 128 | { |
| 129 | GraphicsPipelineState::GraphicsPipelineState(const PIPELINE_STATE_DESC& desc, GpuDeviceFlags deviceMask) |
| 130 | :TGraphicsPipelineState(desc), mDeviceMask(deviceMask) |
| 131 | { } |
| 132 | |
| 133 | void GraphicsPipelineState::initialize() |
| 134 | { |
| 135 | GPU_PIPELINE_PARAMS_DESC paramsDesc; |
| 136 | if (mData.vertexProgram != nullptr) |
| 137 | paramsDesc.vertexParams = mData.vertexProgram->getParamDesc(); |
| 138 | |
| 139 | if (mData.fragmentProgram != nullptr) |
| 140 | paramsDesc.fragmentParams = mData.fragmentProgram->getParamDesc(); |
| 141 | |
| 142 | if (mData.geometryProgram != nullptr) |
| 143 | paramsDesc.geometryParams = mData.geometryProgram->getParamDesc(); |
| 144 | |
| 145 | if (mData.hullProgram != nullptr) |
| 146 | paramsDesc.hullParams = mData.hullProgram->getParamDesc(); |
| 147 | |
| 148 | if (mData.domainProgram != nullptr) |
| 149 | paramsDesc.domainParams = mData.domainProgram->getParamDesc(); |
| 150 | |
| 151 | mParamInfo = GpuPipelineParamInfo::create(paramsDesc, mDeviceMask); |
| 152 | |
| 153 | CoreObject::initialize(); |
| 154 | } |
| 155 | |
| 156 | SPtr<GraphicsPipelineState> GraphicsPipelineState::create(const PIPELINE_STATE_DESC& desc, GpuDeviceFlags deviceMask) |
| 157 | { |
| 158 | return RenderStateManager::instance().createGraphicsPipelineState(desc, deviceMask); |
| 159 | } |
| 160 | |
| 161 | ComputePipelineState::ComputePipelineState(const SPtr<GpuProgram>& program, GpuDeviceFlags deviceMask) |
| 162 | :TComputePipelineState(program), mDeviceMask(deviceMask) |
| 163 | { } |
| 164 | |
| 165 | void ComputePipelineState::initialize() |
| 166 | { |
| 167 | GPU_PIPELINE_PARAMS_DESC paramsDesc; |
| 168 | paramsDesc.computeParams = mProgram->getParamDesc(); |
| 169 | |
| 170 | mParamInfo = GpuPipelineParamInfo::create(paramsDesc, mDeviceMask); |
| 171 | |
| 172 | CoreObject::initialize(); |
| 173 | } |
| 174 | |
| 175 | SPtr<ComputePipelineState> ComputePipelineState::create(const SPtr<GpuProgram>& program, |
| 176 | GpuDeviceFlags deviceMask) |
| 177 | { |
| 178 | return RenderStateManager::instance().createComputePipelineState(program, deviceMask); |
| 179 | } |
| 180 | } |
| 181 | } |