39 D3D11_DEPTH_WRITE_MASK_ZERO,
40 D3D11_COMPARISON_LESS_EQUAL);
93 const float viewportClear[4] = { 0.10f, 0.10f, 0.10f, 1.0f };
100 viewportPass.
begin(deviceContext, viewportClear);
144 if (lhs->materialInstance != rhs->materialInstance) {
145 return lhs->materialInstance < rhs->materialInstance;
150 std::sort(m_transparentQueue.begin(), m_transparentQueue.end(),
152 return lhs->distanceToCamera > rhs->distanceToCamera;
162 ID3D11ShaderResourceView* nullShadowSRV[1] = {
nullptr };
167 D3D11_VIEWPORT shadowViewport{};
168 shadowViewport.TopLeftX = 0.0f;
169 shadowViewport.TopLeftY = 0.0f;
172 shadowViewport.MinDepth = 0.0f;
173 shadowViewport.MaxDepth = 1.0f;
180 if (!
object || !object->castShadow) {
193 const float clearColor[4] = { 0.10f, 0.10f, 0.10f, 1.0f };
194 ID3D11ShaderResourceView* nullShadowSRV[1] = {
nullptr };
215 ID3D11ShaderResourceView* nullShadowSRV[1] = {
nullptr };
235 ID3D11ShaderResourceView* nullShadowSRV[1] = {
nullptr };
243 Material* material =
object->materialInstance ?
object->materialInstance->getMaterial() :
nullptr;
263 if (!
object.mesh || (!
object.materialInstance &&
object.materialInstances.empty())) {
273 std::vector<Submesh>& submeshes =
object.mesh->getSubmeshes();
274 for (
Submesh& submesh : submeshes) {
276 if (submesh.materialSlot <
object.materialInstances.size() &&
277 object.materialInstances[submesh.materialSlot]) {
278 materialInstance =
object.materialInstances[submesh.materialSlot];
281 if (!materialInstance) {
325 submesh.vertexBuffer.render(deviceContext, 0, 1);
326 submesh.indexBuffer.render(deviceContext, 0, 1,
false, DXGI_FORMAT_R32_UINT);
327 deviceContext.
DrawIndexed(submesh.indexCount, submesh.startIndex, 0);
345 std::vector<Submesh>& submeshes =
object.mesh->getSubmeshes();
346 for (
Submesh& submesh : submeshes) {
347 submesh.vertexBuffer.render(deviceContext, 0, 1);
348 submesh.indexBuffer.render(deviceContext, 0, 1,
false, DXGI_FORMAT_R32_UINT);
349 deviceContext.
DrawIndexed(submesh.indexCount, submesh.startIndex, 0);
359 DXGI_FORMAT_R24G8_TYPELESS,
360 D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE);
376 builder.
Add(
"POSITION", DXGI_FORMAT_R32G32B32_FLOAT)
377 .
Add(
"NORMAL", DXGI_FORMAT_R32G32B32_FLOAT)
378 .
Add(
"TANGENT", DXGI_FORMAT_R32G32B32_FLOAT)
379 .
Add(
"BITANGENT", DXGI_FORMAT_R32G32B32_FLOAT)
380 .
Add(
"TEXCOORD", DXGI_FORMAT_R32G32_FLOAT);
402 XMVECTOR lightDirVec = XMVector3Normalize(XMVectorSet(lightDir.
x, lightDir.
y, lightDir.
z, 0.0f));
404 XMVECTOR lightTarget = cameraPos;
405 XMVECTOR lightEye = XMVectorSubtract(lightTarget, XMVectorScale(lightDirVec, 35.0f));
406 XMVECTOR worldUp = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
407 if (fabsf(XMVectorGetX(XMVector3Dot(lightDirVec, worldUp))) > 0.98f) {
408 worldUp = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);
411 XMMATRIX lightView = XMMatrixLookAtLH(lightEye, lightTarget, worldUp);
412 XMMATRIX lightProjection = XMMatrixOrthographicLH(40.0f, 40.0f, 1.0f, 80.0f);
422 D3D11_BLEND_DESC blendDesc{};
423 blendDesc.AlphaToCoverageEnable = FALSE;
424 blendDesc.IndependentBlendEnable = FALSE;
426 D3D11_RENDER_TARGET_BLEND_DESC& renderTarget = blendDesc.RenderTarget[0];
427 renderTarget.BlendEnable = FALSE;
428 renderTarget.SrcBlend = D3D11_BLEND_ONE;
429 renderTarget.DestBlend = D3D11_BLEND_ZERO;
430 renderTarget.BlendOp = D3D11_BLEND_OP_ADD;
431 renderTarget.SrcBlendAlpha = D3D11_BLEND_ONE;
432 renderTarget.DestBlendAlpha = D3D11_BLEND_ZERO;
433 renderTarget.BlendOpAlpha = D3D11_BLEND_OP_ADD;
434 renderTarget.RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
441 renderTarget.BlendEnable = TRUE;
442 renderTarget.SrcBlend = D3D11_BLEND_SRC_ALPHA;
443 renderTarget.DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
444 renderTarget.BlendOp = D3D11_BLEND_OP_ADD;
445 renderTarget.SrcBlendAlpha = D3D11_BLEND_ONE;
446 renderTarget.DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
447 renderTarget.BlendOpAlpha = D3D11_BLEND_OP_ADD;
454 renderTarget.BlendEnable = TRUE;
455 renderTarget.SrcBlend = D3D11_BLEND_SRC_ALPHA;
456 renderTarget.DestBlend = D3D11_BLEND_ONE;
457 renderTarget.BlendOp = D3D11_BLEND_OP_ADD;
458 renderTarget.SrcBlendAlpha = D3D11_BLEND_ONE;
459 renderTarget.DestBlendAlpha = D3D11_BLEND_ONE;
460 renderTarget.BlendOpAlpha = D3D11_BLEND_OP_ADD;
467 renderTarget.BlendEnable = TRUE;
468 renderTarget.SrcBlend = D3D11_BLEND_ONE;
469 renderTarget.DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
470 renderTarget.BlendOp = D3D11_BLEND_OP_ADD;
471 renderTarget.SrcBlendAlpha = D3D11_BLEND_ONE;
472 renderTarget.DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
473 renderTarget.BlendOpAlpha = D3D11_BLEND_OP_ADD;
Declara la API de Camera dentro del subsistema Utilities.
Declara la API de DeviceContext dentro del subsistema Core.
Declara la API de Device dentro del subsistema Core.
Declara la API de EditorViewportPass dentro del subsistema Utilities.
Declara la API de ForwardRenderer dentro del subsistema Rendering.
Declara la API de LayoutBuilder dentro del subsistema Utilities.
Declara la API de MaterialInstance dentro del subsistema Rendering.
Declara la API de Material dentro del subsistema Rendering.
Declara la API de Mesh dentro del subsistema Rendering.
Declara la API de SamplerState dentro del subsistema Core.
Declara la API de Skybox dentro del subsistema Utilities.
void update(DeviceContext &deviceContext, ID3D11Resource *pDstResource, unsigned int DstSubresource, const D3D11_BOX *pDstBox, const void *pSrcData, unsigned int SrcRowPitch, unsigned int SrcDepthPitch)
Actualiza el contenido del buffer (típicamente mediante UpdateSubresource).
HRESULT init(Device &device, const MeshComponent &mesh, unsigned int bindFlag)
Inicializa el buffer como Vertex o Index Buffer usando un MeshComponent.
void render(DeviceContext &deviceContext, unsigned int StartSlot, unsigned int NumBuffers, bool setPixelShader=false, DXGI_FORMAT format=DXGI_FORMAT_UNKNOWN)
Enlaza el buffer a la etapa correspondiente del pipeline para el frame de render.
void destroy()
Libera el ID3D11Buffer y resetea los metadatos internos.
XMMATRIX getView() const
Matriz View (mundo->vista).
EU::Vector3 getPosition() const
Obtiene la posición en mundo.
XMMATRIX getProj() const
Matriz Projection (vista->clip).
HRESULT init(Device &device, bool depthEnable, D3D11_DEPTH_WRITE_MASK writeMask, D3D11_COMPARISON_FUNC depthFunc)
Crea el objeto ID3D11DepthStencilState a partir de flags comunes.
void destroy()
Libera el recurso ID3D11DepthStencilState y deja la instancia en estado no inicializado.
void render(DeviceContext &deviceContext, unsigned int stencilRef=0, bool reset=false)
Aplica el estado de profundidad/esténcil al contexto (OMSetDepthStencilState).
void destroy()
Libera el recurso asociado al ID3D11DepthStencilView.
HRESULT init(Device &device, Texture &depthStencil, DXGI_FORMAT format)
Inicializa el ID3D11DepthStencilView a partir de una textura de profundidad.
ID3D11DepthStencilView * m_depthStencilView
Vista de profundidad/esténcil de Direct3D 11.
void DrawIndexed(unsigned int IndexCount, unsigned int StartIndexLocation, int BaseVertexLocation)
Envía un comando de dibujado de primitivas indexadas.
void OMSetBlendState(ID3D11BlendState *pBlendState, const float BlendFactor[4], unsigned int SampleMask)
Asigna un Blend State al Output Merger.
void PSSetShaderResources(unsigned int StartSlot, unsigned int NumViews, ID3D11ShaderResourceView *const *ppShaderResourceViews)
Asigna Shader Resource Views a la etapa de Pixel Shader.
void IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY Topology)
Define la topología de primitivas a renderizar.
void RSSetViewports(unsigned int NumViewports, const D3D11_VIEWPORT *pViewports)
Configura los viewports en la etapa de rasterización.
ID3D11DeviceContext * m_deviceContext
Puntero al contexto inmediato de Direct3D 11.
void ClearDepthStencilView(ID3D11DepthStencilView *pDepthStencilView, unsigned int ClearFlags, float Depth, UINT8 Stencil)
Limpia un Depth Stencil View.
void OMSetRenderTargets(unsigned int NumViews, ID3D11RenderTargetView *const *ppRenderTargetViews, ID3D11DepthStencilView *pDepthStencilView)
Asigna Render Targets y Depth Stencil al Output Merger.
Encapsula un ID3D11Device y facilita la creación de recursos gráficos en Direct3D 11.
ID3D11Device * m_device
Puntero al dispositivo Direct3D 11.
float x
The x-coordinate of the vector.
float z
The z-coordinate of the vector.
float y
The y-coordinate of the vector.
HRESULT init(Device &device, unsigned int width, unsigned int height)
void setViewport(DeviceContext &deviceContext)
void begin(DeviceContext &deviceContext, const float clearColor[4])
void clearDepth(DeviceContext &deviceContext)
HRESULT resize(Device &device, unsigned int width, unsigned int height)
DepthStencilView m_shadowDSV
void resize(Device &device, unsigned int width, unsigned int height)
Reconstuye los recursos dependientes del tamano del viewport.
ID3D11BlendState * m_alphaBlendState
ID3D11BlendState * m_opaqueBlendState
ID3D11BlendState * m_premultipliedBlendState
std::vector< const RenderObject * > m_transparentQueue
void renderOpaquePass(DeviceContext &deviceContext)
void destroy()
Libera los recursos internos del renderer.
void updatePerFrame(const Camera &camera, const RenderScene &scene, DeviceContext &deviceContext)
Actualiza constantes globales usadas por el frame actual.
void renderObject(DeviceContext &deviceContext, const RenderObject &object, RenderPassType passType)
CBPerMaterial m_cbPerMaterial
HRESULT init(Device &device)
Inicializa buffers, shaders y estados del renderer.
HRESULT createBlendStates(Device &device)
Buffer m_perMaterialBuffer
DepthStencilState m_transparentDepthStencil
void renderShadowPass(DeviceContext &deviceContext)
void renderShadowObject(DeviceContext &deviceContext, const RenderObject &object)
ID3D11BlendState * resolveBlendState(const Material *material) const
ID3D11BlendState * m_additiveBlendState
Texture m_shadowDepthTexture
void render(DeviceContext &deviceContext, const Camera &camera, RenderScene &scene, EditorViewportPass &viewportPass)
Renderiza la escena completa sobre el EditorViewportPass.
RasterizerState m_shadowRasterizer
CBPerObject m_cbPerObject
ShaderProgram m_shadowShader
void buildQueues(RenderScene &scene, const Camera &camera)
std::vector< const RenderObject * > m_opaqueQueue
void updateLightMatrices(const Camera &camera, const RenderScene &scene)
void renderTransparentPass(DeviceContext &deviceContext)
HRESULT createShadowResources(Device &device)
void renderPreShadowDebugPass(DeviceContext &deviceContext, RenderScene &scene)
EditorViewportPass m_preShadowDebugPass
void renderSkyboxPass(DeviceContext &deviceContext, RenderScene &scene)
unsigned int m_shadowMapSize
LayoutBuilder & Add(const char *semantic, DXGI_FORMAT format, UINT semanticIndex=0, UINT inputSlot=0, UINT alignedByteOffset=D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_CLASSIFICATION slotClass=D3D11_INPUT_PER_VERTEX_DATA, UINT instanceStepRate=0)
Agrupa un material base con sus texturas y parametros concretos.
Material * getMaterial() const
void bindTextures(DeviceContext &deviceContext) const
Enlaza las texturas de la instancia en el contexto grafico actual.
MaterialParams & getParams()
Describe el estado fijo compartido por una o mas instancias de material.
ShaderProgram * getShader() const
BlendMode getBlendMode() const
RasterizerState * getRasterizerState() const
SamplerState * getSamplerState() const
MaterialDomain getDomain() const
DepthStencilState * getDepthStencilState() const
void destroy()
Libera el recurso ID3D11RasterizerState.
void render(DeviceContext &deviceContext)
Aplica el Rasterizer State al contexto de dispositivo.
HRESULT init(Device device)
Inicializa el Rasterizer State.
Contenedor temporal con los elementos visibles de un frame.
Skybox * skybox
Skybox activo para el frame actual.
std::vector< RenderObject > transparentObjects
Objetos transparentes ordenables por distancia.
std::vector< LightData > directionalLights
Luces direccionales activas en la escena.
std::vector< RenderObject > opaqueObjects
Objetos opacos listos para renderizar.
void render(DeviceContext &deviceContext, unsigned int StartSlot, unsigned int NumSamplers)
Asigna el Sampler State a la etapa de Pixel Shader.
ID3D11VertexShader * m_VertexShader
Vertex Shader compilado y creado en GPU.
void render(DeviceContext &deviceContext)
Aplica el Vertex Shader, Pixel Shader e Input Layout al pipeline.
HRESULT init(Device &device, const std::string &fileName, LayoutBuilder layoutBuilder)
Inicializa el programa de shaders desde un archivo HLSL.
void destroy()
Libera todos los recursos asociados (shaders, blobs e input layout).
void render(DeviceContext &deviceContext)
HRESULT init(Device &device, const std::string &textureName, ExtensionType extensionType)
Inicializa una textura cargada desde archivo.
void destroy()
Libera los recursos de la textura.
ID3D11ShaderResourceView * m_textureFromImg
Vista de la textura como recurso de shader.
XMFLOAT4X4 LightViewProjection
Describe una porcion renderizable de una malla con sus buffers asociados.