//
// Copyright (c) 2012-2014 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//

// ShaderExecutable11.cpp: Implements a D3D11-specific class to contain shader
// executable implementation details.

#include "libANGLE/renderer/d3d/d3d11/ShaderExecutable11.h"
#include "libANGLE/renderer/d3d/d3d11/Renderer11.h"

namespace rx
{

ShaderExecutable11::ShaderExecutable11(const void *function,
                                       size_t length,
                                       d3d11::PixelShader &&executable)
    : ShaderExecutableD3D(function, length),
      mPixelExecutable(std::move(executable)),
      mVertexExecutable(),
      mGeometryExecutable(),
      mStreamOutExecutable(),
      mComputeExecutable()
{
}

ShaderExecutable11::ShaderExecutable11(const void *function,
                                       size_t length,
                                       d3d11::VertexShader &&executable,
                                       d3d11::GeometryShader &&streamOut)
    : ShaderExecutableD3D(function, length),
      mPixelExecutable(),
      mVertexExecutable(std::move(executable)),
      mGeometryExecutable(),
      mStreamOutExecutable(std::move(streamOut)),
      mComputeExecutable()
{
}

ShaderExecutable11::ShaderExecutable11(const void *function,
                                       size_t length,
                                       d3d11::GeometryShader &&executable)
    : ShaderExecutableD3D(function, length),
      mPixelExecutable(),
      mVertexExecutable(),
      mGeometryExecutable(std::move(executable)),
      mStreamOutExecutable(),
      mComputeExecutable()
{
}

ShaderExecutable11::ShaderExecutable11(const void *function,
                                       size_t length,
                                       d3d11::ComputeShader &&executable)
    : ShaderExecutableD3D(function, length),
      mPixelExecutable(),
      mVertexExecutable(),
      mGeometryExecutable(),
      mStreamOutExecutable(),
      mComputeExecutable(std::move(executable))
{
}

ShaderExecutable11::~ShaderExecutable11()
{
}

ID3D11VertexShader *ShaderExecutable11::getVertexShader() const
{
    return mVertexExecutable.get();
}

ID3D11PixelShader *ShaderExecutable11::getPixelShader() const
{
    return mPixelExecutable.get();
}

ID3D11GeometryShader *ShaderExecutable11::getGeometryShader() const
{
    return mGeometryExecutable.get();
}

ID3D11GeometryShader *ShaderExecutable11::getStreamOutShader() const
{
    return mStreamOutExecutable.get();
}

ID3D11ComputeShader *ShaderExecutable11::getComputeShader() const
{
    return mComputeExecutable.get();
}

UniformStorage11::UniformStorage11(size_t initialSize)
    : UniformStorageD3D(initialSize), mConstantBuffer()
{
}

UniformStorage11::~UniformStorage11()
{
}

gl::Error UniformStorage11::getConstantBuffer(Renderer11 *renderer, const d3d11::Buffer **bufferOut)
{
    if (size() > 0 && !mConstantBuffer.valid())
    {
        D3D11_BUFFER_DESC constantBufferDescription = {0};

        constantBufferDescription.ByteWidth           = static_cast<unsigned int>(size());
        constantBufferDescription.Usage = D3D11_USAGE_DYNAMIC;
        constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
        constantBufferDescription.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
        constantBufferDescription.MiscFlags = 0;
        constantBufferDescription.StructureByteStride = 0;

        ANGLE_TRY(renderer->allocateResource(constantBufferDescription, &mConstantBuffer));
    }

    *bufferOut = &mConstantBuffer;
    return gl::NoError();
}

}  // namespace rx
