如何实现可编程着色器阶段状态?

时间:2018-12-02 05:27:48

标签: directx-11

我正在尝试学习Directx11概念和3D游戏编程。我正在读书https://www.amazon.com/Practical-Rendering-Computation-Direct3D-11/dp/1568817207

我正在尝试实现渲染管线。我的一些课程是:

class ShaderStage
{
public:
  virtual ~ShaderStage() = default;

  // all shader stages must implement these methods
  virtual bool BindShader(Shader* shader) = 0;
  virtual bool BindConstantBuffers(uint32_t start_slot, uint32_t count, ID3D11Buffer * const *buffers) = 0;
  virtual bool BindSamplers(uint32_t start_slot, uint32_t count, ID3D11SamplerState *const *samplers) = 0;
  virtual bool BindShaderResourceViews(uint32_t start_slot, uint32_t count, ID3D11ShaderResourceView * const *views) = 0;

  // only computer shader stage supports Unordered Access View(UAV)
  virtual bool BindUnorderedAccessViews(uint32_t start_slot, uint32_t count, 
ID3D11UnorderedAccessView * const *views, const uint32_t* uav_counters)
  {
    return false;
  }

protected:
  ShaderStage(ID3D11DeviceContext* context)
: context_(context)
  {
  }
  ID3D11DeviceContext* context_;
};

class VertexShaderStage : public ShaderStage
{
public:
  VertexShaderStage(ID3D11DeviceContext* context): ShaderStage(context)
  {
  }

  virtual ~VertexShaderStage() = default;

  bool BindShader(Shader* shader) override
  {
    const auto vertex_shader = dynamic_cast<VertexShader*>(shader);
    if (vertex_shader == nullptr)
      return  false;

    context_->VSSetShader(vertex_shader->VertexShaderPtr(), nullptr, 0);
    return true;
  }

  bool BindConstantBuffers(uint32_t start_slot, uint32_t count, ID3D11Buffer * const *buffers) override
  {
    if (buffers == nullptr || count==0)
      return false;
    context_->VSSetConstantBuffers(start_slot, count, buffers);
    return true;
  }

  bool BindSamplers(uint32_t start_slot, uint32_t count, ID3D11SamplerState *const *samplers) override
  {
    if (samplers == nullptr)
      return false;
    context_->VSSetSamplers(start_slot, count, samplers);
    return true;
  }

  bool BindShaderResourceViews(uint32_t start_slot, uint32_t count, ID3D11ShaderResourceView * const *views) override
  {
    if (views == nullptr)
      return false;
    context_->VSSetShaderResources(start_slot, count, views);
    return true;
  }

};

我知道每个可编程着色器阶段都可以具有Shader ProgramContant BuffersShader Resource ViewsSamplersUnordered Access Views。 但是由于我仍处于学习渲染管线的早期阶段,所以想知道什么是实现可以设置/取消设置constant buffer slotsshader resource view slots等的Shader State类的最佳方法。

一些专家的意见将不胜感激。

0 个答案:

没有答案