Benjamin Yang · EAE 6320

Assignment 02

Mesh and effect representations, and a rectangle instead of a triangle

⬇ Download the game (Direct3D, x64) ⬇ OpenGL build (x86) Unzip anywhere and double click MyGame.exe. ESC closes the game.

What I did

I pulled the "Geometry Data" and "Shading Data" out of Graphics.d3d.cpp / Graphics.gl.cpp into two classes in the Graphics project:

cMesh (cMesh.h, cMesh.cpp, Direct3D/cMesh.d3d.cpp, OpenGL/cMesh.gl.cpp). It owns the vertex buffer (plus the cVertexFormat on D3D, or the vertex array object on OpenGL) and a vertex count. The interface is Initialize( vertexData, vertexCount ), CleanUp() and Draw(). Almost all of it is platform-specific, the shared .cpp only has the destructor that asserts CleanUp() was called.

cEffect (cEffect.h, cEffect.cpp, Direct3D/cEffect.d3d.cpp, OpenGL/cEffect.gl.cpp). It owns the vertex shader, the fragment shader, the cRenderState and, on OpenGL only, the program id. The interface is Initialize( vertexShaderPath, fragmentShaderPath, renderStateBits ), CleanUp() and Bind(). Here a good part is shared: loading the two shaders and initializing the render state is the same on both platforms, and so is releasing the shaders and binding the render state. cEffect.cpp does all of that and calls three small private functions that the platform files implement: InitializePlatformSpecific() (OpenGL creates and links the program, D3D does nothing), CleanUpPlatformSpecific() (OpenGL deletes the program) and BindShaders() (VSSetShader/PSSetShader vs glUseProgram).

I did it the way the assignment suggested: move the code first, get it running, commit, then clean up. The static s_vertexBuffer, s_vertexShader, s_programId and so on in the Graphics files are gone; there is now just one cMesh s_mesh and one cEffect s_effect.

For the second triangle I changed the vertex count to 6 and added three more vertices so the two triangles make the square from (0,0) to (1,1), which is the upper-right quarter of the screen. For the winding order I did not want to keep two different vertex arrays, so the vertex data in Graphics.[platform].cpp is always written counter-clockwise (the OpenGL / right-handed order) and cMesh.d3d.cpp swaps the second and third vertex of every triangle when it copies the data into the Direct3D buffer. That way the code that creates the mesh is the same in both files and does not have to know about winding order at all. I checked with back-face culling on that both triangles show up on both platforms, so the order is right.

Screenshots

Direct3D build showing a colored rectangle in the upper-right corner
Direct3D, x64 Debug. The rectangle is in the upper-right corner and still animates its color.
OpenGL build showing the same rectangle
OpenGL, x86 Debug. Same thing.

Direct3D capture (Visual Studio Graphics Analyzer)

Graphics Analyzer with ClearRenderTargetView selected and a black render target
ClearRenderTargetView() is selected and the render target is all black.
Graphics Analyzer with Draw selected and the rectangle in the render target
Draw() is selected and the rectangle is in the render target.
Pipeline Stages tab showing the wireframe of two triangles
Pipeline Stages tab for the same Draw() call. The Input Assembler thumbnail shows the mesh as a wireframe.
Zoomed Input Assembler thumbnail showing a square split into two triangles
The Input Assembler thumbnail zoomed in: the square with the diagonal is the two triangles.

OpenGL capture (RenderDoc)

RenderDoc with glClear selected and a black texture view
glClear() selected, Texture Viewer tab, the render target is black.
RenderDoc with glDrawArrays selected and the rectangle in the texture view
glDrawArrays() selected, Texture Viewer tab, the rectangle is drawn.
RenderDoc Mesh Viewer showing the two triangles
glDrawArrays() selected, Mesh Viewer tab (VS Input), the two triangles of the rectangle.

The code that binds the effect and draws the mesh

This is the render part of RenderFrame() in Graphics.d3d.cpp. Graphics.gl.cpp has exactly the same lines.

	// Bind the shading data
	{
		s_effect.Bind();
	}
	// Draw the geometry
	{
		s_mesh.Draw();
	}

Initialization is also identical in both files:

	return s_effect.Initialize( "data/Shaders/Vertex/standard.shader", "data/Shaders/Fragment/animatedColor.shader", renderStateBits );
	...
	return s_mesh.Initialize( vertexData, vertexCount );

What is still different between Graphics.d3d.cpp and Graphics.gl.cpp

After this assignment the two files are almost the same. Submission, the event handling, the frame constant buffer update, the geometry and shading initialization and the clean up are identical. What is left:

  1. Clearing the back buffer and depth buffer. D3D calls ClearRenderTargetView / ClearDepthStencilView on views it owns; OpenGL calls glClearColor / glClear and glDepthMask / glClearDepth / glClear.
  2. Presenting the frame. D3D calls swapChain->Present(), OpenGL calls SwapBuffers( deviceContext ).
  3. The render target and depth stencil views. Only the D3D file has s_renderTargetView / s_depthStencilView, InitializeViews() and the code that releases them. OpenGL gets its default framebuffer from the context, so there is nothing to create.
  4. Includes. Small things, like the OpenGL file needing <new>.

All of these can be moved behind platform-independent interfaces the same way mesh and effect were:

Once that is done the only platform-specific code left in the Graphics project would be inside the small classes (sContext, cShader, cVertexFormat, cRenderState, cConstantBuffer, cMesh, cEffect, cView), and Graphics.cpp itself would be one file.

Notes