bool Texture::Create(UINT w, UINT h, const void* bits) { bool ret = false; mWidth = w; mHeight = h; ID3D11Device* device = gCore.GetDevice(); CHECK(device); D3D11_TEXTURE2D_DESC textureDesc; ZeroMemory(&textureDesc, sizeof(textureDesc)); textureDesc.Width = w; textureDesc.Height = h; textureDesc.MipLevels = 1; textureDesc.ArraySize = 1; textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; textureDesc.SampleDesc.Count = 1; textureDesc.Usage = D3D11_USAGE_DEFAULT; textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE; textureDesc.CPUAccessFlags = 0; textureDesc.MiscFlags = 0; if (bits) { D3D11_SUBRESOURCE_DATA subres; subres.pSysMem = bits; subres.SysMemPitch = w * 4; subres.SysMemSlicePitch = 0; // Not needed since this is a 2d texture CHECK(SUCCEEDED(device->CreateTexture2D(&textureDesc, &subres, &mPtr))); } else { CHECK(SUCCEEDED(device->CreateTexture2D(&textureDesc, nullptr, &mPtr))); } D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc; shaderResourceViewDesc.Format = textureDesc.Format; shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; shaderResourceViewDesc.Texture2D.MostDetailedMip = 0; shaderResourceViewDesc.Texture2D.MipLevels = 1; CHECK(SUCCEEDED(device->CreateShaderResourceView(mPtr, &shaderResourceViewDesc, &mSRV))); D3D11_SAMPLER_DESC samplerDesc; ZeroMemory(&samplerDesc, sizeof(samplerDesc)); samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; samplerDesc.MinLOD = 0; samplerDesc.MaxLOD = D3D11_FLOAT32_MAX; CHECK(SUCCEEDED(device->CreateSamplerState(&samplerDesc, &mSampler))); ret = true; Exit0: return ret; }
void eve::dx11::SamplerStates::Init(eve::dx11::Device* device) { ID3D11Device* d = device->GetDevice(); D3D11_SAMPLER_DESC samplerDesc; ZeroMemory(&samplerDesc, sizeof(D3D11_SAMPLER_DESC)); D3D11_TEXTURE_ADDRESS_MODE addressMode = D3D11_TEXTURE_ADDRESS_MODE::D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.AddressU = addressMode; samplerDesc.AddressV = addressMode; samplerDesc.AddressW = addressMode; samplerDesc.ComparisonFunc = D3D11_COMPARISON_FUNC::D3D11_COMPARISON_ALWAYS; samplerDesc.Filter = D3D11_FILTER::D3D11_FILTER_COMPARISON_MIN_MAG_MIP_POINT; samplerDesc.MaxAnisotropy = 0; samplerDesc.MaxLOD = FLT_MAX; samplerDesc.MinLOD = FLT_MIN; samplerDesc.MipLODBias = 0; d->CreateSamplerState(&samplerDesc, &this->m_pPointClamp); samplerDesc.Filter = D3D11_FILTER::D3D11_FILTER_COMPARISON_MIN_MAG_MIP_LINEAR; d->CreateSamplerState(&samplerDesc, &this->m_pLinearClamp); addressMode = D3D11_TEXTURE_ADDRESS_MODE::D3D11_TEXTURE_ADDRESS_WRAP; samplerDesc.AddressU = addressMode; samplerDesc.AddressV = addressMode; samplerDesc.AddressW = addressMode; d->CreateSamplerState(&samplerDesc, &this->m_pLinearWrap); }
EErrorCode::Type SamplerState::Initialize( void ) { ID3D11Device *pDevice = (ID3D11Device*)GraphicsDevice::GetInstance()->GetNakedDevice(); D3D11_SAMPLER_DESC desc; desc.Filter = DX11Mapping::ToDXFilter( m_desc.filter ); desc.AddressU = DX11Mapping::ToDXTextureAddressMode( m_desc.addressU ); desc.AddressV = DX11Mapping::ToDXTextureAddressMode( m_desc.addressV ); desc.AddressW = DX11Mapping::ToDXTextureAddressMode( m_desc.addressW ); desc.MipLODBias = m_desc.mipLODBias; desc.MaxAnisotropy = m_desc.maxAnisotropy; desc.ComparisonFunc = DX11Mapping::ToDXComparisonFunc( m_desc.comparisonFunc ); PSX_MemCopyPerByte( desc.BorderColor, m_desc.borderColor, sizeof(m_desc.borderColor) ); desc.MinLOD = m_desc.minLOD; desc.MaxLOD = m_desc.maxLOD; if ( pDevice->CreateSamplerState( &desc, &m_pSamplerState ) != S_OK ) { PSX_PushError( "Failed to create sampler state." ); return EErrorCode::GRAPHICS; } return EErrorCode::OKAY; }
void TgcDX11Effect::internalAddSampler(TgcEffectValues::Sampler sampler) { ID3D11Device* device = ((TgcDX11Renderer*)GuiController::Instance->renderer)->device; //Create a texture sampler state description. D3D11_SAMPLER_DESC samplerDesc; samplerDesc.Filter = this->getFilter(sampler.filter); samplerDesc.AddressU = this->getAddressMode(sampler.addressU); samplerDesc.AddressV = this->getAddressMode(sampler.addressV); samplerDesc.AddressW = this->getAddressMode(sampler.addressW); samplerDesc.MipLODBias = 0.0f; samplerDesc.MaxAnisotropy = 1; samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; samplerDesc.BorderColor[0] = sampler.borderColor.R; samplerDesc.BorderColor[1] = sampler.borderColor.G; samplerDesc.BorderColor[2] = sampler.borderColor.B; samplerDesc.BorderColor[3] = sampler.borderColor.A; samplerDesc.MinLOD = 0; samplerDesc.MaxLOD = D3D11_FLOAT32_MAX; //Create the texture sampler state. ID3D11SamplerState* samplerState = NULL; HRESULT result = device->CreateSamplerState(&samplerDesc, &samplerState); this->dxSampleStates[sampler.textureName] = samplerState; }
//------------------------------------------------------------------------- // @Create needed sampler states for the textures here //------------------------------------------------------------------------- bool TextureManager::createSamplerStates(const DeviceManager& deviceManager) { //Setup sampler state here D3D11_SAMPLER_DESC samplerStateDesc; samplerStateDesc.Filter = D3D11_FILTER_ANISOTROPIC; samplerStateDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; samplerStateDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; samplerStateDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; samplerStateDesc.MipLODBias = 0.0f; samplerStateDesc.MaxAnisotropy = 16; samplerStateDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; samplerStateDesc.BorderColor[0] = 0.0f; samplerStateDesc.BorderColor[1] = 0.0f; samplerStateDesc.BorderColor[2] = 0.0f; samplerStateDesc.BorderColor[3] = 0.0f; samplerStateDesc.MinLOD = -3.402823466e+38F; samplerStateDesc.MaxLOD = 3.402823466e+38F; ID3D11Device* device = deviceManager.getDevice(); HRESULT hr = device->CreateSamplerState(&samplerStateDesc, &m_samplerState); if (FAILED( hr ) ) { MSG_TRACE_CHANNEL("TEXTUREMANAGER", "Failed to create sampler state: 0x%x", hr ) return false; } return true; }
//----------------------------------------------------------------------------- void CPUTRenderStateBlockDX11::CreateNativeResources() { // Now, create the DX render state items ID3D11Device *pDevice = CPUT_DX11::GetDevice(); HRESULT hr; hr = pDevice->CreateBlendState( &mStateDesc.BlendDesc, &mpBlendState ); ASSERT( SUCCEEDED(hr), _L("Failed to create blend state.") ); hr = pDevice->CreateDepthStencilState( &mStateDesc.DepthStencilDesc, &mpDepthStencilState ); ASSERT( SUCCEEDED(hr), _L("Failed to create depth stencil state.") ); hr = pDevice->CreateRasterizerState( &mStateDesc.RasterizerDesc, &mpRasterizerState ); ASSERT( SUCCEEDED(hr), _L("Failed to create rasterizer state.") ); // TODO: how to map samplers to shaders? // Each type can have different samplers assigned (VS, PS, GS, etc.) // How does DX treat them? 16 unified? or 16 each? // For now, just read 16 samplers, and set to all stages for( UINT ii=0; ii<mNumSamplers; ii++ ) { hr = pDevice->CreateSamplerState( &mStateDesc.SamplerDesc[ii], &mpSamplerState[ii] ); ASSERT( SUCCEEDED(hr), _L("Failed to create sampler state.") ); } } // CPUTRenderStateBlockDX11::CreateDXResources()
/** * @brief Inciar samplers de texturas * @param D3D IN: Controlador de Direct3D */ void SamplerManager::InitSamplers(D3D11Manager *D3D) { // Datos de la API que necesitamos ID3D11Device *dev = D3D->dev; //Crear sampler por puntos D3D11_SAMPLER_DESC sd; sd.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; sd.AddressU = D3D11_TEXTURE_ADDRESS_MIRROR; sd.AddressV = D3D11_TEXTURE_ADDRESS_MIRROR; sd.AddressW = D3D11_TEXTURE_ADDRESS_MIRROR; sd.MipLODBias = 0; sd.MaxLOD = 0; sd.MinLOD = 0; sd.ComparisonFunc = D3D11_COMPARISON_NEVER; sd.MaxAnisotropy = 1; dev->CreateSamplerState(&sd, &pPointMirrorSampler); //Crear sampler lineal ZeroMemory(&sd, sizeof(sd)); sd.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; sd.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; sd.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; sd.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; sd.MipLODBias = 0; sd.MaxLOD = 0; sd.MinLOD = 0; sd.ComparisonFunc = D3D11_COMPARISON_NEVER; sd.MaxAnisotropy = 1; dev->CreateSamplerState(&sd, &pLineWrapSampler); // Crear sampler con comparación ZeroMemory(&sd, sizeof(sd)); sd.Filter = D3D11_FILTER_COMPARISON_MIN_MAG_MIP_LINEAR; sd.AddressU = D3D11_TEXTURE_ADDRESS_MIRROR; sd.AddressV = D3D11_TEXTURE_ADDRESS_MIRROR; sd.AddressW = D3D11_TEXTURE_ADDRESS_MIRROR; sd.MipLODBias = 0; sd.MaxLOD = 0; sd.MinLOD = 0; sd.MaxAnisotropy = 1; sd.ComparisonFunc = D3D11_COMPARISON_LESS; dev->CreateSamplerState(&sd, &pLineMirrorSamplerComp); }
void SwapChain11::initPassThroughResources() { ID3D11Device *device = mRenderer->getDevice(); ASSERT(device != NULL); // Make sure our resources are all not allocated, when we create ASSERT(mQuadVB == NULL && mPassThroughSampler == NULL); ASSERT(mPassThroughIL == NULL && mPassThroughVS == NULL && mPassThroughPS == NULL); D3D11_BUFFER_DESC vbDesc; vbDesc.ByteWidth = sizeof(d3d11::PositionTexCoordVertex) * 4; vbDesc.Usage = D3D11_USAGE_DYNAMIC; vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; vbDesc.MiscFlags = 0; vbDesc.StructureByteStride = 0; HRESULT result = device->CreateBuffer(&vbDesc, NULL, &mQuadVB); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mQuadVB, "Swap chain quad vertex buffer"); D3D11_SAMPLER_DESC samplerDesc; samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.MipLODBias = 0.0f; samplerDesc.MaxAnisotropy = 0; samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; samplerDesc.BorderColor[0] = 0.0f; samplerDesc.BorderColor[1] = 0.0f; samplerDesc.BorderColor[2] = 0.0f; samplerDesc.BorderColor[3] = 0.0f; samplerDesc.MinLOD = 0; samplerDesc.MaxLOD = D3D11_FLOAT32_MAX; result = device->CreateSamplerState(&samplerDesc, &mPassThroughSampler); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mPassThroughSampler, "Swap chain pass through sampler"); D3D11_INPUT_ELEMENT_DESC quadLayout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; result = device->CreateInputLayout(quadLayout, 2, g_VS_Passthrough, sizeof(g_VS_Passthrough), &mPassThroughIL); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mPassThroughIL, "Swap chain pass through layout"); result = device->CreateVertexShader(g_VS_Passthrough, sizeof(g_VS_Passthrough), NULL, &mPassThroughVS); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mPassThroughVS, "Swap chain pass through vertex shader"); result = device->CreatePixelShader(g_PS_PassthroughRGBA, sizeof(g_PS_PassthroughRGBA), NULL, &mPassThroughPS); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mPassThroughPS, "Swap chain pass through pixel shader"); }
vpResult vprSamplerStateDX11::init() { vprDeviceDX11* dx11Device = static_cast<vprDeviceDX11*>(m_device); ID3D11Device* nativeDevice = dx11Device->getNativeDevice(); D3D11_SAMPLER_DESC nativeDesc; ZeroMemory(&nativeDesc, sizeof(nativeDesc)); if (m_desc.m_minFilter == vprFilter::ANISOTROPIC || m_desc.m_magFilter == vprFilter::ANISOTROPIC || m_desc.m_mipFilter == vprFilter::ANISOTROPIC) { nativeDesc.Filter = m_desc.m_comparison ? D3D11_FILTER_COMPARISON_ANISOTROPIC : D3D11_FILTER_ANISOTROPIC; } else { int indx = 0; if (m_desc.m_minFilter == vprFilter::LINEAR) { indx |= 1; } if (m_desc.m_magFilter == vprFilter::LINEAR) { indx |= 2; } if (m_desc.m_mipFilter == vprFilter::LINEAR) { indx |= 4; } if (m_desc.m_comparison) { indx |= 8; } nativeDesc.Filter = filterToDX11[indx]; } nativeDesc.AddressU = textureAddressModeToDX11[m_desc.m_addressU]; nativeDesc.AddressV = textureAddressModeToDX11[m_desc.m_addressV]; nativeDesc.AddressW = textureAddressModeToDX11[m_desc.m_addressW]; nativeDesc.MipLODBias = m_desc.m_mipLODBias; nativeDesc.MaxAnisotropy = m_desc.m_maxAnisotropy; nativeDesc.ComparisonFunc = comparisonFuncToDX11[m_desc.m_comparisonFunc]; nativeDesc.BorderColor[0] = m_desc.m_borderColor[0]; nativeDesc.BorderColor[1] = m_desc.m_borderColor[1]; nativeDesc.BorderColor[2] = m_desc.m_borderColor[2]; nativeDesc.BorderColor[3] = m_desc.m_borderColor[3]; nativeDesc.MinLOD = m_desc.m_minLOD; nativeDesc.MaxLOD = m_desc.m_maxLOD; if (FAILED(nativeDevice->CreateSamplerState(&nativeDesc, &m_nativeState))) { return VP_FAILURE; } return VP_SUCCESS; }
void SpriteShader::CreateBuffers() { D3D11_BUFFER_DESC mbd; mbd.Usage = D3D11_USAGE_DYNAMIC; mbd.ByteWidth = sizeof(XMMATRIX); mbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER; mbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; mbd.MiscFlags = 0; mbd.StructureByteStride = 0; ID3D11Device* dev = (ID3D11Device*)EngineCore::GetGraphicsAPI()->GetDevice(); dev->CreateBuffer( &mbd, NULL, &gpu_vs_buffer0 ); dev->CreateBuffer( &mbd, NULL, &gpu_vs_buffer1 ); D3D11_SAMPLER_DESC sd; sd.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; sd.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; sd.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; sd.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; sd.MipLODBias = 0.0f; sd.MaxAnisotropy = 1; sd.ComparisonFunc = D3D11_COMPARISON_ALWAYS; sd.BorderColor[0] = 0; sd.BorderColor[1] = 0; sd.BorderColor[2] = 0; sd.BorderColor[3] = 0; sd.MinLOD = 0; sd.MaxLOD = D3D11_FLOAT32_MAX; dev->CreateSamplerState( &sd, &samplerState ); }
option<skybox_texture> load_skybox(const wchar_t * filename, ID3D11Device& device) { skybox_texture sky; auto result = DirectX::CreateDDSTextureFromFile(&device, filename, nullptr, &sky.resource_view); if (result != S_OK) { return None<skybox_texture>(); } D3D11_SAMPLER_DESC builder; ZeroMemory(&builder, sizeof(builder)); builder.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; builder.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; builder.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; builder.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; builder.MaxLOD = D3D11_FLOAT32_MAX; result = device.CreateSamplerState(&builder, &sky.state); // Create a rasterizer state for the sky box D3D11_RASTERIZER_DESC rastDesc; ZeroMemory(&rastDesc, sizeof(rastDesc)); rastDesc.FillMode = D3D11_FILL_SOLID; rastDesc.CullMode = D3D11_CULL_FRONT; rastDesc.DepthClipEnable = true; device.CreateRasterizerState(&rastDesc, &sky.rasterizer_state); // A depth state for the sky rendering D3D11_DEPTH_STENCIL_DESC dsDesc; ZeroMemory(&dsDesc, sizeof(dsDesc)); dsDesc.DepthEnable = true; dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; dsDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL; device.CreateDepthStencilState(&dsDesc, &sky.depth_state); if (result != S_OK) { return None<skybox_texture>(); } return Some(std::move(sky)); }
void initAll(ID3D11Device& device, PipelineStates& pipelineStates) { assert(pipelineStates.mLinearClampSS == nullptr); assert(pipelineStates.mWireframeRS == nullptr); // // Linear clamp sampler state // D3D11_SAMPLER_DESC samplerDesc; samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.MipLODBias = 0; samplerDesc.MaxAnisotropy = 16; samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; samplerDesc.BorderColor[0] = 0.0f; samplerDesc.BorderColor[1] = 0.0f; samplerDesc.BorderColor[2] = 0.0f; samplerDesc.BorderColor[3] = 0.0f; samplerDesc.MipLODBias = 0.0f; // FLT_MIN samplerDesc.MinLOD = 0.0f; // FLT_MIN samplerDesc.MaxLOD = D3D11_FLOAT32_MAX; // FLT_MAX HRESULT result = device.CreateSamplerState(&samplerDesc, &pipelineStates.mLinearClampSS); DxErrorChecker(result); // // Wireframe rasterizer state // D3D11_RASTERIZER_DESC wireframeDesc; ZeroMemory(&wireframeDesc, sizeof(D3D11_RASTERIZER_DESC)); wireframeDesc.FillMode = D3D11_FILL_WIREFRAME; wireframeDesc.CullMode = D3D11_CULL_BACK; wireframeDesc.FrontCounterClockwise = false; wireframeDesc.DepthClipEnable = true; result = device.CreateRasterizerState(&wireframeDesc, &pipelineStates.mWireframeRS); DxErrorChecker(result); }
option<texture> load_texture_from_file(const wchar_t * filename, ID3D11Device& device) { texture ret; auto result = DirectX::CreateWICTextureFromFile(&device, filename, nullptr, &ret.resource_view); if (result != S_OK) { return None<texture>(); } D3D11_SAMPLER_DESC builder; ZeroMemory(&builder, sizeof(builder)); builder.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; builder.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; builder.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; builder.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; builder.MaxLOD = D3D11_FLOAT32_MAX; result = device.CreateSamplerState(&builder, &ret.state); if (result != S_OK) { return None<texture>(); } return Some(std::move(ret)); }
void BaseShader::Initialize(std::string vsFile, std::string psFile) { if (m_bInit) { LogManager::GetInstance().Warning("You've already initialized this Shader (%p)", this); return; } HRESULT result; ID3D10Blob* errorMessage = nullptr; ID3D10Blob* vertexShaderBuffer = nullptr; ID3D10Blob* pixelShaderBuffer = nullptr; ID3D11Device* device = Application::GetInstance().GetGraphicsDevice()->GetDXSystem()->GetDevice(); result = D3DX11CompileFromFile(vsFile.c_str(), nullptr, nullptr, "main", "vs_4_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, nullptr, &vertexShaderBuffer, &errorMessage, nullptr); if (FAILED(result)) { if (errorMessage) { LogManager::GetInstance().Trace("[HLSL] %s", (const char*)errorMessage->GetBufferPointer()); } else { LogManager::GetInstance().Warning("InitializeShader missing shader file (%s)", vsFile); } return; } result = D3DX11CompileFromFile(psFile.c_str(), nullptr, nullptr, "main", "ps_4_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, nullptr, &pixelShaderBuffer, &errorMessage, nullptr); if (FAILED(result)) { if (errorMessage) { LogManager::GetInstance().Trace("[HLSL] %s", (const char*)errorMessage->GetBufferPointer()); } else { LogManager::GetInstance().Warning("InitializeShader missing shader file (%s)", psFile); } return; } result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), nullptr, &m_vertexShader); if (FAILED(result)) { LogManager::GetInstance().Error("InitializeShader could not create vertex shader"); return; } result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), nullptr, &m_pixelShader); if (FAILED(result)) { LogManager::GetInstance().Error("InitializeShader could not create pixel shader"); return; } std::vector<D3D11_INPUT_ELEMENT_DESC> polygonLayout; ID3D11ShaderReflection* vertexShaderReflection = nullptr; ID3D11ShaderReflection* pixelShaderReflection = nullptr; result = D3DReflect(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), IID_ID3D11ShaderReflection, (void**)&vertexShaderReflection); if (FAILED(result)) { LogManager::GetInstance().Error("BaseShader: Could not peek into the VertexShader"); return; } result = D3DReflect(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), IID_ID3D11ShaderReflection, (void**)&pixelShaderReflection); if (FAILED(result)) { LogManager::GetInstance().Error("BaseShader: Could not peek into the PixelShader"); return; } D3D11_SHADER_DESC pixelDesc; pixelShaderReflection->GetDesc(&pixelDesc); for (unsigned int i = 0; i < pixelDesc.BoundResources; i++) { D3D11_SHADER_INPUT_BIND_DESC inputDesc; pixelShaderReflection->GetResourceBindingDesc(i, &inputDesc); //BindCount is the size of the array if (inputDesc.Type == D3D_SIT_TEXTURE) { m_supportedTextures[inputDesc.BindPoint] = true; } } D3D11_SHADER_DESC shaderDesc; vertexShaderReflection->GetDesc(&shaderDesc); for (unsigned int i = 0; i < shaderDesc.InputParameters; i++) { D3D11_SIGNATURE_PARAMETER_DESC paramDesc; vertexShaderReflection->GetInputParameterDesc(i, ¶mDesc); D3D11_INPUT_ELEMENT_DESC elementDesc; elementDesc.SemanticName = paramDesc.SemanticName; elementDesc.SemanticIndex = paramDesc.SemanticIndex; elementDesc.InputSlot = 0; elementDesc.AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; elementDesc.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; elementDesc.InstanceDataStepRate = 0; if (paramDesc.Mask == 1) { switch (paramDesc.ComponentType) { case D3D_REGISTER_COMPONENT_UINT32: elementDesc.Format = DXGI_FORMAT_R32_UINT; break; case D3D_REGISTER_COMPONENT_SINT32: elementDesc.Format = DXGI_FORMAT_R32_SINT; break; case D3D_REGISTER_COMPONENT_FLOAT32: elementDesc.Format = DXGI_FORMAT_R32_FLOAT; break; default: LogManager::GetInstance().Error("Shader: Unknown RegisterComponentType, Mask = 1"); return; } } else if (paramDesc.Mask <= 3) { switch (paramDesc.ComponentType) { case D3D_REGISTER_COMPONENT_UINT32: elementDesc.Format = DXGI_FORMAT_R32G32_UINT; break; case D3D_REGISTER_COMPONENT_SINT32: elementDesc.Format = DXGI_FORMAT_R32G32_SINT; break; case D3D_REGISTER_COMPONENT_FLOAT32: elementDesc.Format = DXGI_FORMAT_R32G32_FLOAT; break; default: LogManager::GetInstance().Error("Shader: Unknown RegisterComponentType, Mask <= 3"); return; } } else if (paramDesc.Mask <= 7) { switch (paramDesc.ComponentType) { case D3D_REGISTER_COMPONENT_UINT32: elementDesc.Format = DXGI_FORMAT_R32G32B32_UINT; break; case D3D_REGISTER_COMPONENT_SINT32: elementDesc.Format = DXGI_FORMAT_R32G32B32_SINT; break; case D3D_REGISTER_COMPONENT_FLOAT32: elementDesc.Format = DXGI_FORMAT_R32G32B32_FLOAT; break; default: LogManager::GetInstance().Error("Shader: Unknown RegisterComponentType, Mask <= 7"); return; } } else if (paramDesc.Mask <= 15) { switch (paramDesc.ComponentType) { case D3D_REGISTER_COMPONENT_UINT32: elementDesc.Format = DXGI_FORMAT_R32G32B32A32_UINT; break; case D3D_REGISTER_COMPONENT_SINT32: elementDesc.Format = DXGI_FORMAT_R32G32B32A32_SINT; break; case D3D_REGISTER_COMPONENT_FLOAT32: elementDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT; break; default: LogManager::GetInstance().Error("Shader: Unknown RegisterComponentType, Mask <= 15"); return; } } polygonLayout.push_back(elementDesc); } result = device->CreateInputLayout(&polygonLayout[0], polygonLayout.size(), vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &m_layout); if (FAILED(result)) { LogManager::GetInstance().Error("Shader: Failed to create the input layout"); return; } vertexShaderReflection->Release(); vertexShaderReflection = nullptr; pixelShaderReflection->Release(); pixelShaderReflection = nullptr; D3D11_SAMPLER_DESC samplerDesc; samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; samplerDesc.MipLODBias = 0.0f; samplerDesc.MaxAnisotropy = 1; samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; samplerDesc.BorderColor[0] = 0; samplerDesc.BorderColor[1] = 0; samplerDesc.BorderColor[2] = 0; samplerDesc.BorderColor[3] = 0; samplerDesc.MinLOD = 0; samplerDesc.MaxLOD = D3D11_FLOAT32_MAX; result = device->CreateSamplerState(&samplerDesc, &m_sampleState); if (FAILED(result)) { LogManager::GetInstance().Error("InitializeShader could not create the texture sampler state"); return; } m_bInit = true; }
/////////////////////////////////////////////////////////////////////////////////////////////////// /// CelShadeApp::CelShadeD3D /// /// @brief /// Render a cel-shading demo using Direct3D /// @return /// N/A /////////////////////////////////////////////////////////////////////////////////////////////////// void CelShadeApp::CelShadeD3D() { ID3D11DeviceContext* pContext = m_pDxData->pD3D11Context; ID3D11Device* pDevice = m_pDxData->pD3D11Device; D3DX11_IMAGE_LOAD_INFO imageLoadInfo; memset( &imageLoadInfo, 0, sizeof(D3DX11_IMAGE_LOAD_INFO) ); imageLoadInfo.BindFlags = D3D11_BIND_SHADER_RESOURCE; imageLoadInfo.Format = DXGI_FORMAT_R8G8B8A8_UNORM; DxTextureCreateInfo shadeTexInfo; memset(&shadeTexInfo, 0, sizeof(DxTextureCreateInfo)); shadeTexInfo.flags.RenderTarget = TRUE; shadeTexInfo.flags.ShaderInput = TRUE; shadeTexInfo.format = DXGI_FORMAT_R8G8B8A8_UNORM; shadeTexInfo.width = m_screenWidth; shadeTexInfo.height = m_screenHeight; DxTexture* pShadeTex = DxTexture::Create(pDevice, &shadeTexInfo); DxTextureCreateInfo edgeTexInfo; memset(&edgeTexInfo, 0, sizeof(DxTextureCreateInfo)); edgeTexInfo.flags.RenderTarget = TRUE; edgeTexInfo.flags.ShaderInput = TRUE; edgeTexInfo.format = DXGI_FORMAT_R8G8B8A8_UNORM; edgeTexInfo.width = m_screenWidth; edgeTexInfo.height = m_screenHeight; DxTexture* pEdgeTex = DxTexture::Create(pDevice, &edgeTexInfo); // Samplers ///////////////////////////////////////////////////////////////////////////// // SamplerState PointSampler : register(s0); D3D11_SAMPLER_DESC pointSamplerDesc; memset(&pointSamplerDesc, 0, sizeof(D3D11_SAMPLER_DESC)); pointSamplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; pointSamplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; pointSamplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; pointSamplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; pointSamplerDesc.MinLOD = -FLT_MAX; pointSamplerDesc.MaxLOD = FLT_MAX; pointSamplerDesc.MipLODBias = 0.0f; pointSamplerDesc.MaxAnisotropy = 16; ID3D11SamplerState* pPointSampler = NULL; pDevice->CreateSamplerState(&pointSamplerDesc, &pPointSampler); // UINT numVertices = 0, numIndices = 0; VertexPTN* pVB = NULL; UINT* pIB = NULL; ImportPly("Content/dragon_vrip_res3.ply", numVertices, &pVB, numIndices, &pIB); //ImportPly("Content/bun_zipper_res4.ply", numVertices, &pVB, numIndices, &pIB); DxMeshCreateInfo meshCreateInfo = {0}; meshCreateInfo.indexCount = numIndices; meshCreateInfo.pIndexArray = pIB; meshCreateInfo.indexFormat = DXGI_FORMAT_R32_UINT; meshCreateInfo.pVertexArray = pVB; meshCreateInfo.vertexCount = numVertices; meshCreateInfo.vertexElementSize = sizeof(VertexPTN); DxMesh* pMesh = DxMesh::Create(pDevice, &meshCreateInfo); Plane p; DxMeshCreateInfo planeMeshInfo; memset(&planeMeshInfo, 0, sizeof(planeMeshInfo)); planeMeshInfo.pVertexArray = p.GetVB(); planeMeshInfo.vertexCount = p.NumVertices(); planeMeshInfo.vertexElementSize = sizeof(VertexPTN); DxMesh* pPlaneMesh = DxMesh::Create(pDevice, &planeMeshInfo); Cube c; DxMeshCreateInfo cubeMeshInfo; memset(&cubeMeshInfo, 0, sizeof(cubeMeshInfo)); cubeMeshInfo.pVertexArray = c.GetVB(); cubeMeshInfo.vertexCount = c.NumVertices(); cubeMeshInfo.vertexElementSize = sizeof(VertexPT); DxMesh* pCubeMesh = DxMesh::Create(pDevice, &cubeMeshInfo); D3D11_SUBRESOURCE_DATA cbInitData; memset(&cbInitData, 0, sizeof(D3D11_SUBRESOURCE_DATA)); // Camera Buffer CameraBufferData cameraData; memset(&cameraData, 0, sizeof(CameraBufferData)); DxBufferCreateInfo cameraBufferCreateInfo = {0}; cameraBufferCreateInfo.flags.cpuWriteable = TRUE; cameraBufferCreateInfo.elemSizeBytes = sizeof(CameraBufferData); cameraBufferCreateInfo.pInitialData = &cameraData; DxBuffer* pCameraBuffer = DxBuffer::Create(pDevice, &cameraBufferCreateInfo); // Shaders //////////////////////////////////////////////////////////////////////////////////// m_pPosTexTriVS = DxShader::CreateFromFile(pDevice, "PosTexTri", "Content/shaders/CelShade.hlsl", PosTexVertexDesc, PosTexElements); m_pPosTexNormVS = DxShader::CreateFromFile(pDevice, "PosTexNorm", "Content/shaders/CelShade.hlsl", PosTexNormVertexDesc, PosTexNormElements); m_pCelShadePS = DxShader::CreateFromFile(pDevice, "CelShade", "Content/shaders/CelShade.hlsl"); DxShader* pDetectEdges = DxShader::CreateFromFile(pDevice, "DetectEdges", "Content/shaders/CelShade.hlsl"); DxShader* pApplyTexPS = DxShader::CreateFromFile(pDevice, "ApplyTex", "Content/shaders/CelShade.hlsl"); DxShader* pCubeVS = DxShader::CreateFromFile(pDevice, "PosTex", "Content/shaders/CelShade.hlsl", PosTexVertexDesc, PosTexElements); DxShader* pCubePS = DxShader::CreateFromFile(pDevice, "CubePS", "Content/shaders/CelShade.hlsl"); //////////////////////////////////////////////////////////////////////////////////////// pContext->ClearState(); // SET RENDER STATE FLOAT clearColor[4]; clearColor[0] = 0.2f; clearColor[1] = 0.2f; clearColor[2] = 0.2f; clearColor[3] = 1.0f; D3D11_RASTERIZER_DESC shadeDesc; shadeDesc.FillMode = D3D11_FILL_SOLID; shadeDesc.CullMode = D3D11_CULL_BACK; shadeDesc.FrontCounterClockwise = FALSE; shadeDesc.DepthBias = 0; shadeDesc.DepthBiasClamp = 0.0f; shadeDesc.SlopeScaledDepthBias = 0; shadeDesc.DepthClipEnable = false; shadeDesc.ScissorEnable = false; shadeDesc.MultisampleEnable = false; shadeDesc.AntialiasedLineEnable = false; ID3D11RasterizerState* pShadeRS = NULL; pDevice->CreateRasterizerState(&shadeDesc, &pShadeRS); pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); m_pWindow->Show(); BOOL quit = false; FLOAT yRotationAngle = 0.0f; while (!quit) { ProcessUpdates(); BeginFrame(); CameraBufferData* pCameraData = NULL; // new frame, clear state pContext->ClearState(); pContext->RSSetViewports(1, &m_pDxData->viewport); pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); pContext->RSSetState(pShadeRS); pContext->PSSetSamplers(0, 1, &pPointSampler); pContext->OMSetRenderTargets(1, &m_pDxData->pAppRenderTargetView, m_pDxData->pAppDepthStencilTex->GetDepthStencilView()); pContext->ClearRenderTargetView(m_pDxData->pAppRenderTargetView, clearColor); pContext->ClearDepthStencilView(m_pDxData->pAppDepthStencilTex->GetDepthStencilView(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0, 0); ///// Draw Mesh /////////////////////////////////////////////////////////////////////////// FLOAT viewRotationY = (GetMousePos().x - (m_screenWidth / 2.0f)) /(m_screenWidth / 2.0f); viewRotationY *= (3.14159f / 4.0f); FLOAT viewRotationZ = (GetMousePos().y - (m_screenHeight / 2.0f)) /(m_screenHeight / 2.0f); viewRotationZ *= (3.14159f / 4.0f); pCameraData = reinterpret_cast<CameraBufferData*>(pCameraBuffer->Map(pContext)); pCameraData->worldMatrix = XMMatrixScaling(25, 25, 25) * XMMatrixRotationY(yRotationAngle) * XMMatrixTranslation(m_pCamera->Position().x, m_pCamera->Position().y, m_pCamera->Position().z) ; // translate world +6 in Z to position camera -9 from world origin pCameraData->viewMatrix = m_pCamera->W2C(); pCameraData->projectionMatrix = m_pCamera->C2S(); pCameraBuffer->Unmap(pContext); pCameraBuffer->BindVS(pContext, 0); pMesh->Bind(pContext); m_pPosTexNormVS->Bind(pContext); m_pCelShadePS->Bind(pContext); pMesh->Draw(pContext); ///// Detect Edges /////////////////////////////////////////////////////////////////////////// ///// Draw Light Position //////////////////////////////////////////////////////////////////// //yRotationAngle = 0; pCameraData = reinterpret_cast<CameraBufferData*>(pCameraBuffer->Map(pContext)); pCameraData->worldMatrix = XMMatrixScaling(1, 1, 1); // XMMatrixRotationY(yRotationAngle); // XMMatrixTranslation(-10, 10, 10); // translate world +6 in Z to position camera -9 from world origin pCameraData->viewMatrix = XMMatrixTranslation(0, 0, 10) * m_pCamera->W2C(); pCameraData->projectionMatrix = m_pCamera->C2S(); pCameraBuffer->Unmap(pContext); pCameraBuffer->BindVS(pContext, 0); pCubeVS->Bind(pContext); pCubePS->Bind(pContext); pCubeMesh->Bind(pContext); pCubeMesh->Draw(pContext); ///// Draw UI //////////////////////////////////////////////////////////////////////////////// ///@todo Consider moving the following UI drawing to Draw2D() m_pUI->Begin(); // Draw UI stuff m_pUI->RenderRect(); m_pUI->RenderText(); m_pUI->End(); /// Blend UI onto final image DrawUI(); m_pDxData->pDXGISwapChain->Present(0,0); EndFrame(); Sleep(50); yRotationAngle += 3.14159f / 60.0f; } // Shader Resource Views pCameraBuffer->Destroy(); // Shaders m_pCelShadePS->Destroy(); m_pCelShadePS = NULL; m_pPosTexTriVS->Destroy(); m_pPosTexTriVS = NULL; m_pPosTexNormVS->Destroy(); m_pPosTexNormVS = NULL; pApplyTexPS->Destroy(); pApplyTexPS = NULL; pPlaneMesh->Destroy(); pPlaneMesh = NULL; // Samplers pPointSampler->Release(); // Rasterizer State pShadeRS->Release(); m_pDxData->pD3D11Context->ClearState(); m_pDxData->pD3D11Context->Flush(); }
bool MultiTextureShader::InitializeShader(Graphics* graphics,std::wstring vertexShaderFilename,std::wstring pixelShaderFilename) { // create a little structure to handle // shader buffer and size struct ShaderBuffer { unsigned int Size; void* Buffer; }; ShaderBuffer vertexBuffer, pixelBuffer; D3D11_INPUT_ELEMENT_DESC polygonLayout[2]; unsigned int numElements; D3D11_SAMPLER_DESC samplerDesc; ID3D11Device* device = graphics->GetDevice(); memset(&vertexBuffer,0,sizeof(ShaderBuffer)); memset(&pixelBuffer,0,sizeof(ShaderBuffer)); // load the compiled shaders vertexBuffer.Buffer = LoadCompiledShader(vertexShaderFilename, vertexBuffer.Size); pixelBuffer.Buffer = LoadCompiledShader(pixelShaderFilename, pixelBuffer.Size); // set the shaders if(FAILED(device->CreateVertexShader(vertexBuffer.Buffer,vertexBuffer.Size,nullptr,&m_vertexShader))) return false; if(FAILED(device->CreatePixelShader(pixelBuffer.Buffer,pixelBuffer.Size,nullptr,&m_pixelShader))) return false; // create the vertex input layout polygonLayout[0].SemanticName = "POSITION"; polygonLayout[0].SemanticIndex = 0; polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT; polygonLayout[0].InputSlot = 0; polygonLayout[0].AlignedByteOffset = 0; polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; polygonLayout[0].InstanceDataStepRate = 0; polygonLayout[1].SemanticName = "TEXCOORD"; polygonLayout[1].SemanticIndex = 0; polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT; polygonLayout[1].InputSlot = 0; polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; polygonLayout[1].InstanceDataStepRate = 0; // get a count of the elements in the layout numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]); // create the vertex input layout if(FAILED(device->CreateInputLayout(polygonLayout,numElements,vertexBuffer.Buffer, vertexBuffer.Size,&m_layout))) return false; // released the vertex shader buffer and pixel shader buffers // since they are no longer needed delete vertexBuffer.Buffer; delete pixelBuffer.Buffer; // initialize our constant buffer m_matrixBuffer.Initialize(device); // create a texture sampler state description samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; samplerDesc.MipLODBias = 0.0f; samplerDesc.MaxAnisotropy = 1; samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; //memset(&samplerDesc.BorderColor,0,sizeof(FLOAT)*4); samplerDesc.BorderColor[0] = 0.0f; samplerDesc.BorderColor[1] = 0.0f; samplerDesc.BorderColor[2] = 0.0f; samplerDesc.BorderColor[3] = 0.0f; samplerDesc.MinLOD = 0; samplerDesc.MaxLOD = D3D11_FLOAT32_MAX; // create the texture sampler if(FAILED(device->CreateSamplerState(&samplerDesc,&m_sampleState))) return false; return true; }
void SwapChain11::initPassThroughResources() { if (mPassThroughResourcesInit) { return; } TRACE_EVENT0("gpu.angle", "SwapChain11::initPassThroughResources"); ID3D11Device *device = mRenderer->getDevice(); ASSERT(device != NULL); // Make sure our resources are all not allocated, when we create ASSERT(mQuadVB == NULL && mPassThroughSampler == NULL); ASSERT(mPassThroughIL == NULL && mPassThroughVS == NULL && mPassThroughPS == NULL); D3D11_BUFFER_DESC vbDesc; vbDesc.ByteWidth = sizeof(d3d11::PositionTexCoordVertex) * 4; vbDesc.Usage = D3D11_USAGE_DYNAMIC; vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; vbDesc.MiscFlags = 0; vbDesc.StructureByteStride = 0; HRESULT result = device->CreateBuffer(&vbDesc, NULL, &mQuadVB); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mQuadVB, "Swap chain quad vertex buffer"); D3D11_SAMPLER_DESC samplerDesc; samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.MipLODBias = 0.0f; samplerDesc.MaxAnisotropy = 0; samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; samplerDesc.BorderColor[0] = 0.0f; samplerDesc.BorderColor[1] = 0.0f; samplerDesc.BorderColor[2] = 0.0f; samplerDesc.BorderColor[3] = 0.0f; samplerDesc.MinLOD = 0; samplerDesc.MaxLOD = D3D11_FLOAT32_MAX; result = device->CreateSamplerState(&samplerDesc, &mPassThroughSampler); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mPassThroughSampler, "Swap chain pass through sampler"); D3D11_INPUT_ELEMENT_DESC quadLayout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; result = device->CreateInputLayout(quadLayout, 2, g_VS_Passthrough2D, sizeof(g_VS_Passthrough2D), &mPassThroughIL); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mPassThroughIL, "Swap chain pass through layout"); result = device->CreateVertexShader(g_VS_Passthrough2D, sizeof(g_VS_Passthrough2D), NULL, &mPassThroughVS); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mPassThroughVS, "Swap chain pass through vertex shader"); result = device->CreatePixelShader(g_PS_PassthroughRGBA2D, sizeof(g_PS_PassthroughRGBA2D), NULL, &mPassThroughPS); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mPassThroughPS, "Swap chain pass through pixel shader"); // Use the default rasterizer state but without culling D3D11_RASTERIZER_DESC rasterizerDesc; rasterizerDesc.FillMode = D3D11_FILL_SOLID; rasterizerDesc.CullMode = D3D11_CULL_NONE; rasterizerDesc.FrontCounterClockwise = FALSE; rasterizerDesc.DepthBias = 0; rasterizerDesc.SlopeScaledDepthBias = 0.0f; rasterizerDesc.DepthBiasClamp = 0.0f; rasterizerDesc.DepthClipEnable = TRUE; rasterizerDesc.ScissorEnable = FALSE; rasterizerDesc.MultisampleEnable = FALSE; rasterizerDesc.AntialiasedLineEnable = FALSE; result = device->CreateRasterizerState(&rasterizerDesc, &mPassThroughRS); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mPassThroughRS, "Swap chain pass through rasterizer state"); mPassThroughResourcesInit = true; }
void D3D11hud::Init(int width, int height) { Hud::Init(width, height); ID3D11Device *device = NULL; _deviceContext->GetDevice(&device); // define font texture D3D11_TEXTURE2D_DESC texDesc; texDesc.Width = FONT_TEXTURE_WIDTH; texDesc.Height = FONT_TEXTURE_HEIGHT; texDesc.MipLevels = 1; texDesc.ArraySize = 1; texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; texDesc.SampleDesc.Count = 1; texDesc.SampleDesc.Quality = 0; texDesc.Usage = D3D11_USAGE_DEFAULT; texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE; texDesc.CPUAccessFlags = 0; texDesc.MiscFlags = 0; D3D11_SUBRESOURCE_DATA subData; subData.pSysMem = font_image; subData.SysMemPitch = FONT_TEXTURE_WIDTH*4; subData.SysMemSlicePitch = FONT_TEXTURE_WIDTH*FONT_TEXTURE_HEIGHT*4; HRESULT hr = device->CreateTexture2D(&texDesc, &subData, &_fontTexture); assert(_fontTexture); // shader resource view D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; ZeroMemory(&srvDesc, sizeof(srvDesc)); srvDesc.Format = texDesc.Format; srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; srvDesc.Texture2D.MostDetailedMip = 0; srvDesc.Texture2D.MipLevels = texDesc.MipLevels; device->CreateShaderResourceView(_fontTexture, &srvDesc, &_shaderResourceView); assert(_shaderResourceView); D3D11_SAMPLER_DESC samplerDesc; ZeroMemory(&samplerDesc, sizeof(samplerDesc)); samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; samplerDesc.AddressU = samplerDesc.AddressV = samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; samplerDesc.MaxAnisotropy = 1; samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; samplerDesc.MaxLOD = D3D11_FLOAT32_MAX; device->CreateSamplerState(&samplerDesc, &_samplerState); assert(_samplerState); ID3DBlob* pVSBlob; ID3DBlob* pPSBlob; pVSBlob = d3d11CompileShader(s_VS, "vs_main", "vs_4_0"); pPSBlob = d3d11CompileShader(s_PS, "ps_main", "ps_4_0"); assert(pVSBlob); assert(pPSBlob); D3D11_INPUT_ELEMENT_DESC inputElementDesc[] = { { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, sizeof(float)*2, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, sizeof(float)*5, D3D11_INPUT_PER_VERTEX_DATA, 0 } }; device->CreateInputLayout(inputElementDesc, ARRAYSIZE(inputElementDesc), pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), &_inputLayout); assert(_inputLayout); device->CreateVertexShader(pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, &_vertexShader); assert(_vertexShader); device->CreatePixelShader(pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &_pixelShader); assert(_pixelShader); D3D11_RASTERIZER_DESC rasDesc; rasDesc.FillMode = D3D11_FILL_SOLID; rasDesc.CullMode = D3D11_CULL_NONE; rasDesc.FrontCounterClockwise = FALSE; rasDesc.DepthBias = 0; rasDesc.DepthBiasClamp = 0; rasDesc.SlopeScaledDepthBias = 0.0f; rasDesc.DepthClipEnable = FALSE; rasDesc.ScissorEnable = FALSE; rasDesc.MultisampleEnable = FALSE; rasDesc.AntialiasedLineEnable = FALSE; device->CreateRasterizerState(&rasDesc, &_rasterizerState); assert(_rasterizerState); // constant buffer D3D11_BUFFER_DESC cbDesc; cbDesc.Usage = D3D11_USAGE_DYNAMIC; cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; cbDesc.MiscFlags = 0; cbDesc.ByteWidth = sizeof(CB_HUD_PROJECTION); device->CreateBuffer(&cbDesc, NULL, &_constantBuffer); assert(_constantBuffer); }
bool ShadowMappingShader::InitializeShader(const char * i_vsFileName, const char * i_psFileName) { HRESULT result; ID3D10Blob* errorMessage; ID3D10Blob* vertexShaderBuffer; ID3D10Blob* pixelShaderBuffer; D3D11_INPUT_ELEMENT_DESC polygonLayout[3]; unsigned int numElements; D3D11_SAMPLER_DESC samplerDesc; D3D11_BUFFER_DESC matrixBufferDesc; D3D11_BUFFER_DESC lightBufferDesc; D3D11_BUFFER_DESC lightBufferDesc2; // Initialize the pointers this function will use to null. errorMessage = 0; vertexShaderBuffer = 0; pixelShaderBuffer = 0; // Compile the vertex shader code. result = D3DX11CompileFromFile(i_vsFileName, NULL, NULL, "ShadowVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL, &vertexShaderBuffer, &errorMessage, NULL); if (FAILED(result)) { // If the shader failed to compile it should have writen something to the error message. if (errorMessage) { outputShaderErrorMessage(errorMessage, i_vsFileName); } // If there was nothing in the error message then it simply could not find the shader file itself. else { MessageBox(System::Window::GetWindwsHandle(), i_vsFileName, "Missing Shader File", MB_OK); } return false; } // Compile the pixel shader code. result = D3DX11CompileFromFile(i_psFileName, NULL, NULL, "ShadowPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, NULL, &pixelShaderBuffer, &errorMessage, NULL); if (FAILED(result)) { // If the shader failed to compile it should have writen something to the error message. if (errorMessage) { outputShaderErrorMessage(errorMessage, i_psFileName); } // If there was nothing in the error message then it simply could not find the file itself. else { MessageBox(System::Window::GetWindwsHandle(), i_psFileName, "Missing Shader File", MB_OK); } return false; } ID3D11Device* device = GraphicsDX::GetDevice(); // Create the vertex shader from the buffer. result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader); if (FAILED(result)) { return false; } // Create the pixel shader from the buffer. result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader); if (FAILED(result)) { return false; } // Create the vertex input layout description. polygonLayout[0].SemanticName = "POSITION"; polygonLayout[0].SemanticIndex = 0; polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT; polygonLayout[0].InputSlot = 0; polygonLayout[0].AlignedByteOffset = 0; polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; polygonLayout[0].InstanceDataStepRate = 0; polygonLayout[1].SemanticName = "TEXCOORD"; polygonLayout[1].SemanticIndex = 0; polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT; polygonLayout[1].InputSlot = 0; polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; polygonLayout[1].InstanceDataStepRate = 0; polygonLayout[2].SemanticName = "NORMAL"; polygonLayout[2].SemanticIndex = 0; polygonLayout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT; polygonLayout[2].InputSlot = 0; polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; polygonLayout[2].InstanceDataStepRate = 0; // Get a count of the elements in the layout. numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]); // Create the vertex input layout. result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &m_layout); if (FAILED(result)) { return false; } // Release the vertex shader buffer and pixel shader buffer since they are no longer needed. vertexShaderBuffer->Release(); vertexShaderBuffer = 0; pixelShaderBuffer->Release(); pixelShaderBuffer = 0; // Create a wrap texture sampler state description. samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; samplerDesc.MipLODBias = 0.0f; samplerDesc.MaxAnisotropy = 1; samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; samplerDesc.BorderColor[0] = 0; samplerDesc.BorderColor[1] = 0; samplerDesc.BorderColor[2] = 0; samplerDesc.BorderColor[3] = 0; samplerDesc.MinLOD = 0; samplerDesc.MaxLOD = D3D11_FLOAT32_MAX; // Create the texture sampler state. result = device->CreateSamplerState(&samplerDesc, &m_sampleStateWrap); if (FAILED(result)) { return false; } // Create a clamp texture sampler state description. samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; // Create the texture sampler state. result = device->CreateSamplerState(&samplerDesc, &m_sampleStateClamp); if (FAILED(result)) { return false; } // Setup the description of the dynamic matrix constant buffer that is in the vertex shader. matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC; matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType); matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; matrixBufferDesc.MiscFlags = 0; matrixBufferDesc.StructureByteStride = 0; // Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class. result = device->CreateBuffer(&matrixBufferDesc, NULL, &m_matrixBuffer); if (FAILED(result)) { return false; } // Setup the description of the light dynamic constant buffer that is in the pixel shader. lightBufferDesc.Usage = D3D11_USAGE_DYNAMIC; lightBufferDesc.ByteWidth = sizeof(LightBufferType); lightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; lightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; lightBufferDesc.MiscFlags = 0; lightBufferDesc.StructureByteStride = 0; // Create the constant buffer pointer so we can access the pixel shader constant buffer from within this class. result = device->CreateBuffer(&lightBufferDesc, NULL, &m_lightBuffer); if (FAILED(result)) { return false; } // Setup the description of the light dynamic constant buffer that is in the vertex shader. lightBufferDesc2.Usage = D3D11_USAGE_DYNAMIC; lightBufferDesc2.ByteWidth = sizeof(LightBufferType2); lightBufferDesc2.BindFlags = D3D11_BIND_CONSTANT_BUFFER; lightBufferDesc2.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; lightBufferDesc2.MiscFlags = 0; lightBufferDesc2.StructureByteStride = 0; // Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class. result = device->CreateBuffer(&lightBufferDesc2, NULL, &m_lightBuffer2); if (FAILED(result)) { return false; } return true; }