microsoft / DirectXShaderCompiler

This repo hosts the source for the DirectX Shader Compiler which is based on LLVM/Clang.
Other
3.05k stars 677 forks source link

Anyway use dxc in uwp at runtime(without load dll)? #3680

Closed ganluo960214 closed 3 years ago

ganluo960214 commented 3 years ago

try to write dxr in uwp project use , it need load dll at runtime, but uwp not allow it.

I just want the hlsl result(pIDxcCompiler->Compile). Anyway can do it? I also a cpp noob.

#pragma region
    std::array<D3D12_STATE_SUBOBJECT,10> Subobjects;
    uint32_t index = 0;

    // 1 dxil
#pragma region
    {
        // 1
        ComPtr<IDxcLibrary> pIDxcLibrary;
        ThrowIfFailed(DxcCreateInstance(CLSID_DxcLibrary, IID_PPV_ARGS(&pIDxcLibrary)));

        ComPtr<IDxcCompiler> pIDxcCompiler;
        ThrowIfFailed(DxcCreateInstance(CLSID_DxcCompiler, IID_PPV_ARGS(&pIDxcCompiler)));

        uint32_t codePage = CP_UTF8;
        ComPtr<IDxcBlobEncoding> pIDxcSourceBlob;
        ThrowIfFailed(pIDxcLibrary->CreateBlobFromFile(L"PS.hlsl", &codePage, &pIDxcSourceBlob));

        ComPtr<IDxcOperationResult> pIDxcOperationResult; 
        ThrowIfFailed(pIDxcCompiler->Compile(
            pIDxcSourceBlob.Get(), // pSource
            L"PS.hlsl", // pSourceName
            L"main", // pEntryPoint
            L"PS_6_5", // pTargetProfile
            NULL, 0, // pArguments, argCount
            NULL, 0, // pDefines, defineCount
            NULL, // pIncludeHandler
            &pIDxcOperationResult));

        HRESULT compileHResult;
        ThrowIfFailed(pIDxcOperationResult->GetStatus(&compileHResult));

        ComPtr<IDxcBlob> pBlob;
        ThrowIfFailed(pIDxcOperationResult->GetResult(&pBlob));

        // 2
        std::vector<D3D12_EXPORT_DESC> ExportDesc;
        std::vector<std::wstring> ExportName;
        ExportDesc.resize(ARRAYSIZE(kDxrShaderEntryPoints));
        ExportName.resize(ARRAYSIZE(kDxrShaderEntryPoints));
        for (uint32_t i = 0; i < ARRAYSIZE(kDxrShaderEntryPoints); i++)
        {
            ExportName[i] = kDxrShaderEntryPoints[i];
            ExportDesc[i].Name = ExportName[i].c_str();
            ExportDesc[i].Flags = D3D12_EXPORT_FLAG_NONE;
            ExportDesc[i].ExportToRename = nullptr;
        }

        // 3
        D3D12_DXIL_LIBRARY_DESC dxilLibDesc = {};
        dxilLibDesc.DXILLibrary.pShaderBytecode = pBlob->GetBufferPointer();
        dxilLibDesc.DXILLibrary.BytecodeLength = pBlob->GetBufferSize();
        dxilLibDesc.NumExports = ARRAYSIZE(kDxrShaderEntryPoints);
        dxilLibDesc.pExports = ExportDesc.data();       

        // 4
        D3D12_STATE_SUBOBJECT stDXILLibSubobject = {};
        stDXILLibSubobject.Type = D3D12_STATE_SUBOBJECT_TYPE_DXIL_LIBRARY;
        stDXILLibSubobject.pDesc = &dxilLibDesc;

        Subobjects[index] = stDXILLibSubobject;
        index++;
    }
#pragma endregion
#pragma endregion
ganluo960214 commented 3 years ago

I follow the instructions, run the 'hctbuild'.

Chang the 'Clang libraries'\'dxcompiler' cmake file 123 line to add_clang_library(dxcompiler STATIC ${SOURCES}) get a dxcompiler.lib. try use static library in link stage but failed.