bool LightShaderClass::InitShader(ID3D11Device* pDevice, WCHAR* vFileName, WCHAR* pFileName, WCHAR* gFileName) { bool result; D3D11_INPUT_ELEMENT_DESC vertexDesc[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "BLENDWEIGHT", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 32, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "BLENDINDICES", 0, DXGI_FORMAT_R32G32B32A32_UINT, 0, 48, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "BLENDINDICES", 1, DXGI_FORMAT_R32_UINT, 0, 64, D3D11_INPUT_PER_VERTEX_DATA, 0 } }; // Get a count of the elements in the layout. int numElements = sizeof(vertexDesc) / sizeof(vertexDesc[0]); result = ShaderClass::InitShader(pDevice, vFileName, pFileName, gFileName, vertexDesc, numElements); if (!result) { return false; } result = createSamplerState(pDevice, &mSampleState); if (!result) { return false; } result = createConstantBuffer(pDevice, sizeof(XMFLOAT4) * 3 + sizeof(PointLight)*MAX_ACTIVE_LIGHTS + sizeof(MatrialDescPadded)*MAX_MATERIAL_COUNT, &mLightBuffer, D3D11_BIND_CONSTANT_BUFFER); if (!result) { return false; } result = createConstantBuffer(pDevice, sizeof(XMFLOAT4X4)* 2 + sizeof(XMFLOAT4X4)*MAX_BONE_COUNT, &mBoneTBuffer, D3D11_BIND_CONSTANT_BUFFER); if (!result) { return false; } result = createGeometryShader(pDevice, L"data/shaders/AnimatedLightGeometryShader.hlsl", "GSMain", &mAniGS); if (!result) { return false; } return true; }
bool TerrainShaderClass::InitShader(ID3D11Device* pDevice, WCHAR* vFileName, WCHAR* pFileName, WCHAR* gFileName) { bool result; D3D11_INPUT_ELEMENT_DESC polygonLayout[6];/* = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 1, DXGI_FORMAT_R32G32_FLOAT, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0 } };*/ // Create the vertex input layout description. // This setup needs to match the VertexType stucture in the ModelClass and in the shader. 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; polygonLayout[3].SemanticName = "TEXCOORD"; polygonLayout[3].SemanticIndex = 1; polygonLayout[3].Format = DXGI_FORMAT_R32G32_FLOAT; polygonLayout[3].InputSlot = 0; polygonLayout[3].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; polygonLayout[3].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; polygonLayout[3].InstanceDataStepRate = 0; polygonLayout[4].SemanticName = "TANGENT"; polygonLayout[4].SemanticIndex = 0; polygonLayout[4].Format = DXGI_FORMAT_R32G32B32_FLOAT; polygonLayout[4].InputSlot = 0; polygonLayout[4].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; polygonLayout[4].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; polygonLayout[4].InstanceDataStepRate = 0; polygonLayout[5].SemanticName = "BINORMAL"; polygonLayout[5].SemanticIndex = 0; polygonLayout[5].Format = DXGI_FORMAT_R32G32B32_FLOAT; polygonLayout[5].InputSlot = 0; polygonLayout[5].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; polygonLayout[5].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; polygonLayout[5].InstanceDataStepRate = 0; // Get a count of the elements in the layout. int numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]); result = ShaderClass::InitShader(pDevice, vFileName, pFileName, gFileName, polygonLayout, numElements); if (!result) { return false; } result = createSamplerState(pDevice, &mSampleState); if (!result) { return false; } // Deferred init result = createConstantBuffer(pDevice, sizeof(TerrainCBufferType), &mLightBuffer, D3D11_BIND_CONSTANT_BUFFER); if (!result) { return false; } result = createPixelShader(pDevice, L"data/shaders/DeferredTerrainPixelShader.hlsl", "PSMain", &mDeferredPS); if (!result) { return false; } // Shadow Init result = createConstantBuffer(pDevice, sizeof(ShadowBuffer), &mShadowBuffer, D3D11_BIND_CONSTANT_BUFFER); if (!result) { return false; } result = createVertexShader(pDevice, L"data/shaders/ShadowDeferredVS.hlsl", "VSMain", &mShadowDeferredVS); if (!result) { return false; } result = createGeometryShader(pDevice, L"data/shaders/ShadowDeferredGS.hlsl", "GSMain", &mShadowDeferredGS); if (!result) return false; result = createPixelShader(pDevice, L"data/shaders/ShadowDeferredPS.hlsl", "PSMain", &mShadowDeferredPS); if (!result) { return false; } D3D11_SAMPLER_DESC samplerDesc; // Create a texture sampler state description. samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;//D3D11_FILTER_ANISOTROPIC; samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_BORDER; samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_BORDER; samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_BORDER; 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 = createSamplerState(pDevice, &mPointSampleState, &samplerDesc); if (!result) { return false; } return true; }
bool Controller::init(RenderDX11* _renderer, string _workingDirectory) { m_terrain = new Terrain(); HRESULT hr; bool res; if(!_renderer) // no rendering available, go home return false; if(_workingDirectory == "") // dont set the current environment as a working directory you bozo return false; m_pWorkingDirectory = _workingDirectory; m_renderer = _renderer; /* --- Overview of how the data linking and abstraction works --- Property { struct<T>[] } --- Refs by Id (explicit) ---> Resources<T>[Id] Composition { Interface<Property>[] } --- Refs by Datatype (Id is implicit) ---> Property<T>[Id] LLC: Create raw data MLL: Create 'property struct', link to desired raw data HLC: Create 'composition struct', link to all desired properties (note: as of now only one property per type is allowed) */ /************************ LOW LEVEL CREATION *************************/ /* init default render resources */ // blendstates unsigned int ref; hr = createBlendState(ref, D3D11_BLEND_ONE, D3D11_BLEND_ONE, D3D11_BLEND_ONE, TRUE); hr = createBlendState(ref, D3D11_BLEND_ONE, D3D11_BLEND_ONE, D3D11_BLEND_ONE, FALSE); hr = createBlendState(ref, D3D11_BLEND_SRC_ALPHA, D3D11_BLEND_INV_SRC_ALPHA, D3D11_BLEND_SRC_ALPHA, TRUE); D3D11_SAMPLER_DESC sampDesc; ZeroMemory( &sampDesc, sizeof(sampDesc)); sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; sampDesc.MinLOD = 0; sampDesc.MaxLOD = D3D11_FLOAT32_MAX; hr = createSamplerState(ref, sampDesc); sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; hr = createSamplerState(ref, sampDesc); // cbuffers unsigned onceRef, changeRef; hr = createCBuffer(onceRef, sizeof(CBOnce)); hr = createCBuffer(changeRef, sizeof(CBOnChange)); m_renderer->cbOnceID = onceRef; m_renderer->cbOnChangeID = changeRef; // shaders unsigned int vRef, pRef; hr = createShader<ID3D11VertexShader>(vRef, standardLayout, "defaultVS", "vs_4_0"); hr = createShader<ID3D11PixelShader>(pRef, standardLayout, "defaultPS", "ps_4_0"); // SRV's hr = createSRV(ref, "grass.png"); hr = createSRV(ref, "bozo.jpg"); /*************************** MEDIUM LEVEL LINKING ****************************/ /* Init default properties */ unsigned int sRef, mRef, tRef; // Shader Shader* s = new Shader(vRef, pRef); CModel<Shader>* cs = new CModel<Shader>("default_shader", s); // Mesh createCube(vRef, pRef); // notice reuse of paramaters Mesh* m = new Mesh(vRef, pRef, 36, ref); CModel<Mesh>* cm = new CModel<Mesh>("default_mesh", m); // Transform Transform* t = new Transform(elm::vec3(100,100,100), elm::vec3(0), elm::vec3(10)); CModel<Transform>* ct = new CModel<Transform>("default_transform", t); /* register properties */ addProperty(cs); addProperty(cm); addProperty(ct); /***************************** HIGH LEVEL COMPOSITION ******************************/ // Composition Composition* c = new Composition(); c->setName("default_object"); c->setProperty<Shader>(cs); c->setProperty<Mesh>(cm); c->setProperty<Transform>(ct); addComposition(c); return true; }
bool D3D11Context::startup (void* hwnd) { UINT flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; #ifdef _DEBUG flags |= D3D11_CREATE_DEVICE_DEBUG; #endif D3D_FEATURE_LEVEL featureLevels[] = { D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, }; if (m_failed (::D3D11CreateDevice (nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, flags, featureLevels, numElementsInArray (featureLevels), D3D11_SDK_VERSION, device, &featureLevel, contextIM))) { if (m_failed (::D3D11CreateDevice (nullptr, D3D_DRIVER_TYPE_WARP, nullptr, flags, featureLevels, numElementsInArray (featureLevels), D3D11_SDK_VERSION, device, &featureLevel, contextIM))) return false; } Hold<IDXGIDevice2> dxgiDevice; if (m_failed (device.as (dxgiDevice))) return false; Hold<IDXGIAdapter> dxgiAdapter; if (m_failed (dxgiDevice->GetAdapter (dxgiAdapter))) return false; Hold<IDXGIFactory2> dxgiFactory; if (m_failed (dxgiAdapter->GetParent (__uuidof (IDXGIFactory2), dxgiFactory))) return false; DXGI_SWAP_CHAIN_DESC1 desc; desc.Width = 0; desc.Height = 0; desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; desc.Stereo = false; desc.SampleDesc.Count = 1; desc.SampleDesc.Quality = 0; desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; desc.BufferCount = 2; desc.Scaling = DXGI_SCALING_STRETCH; // DXGI_SCALING_NONE is not supported on Windows7 desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; desc.AlphaMode = DXGI_ALPHA_MODE_IGNORE; desc.Flags = 0; if (m_failed (dxgiFactory->CreateSwapChainForHwnd (device, (HWND)hwnd, &desc, nullptr, nullptr, swapchain))) return false; Hold<ID3D11Texture2D> backBuf; if (m_failed (swapchain->GetBuffer (0, __uuidof (ID3D11Texture2D), backBuf))) return false; if (m_failed (device->CreateRenderTargetView (backBuf, nullptr, backBufRTView))) return false; RECT rect; ::GetClientRect ((HWND)hwnd, &rect); if (m_isnull (depthBuf.set (createTexture2DRT (rect.right - rect.left, rect.bottom - rect.top, 1, DXGI_FORMAT_R32_TYPELESS, DXGI_FORMAT_D32_FLOAT)))) return false; if (m_isnull (depthBufDSView.set (createDepthStencilView (depthBuf, 0, DXGI_FORMAT_D32_FLOAT)))) return false; if (m_isnull (depthBufSRView.set (createShaderResourceView (depthBuf, DXGI_FORMAT_R32_FLOAT)))) return false; // common sampler states { D3D11_SAMPLER_DESC _ = CD3D11_SAMPLER_DESC (D3D11_DEFAULT); _.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; _.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; _.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; _.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; if (m_isnull (sampWrapLinear.set (createSamplerState (_)))) return false; } { D3D11_SAMPLER_DESC _ = CD3D11_SAMPLER_DESC (D3D11_DEFAULT); _.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; _.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; _.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; _.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; if (m_isnull (sampWrapPoint.set (createSamplerState (_)))) return false; } { D3D11_SAMPLER_DESC _ = CD3D11_SAMPLER_DESC (D3D11_DEFAULT); _.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; _.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; _.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; _.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; if (m_isnull (sampClampLinear.set (createSamplerState (_)))) return false; } { D3D11_SAMPLER_DESC _ = CD3D11_SAMPLER_DESC (D3D11_DEFAULT); _.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; _.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; _.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; _.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; if (m_isnull (sampClampPoint.set (createSamplerState (_)))) return false; } // common rasterizer state { D3D11_RASTERIZER_DESC _ = CD3D11_RASTERIZER_DESC (D3D11_DEFAULT); _.CullMode = D3D11_CULL_NONE; _.FrontCounterClockwise = FALSE; if (m_isnull (rastCullNone.set (createRasterizerState (_)))) return false; } { D3D11_RASTERIZER_DESC _ = CD3D11_RASTERIZER_DESC (D3D11_DEFAULT); _.CullMode = D3D11_CULL_FRONT; _.FrontCounterClockwise = FALSE; if (m_isnull (rastCWCullFront.set (createRasterizerState (_)))) return false; } { D3D11_RASTERIZER_DESC _ = CD3D11_RASTERIZER_DESC (D3D11_DEFAULT); _.CullMode = D3D11_CULL_BACK; _.FrontCounterClockwise = FALSE; if (m_isnull (rastCWCullBack.set (createRasterizerState (_)))) return false; } { D3D11_RASTERIZER_DESC _ = CD3D11_RASTERIZER_DESC (D3D11_DEFAULT); _.CullMode = D3D11_CULL_FRONT; _.FrontCounterClockwise = TRUE; if (m_isnull (rastCCWCullFront.set (createRasterizerState (_)))) return false; } { D3D11_RASTERIZER_DESC _ = CD3D11_RASTERIZER_DESC (D3D11_DEFAULT); _.CullMode = D3D11_CULL_BACK; _.FrontCounterClockwise = TRUE; if (m_isnull (rastCCWCullBack.set (createRasterizerState (_)))) return false; } // common depth stencil state { D3D11_DEPTH_STENCIL_DESC _ = CD3D11_DEPTH_STENCIL_DESC (D3D11_DEFAULT); _.DepthEnable = TRUE; _.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; if (m_isnull (depthTestOnWriteOn.set (createDepthStencilState (_)))) return false; } { D3D11_DEPTH_STENCIL_DESC _ = CD3D11_DEPTH_STENCIL_DESC (D3D11_DEFAULT); _.DepthEnable = TRUE; _.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO; if (m_isnull (depthTestOnWriteOff.set (createDepthStencilState (_)))) return false; } { D3D11_DEPTH_STENCIL_DESC _ = CD3D11_DEPTH_STENCIL_DESC (D3D11_DEFAULT); _.DepthEnable = FALSE; _.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; if (m_isnull (depthTestOffWriteOn.set (createDepthStencilState (_)))) return false; } { D3D11_DEPTH_STENCIL_DESC _ = CD3D11_DEPTH_STENCIL_DESC (D3D11_DEFAULT); _.DepthEnable = FALSE; _.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO; if (m_isnull (depthTestOffWriteOff.set (createDepthStencilState (_)))) return false; } return true; }