zoukankan      html  css  js  c++  java
  • Shader

    ///////////////Begin Shader Source File/////////////////////

    cbuffer cbChangesPerFrame : register( b0 )
    {
    matrix mvp_;
    };


    Texture2D colorMap_ : register( t0 );
    SamplerState colorSampler_ : register( s0 );


    struct VS_Input
    {
    float4 pos : POSITION;
    float2 tex0 : TEXCOORD0;
    };

    struct PS_Input
    {
    float4 pos : SV_POSITION;
    float2 tex0 : TEXCOORD0;
    };


    PS_Input VS_Main( VS_Input vertex )
    {
    PS_Input vsOut = ( PS_Input )0;
    vsOut.pos = mul( vertex.pos, mvp_ );
    vsOut.tex0 = vertex.tex0;

    return vsOut;
    }


    float4 PS_Main( PS_Input frag ) : SV_TARGET
    {
    return colorMap_.Sample( colorSampler_, frag.tex0 );
    }

    ////////////////////////End Shader Source File////////////////////

    //t0 s0 b0
    传入值得方式
    d3dContext_->PSSetShaderResources( 0, 1, &colorMap_ );
    d3dContext_->PSSetSamplers( 0, 1, &colorMapSampler_ );

    d3dContext_->VSSetConstantBuffers( 0, 1, &mvpCB_ );

    //////////////编译Shader///////////////////

    ID3DBlob* vsBuffer = 0;
    //bool comileResult = CompileD3DShader("TextureMap.fx","VS_Main","vs_4_0",&vsBuffer);
    if(compileResult == false) return false;
    ID3DBlob* errorBuffe = 0;
    HRESULT result;

    result = D3DX11CompileFromFile("TextureMap.fx",0,0,"VS_Main","vs_4_0",D3DCOMPILE_ENABLE_STRICENESS,0,0,vsBuffer,&errorBuffe,0);
    if(FAILED(result)
    {
    if(errorBuffe != 0) errorBuffe->release();
    }

    if(errorBuffe != 0) errorBuffe->release();

    HRESULT d3dResult;
    ID3DVertexShader* vsShader = 0;
    d3dResult = d3dDevice_->CreateVertexShader(vsBuffer->GetBufferPointer(),vsBuffer->GetBufferSize(),0,&vsShader);
    if(FAILED(d3dResult))
    {
    if(vsBuffer) vsBuffer->Release();
    return false;
    }

    D3D11_INPUT_ELEMENT_DES vsLayout[]=
    {
    {"POSITION",0,DCGI_FORMART_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0}.
    {"TEXCOORD",0,DXGI_FORMAT_R32G32_FLOAT,0,12,D3D11_INPUT_PER_VERTEX_DATA,0}
    };

    unsigned int totalLayoutElements = ARRAYSIZE(vsLayout);

    ID3D11InputLayout* inputLayout_ = 0;
    d3dResutl = d3dDevice_->CreateInputLayout(vsLayout,totalLayoutElements,vsBuffer->GetBufferPointer,vsBuffer->GetBufferSize,&inputLayout_);
    vsBuffer_->Release();


    //创建ConstantBuffer
    D3D11_BUFFER_DESC constDesc;
    ZeroMemory(&constDesc,sizeof(constDesc));
    constDesc.BindFlag = D3D11_BIND_CONSTANT_BUFFER;
    constDesc.ByteWidth = sizeof(XMMATRIX);
    constDesc.Usage = D3D11_USAGE_DEFAULT;

    ID3D11Buffer* mvpCB_;
    d3dResult = d3dDevice_->CreateBuffer(&constDesc,-,&mvpCB_)'

    //更新constBuffer
    d3dConetxt_->UpdateSubresource(mvpCB_,0,0,&data,0,0);
    d3dContext_->PSSetConstantBuffers(0,1,&mvpCB_);


    ///////////////////Example Use Const Buffer//////////////////////////////////////

    ID3D11Buffer* g_pConstantBuffer11 = NULL;

    // Define the constant data used to communicate with shaders.
    struct VS_CONSTANT_BUFFER
    {
    XMFLOAT4X4 mWorldViewProj;
    XMFLOAT4 vSomeVectorThatMayBeNeededByASpecificShader;
    float fSomeFloatThatMayBeNeededByASpecificShader;
    float fTime;
    float fSomeFloatThatMayBeNeededByASpecificShader2;
    float fSomeFloatThatMayBeNeededByASpecificShader3;
    } VS_CONSTANT_BUFFER;

    // Supply the vertex shader constant data.
    VS_CONSTANT_BUFFER VsConstData;
    VsConstData.mWorldViewProj = {...};
    VsConstData.vSomeVectorThatMayBeNeededByASpecificShader = XMFLOAT4(1,2,3,4);
    VsConstData.fSomeFloatThatMayBeNeededByASpecificShader = 3.0f;
    VsConstData.fTime = 1.0f;
    VsConstData.fSomeFloatThatMayBeNeededByASpecificShader2 = 2.0f;
    VsConstData.fSomeFloatThatMayBeNeededByASpecificShader3 = 4.0f;

    // Fill in a buffer description.
    D3D11_BUFFER_DESC cbDesc;
    cbDesc.ByteWidth = sizeof( VS_CONSTANT_BUFFER );
    cbDesc.Usage = D3D11_USAGE_DYNAMIC;
    cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
    cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
    cbDesc.MiscFlags = 0;
    cbDesc.StructureByteStride = 0;

    // Fill in the subresource data.
    D3D11_SUBRESOURCE_DATA InitData;
    InitData.pSysMem = &VsConstData;
    InitData.SysMemPitch = 0;
    InitData.SysMemSlicePitch = 0;

    // Create the buffer.
    hr = g_pd3dDevice->CreateBuffer( &cbDesc, &InitData,
    &g_pConstantBuffer11 );

    if( FAILED( hr ) )
    return hr;

    // Set the buffer.
    g_pd3dContext->VSSetConstantBuffers( 0, 1, &g_pConstantBuffer11 );


    This example shows the associated HLSL cbuffer definition.
    cbuffer VS_CONSTANT_BUFFER : register(b0)
    {
    matrix mWorldViewProj;
    float4 vSomeVectorThatMayBeNeededByASpecificShader;
    float fSomeFloatThatMayBeNeededByASpecificShader;
    float fTime;
    float fSomeFloatThatMayBeNeededByASpecificShader2;
    float fSomeFloatThatMayBeNeededByASpecificShader3;
    };

    作者:长风 Email:844064492@qq.com QQ群:607717453 Git:https://github.com/zhaohu19910409Dz 开源项目:https://github.com/OriginMEK/MEK 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利. 感谢您的阅读。如果觉得有用的就请各位大神高抬贵手“推荐一下”吧!你的精神支持是博主强大的写作动力。 如果觉得我的博客有意思,欢迎点击首页左上角的“+加关注”按钮关注我!
  • 相关阅读:
    超大文件排序
    透彻理解迪杰斯特拉算法
    Floyd-傻子也能看懂的弗洛伊德算法(转)
    轻松实现在浏览器上播放本地视频
    Caffeine缓存处理
    每日日报94
    每日日报93
    下载安装SQL server2008的步骤
    每日日报92
    每日日报91
  • 原文地址:https://www.cnblogs.com/zhaohu/p/6995039.html
Copyright © 2011-2022 走看看