D3D11: Only apply attachments that are written by the program

This works around a bug in the AMD driver that writes 0's to the
first attachment if it isn't written by the pixel shader.

BUG=angleproject:2048

Change-Id: I384fd60c0e0a37fbc0fd7b69fe1ec74fe4ffac8f
Reviewed-on: https://chromium-review.googlesource.com/531630
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
diff --git a/src/libANGLE/renderer/d3d/FramebufferD3D.cpp b/src/libANGLE/renderer/d3d/FramebufferD3D.cpp
index 6975b05..4d3b064 100644
--- a/src/libANGLE/renderer/d3d/FramebufferD3D.cpp
+++ b/src/libANGLE/renderer/d3d/FramebufferD3D.cpp
@@ -301,11 +301,9 @@
 void FramebufferD3D::syncState(const gl::Context *context,
                                const gl::Framebuffer::DirtyBits &dirtyBits)
 {
-    bool invalidateColorAttachmentCache = false;
-
     if (!mColorAttachmentsForRender.valid())
     {
-        invalidateColorAttachmentCache = true;
+        return;
     }
 
     for (auto dirtyBit : dirtyBits)
@@ -314,13 +312,19 @@
              dirtyBit < gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX) ||
             dirtyBit == gl::Framebuffer::DIRTY_BIT_DRAW_BUFFERS)
         {
-            invalidateColorAttachmentCache = true;
+            mColorAttachmentsForRender.reset();
         }
     }
+}
 
-    if (!invalidateColorAttachmentCache)
+const gl::AttachmentList &FramebufferD3D::getColorAttachmentsForRender(const gl::Context *context)
+{
+    gl::DrawBufferMask activeProgramOutputs =
+        context->getContextState().getState().getProgram()->getActiveOutputVariables();
+
+    if (mColorAttachmentsForRender.valid() && mCurrentActiveProgramOutputs == activeProgramOutputs)
     {
-        return;
+        return mColorAttachmentsForRender.value();
     }
 
     // Does not actually free memory
@@ -335,7 +339,8 @@
         GLenum drawBufferState                           = drawBufferStates[attachmentIndex];
         const gl::FramebufferAttachment &colorAttachment = colorAttachments[attachmentIndex];
 
-        if (colorAttachment.isAttached() && drawBufferState != GL_NONE)
+        if (colorAttachment.isAttached() && drawBufferState != GL_NONE &&
+            activeProgramOutputs[attachmentIndex])
         {
             ASSERT(drawBufferState == GL_BACK ||
                    drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + attachmentIndex));
@@ -348,11 +353,8 @@
     }
 
     mColorAttachmentsForRender = std::move(colorAttachmentsForRender);
-}
+    mCurrentActiveProgramOutputs = activeProgramOutputs;
 
-const gl::AttachmentList &FramebufferD3D::getColorAttachmentsForRender() const
-{
-    ASSERT(mColorAttachmentsForRender.valid());
     return mColorAttachmentsForRender.value();
 }