Graphics API Reference

Introduction

This section provides a comprehensive reference for the Graphics API, detailing its components, functions, and best practices for developing visually rich applications and experiences. The Graphics API offers a powerful and flexible interface for interacting with the GPU, enabling high-performance rendering of 2D and 3D graphics.

Key features include:

  • Advanced shader programming support.
  • Efficient management of graphics resources like textures and buffers.
  • Optimized rendering pipelines for various hardware.
  • Cross-platform compatibility.

Core Concepts

Understanding the fundamental concepts of the Graphics API is crucial for effective utilization. This includes:

  • The Rendering Pipeline: The sequence of stages that data passes through to become an image on the screen.
  • Shaders: Small programs that run on the GPU to perform specific rendering tasks.
  • Graphics Resources: Data structures like textures, buffers, and render targets used for rendering.
  • State Management: Configuring various settings that affect how rendering is performed.

API Reference

Explore the detailed functions and structures that make up the Graphics API.

Rendering Pipeline

The rendering pipeline is the core mechanism for drawing graphics. It encompasses stages from vertex data input to pixel output.

SetRenderTargets

Sets the render targets for the current rendering operation.
Parameters:
  • RenderTarget[] targets: An array of render target objects to bind.
  • DepthStencilTarget* depthStencil: Optional depth-stencil buffer.
Returns: void
void GraphicsAPI::SetRenderTargets(RenderTarget[] targets, DepthStencilTarget* depthStencil = nullptr);

ClearRenderTarget

Clears a specified render target with a given color.
Parameters:
  • RenderTarget* target: The render target to clear.
  • Color color: The color to clear with (RGBA values from 0.0 to 1.0).
Returns: void
void GraphicsAPI::ClearRenderTarget(RenderTarget* target, Color color);

Shaders

Shaders are essential for defining visual appearance. The API provides functions to load, compile, and bind shaders.

CreateShaderFromSource

Compiles a shader from source code strings.
Parameters:
  • ShaderType type: The type of shader (e.g., Vertex, Fragment).
  • const char* source: The shader source code.
Returns: Shader* A pointer to the compiled shader object, or nullptr on failure.
Shader* GraphicsAPI::CreateShaderFromSource(ShaderType type, const char* source);

BindShader

Binds a compiled shader program for subsequent rendering.
Parameters:
  • ShaderProgram* program: The shader program to bind.
Returns: void
void GraphicsAPI::BindShader(ShaderProgram* program);

Textures

Textures are used to add detail and color to surfaces. Functions for creating and managing textures are provided.

CreateTexture2D

Creates a 2D texture from image data.
Parameters:
  • int width: The width of the texture.
  • int height: The height of the texture.
  • PixelFormat format: The pixel format of the texture.
  • const void* data: Pointer to the image data.
Returns: Texture* A pointer to the created texture object.
Texture* GraphicsAPI::CreateTexture2D(int width, int height, PixelFormat format, const void* data);

BindTexture

Binds a texture to a specific texture unit.
Parameters:
  • TextureUnit unit: The texture unit to bind to.
  • Texture* texture: The texture to bind.
Returns: void
void GraphicsAPI::BindTexture(TextureUnit unit, Texture* texture);

Buffers

Vertex, index, and uniform buffers are used to store data for rendering.

CreateBuffer

Creates a generic buffer object.
Parameters:
  • BufferType type: The type of buffer (e.g., Vertex, Index, Uniform).
  • size_t size: The size of the buffer in bytes.
  • const void* data: Pointer to initial data, or nullptr.
  • BufferUsage usage: The intended usage pattern of the buffer.
Returns: Buffer* A pointer to the created buffer object.
Buffer* GraphicsAPI::CreateBuffer(BufferType type, size_t size, const void* data, BufferUsage usage);

UpdateBufferData

Updates the data within an existing buffer.
Parameters:
  • Buffer* buffer: The buffer to update.
  • size_t offset: The offset in bytes from the start of the buffer.
  • size_t size: The amount of data to update.
  • const void* data: Pointer to the new data.
Returns: void
void GraphicsAPI::UpdateBufferData(Buffer* buffer, size_t offset, size_t size, const void* data);

Vertex Processing

This stage transforms vertex data from model space to screen space.

BindVertexArray

Binds a vertex array object (VAO) which defines vertex attribute pointers.
Parameters:
  • VertexArray* vao: The vertex array object to bind.
Returns: void
void GraphicsAPI::BindVertexArray(VertexArray* vao);

Fragment Processing

This stage determines the final color of each pixel.

DrawArrays

Renders primitives from the currently bound vertex array.
Parameters:
  • PrimitiveType type: The type of primitive to draw (e.g., Triangles, Lines).
  • int first: The index of the first vertex to draw.
  • int count: The number of vertices to draw.
Returns: void
void GraphicsAPI::DrawArrays(PrimitiveType type, int first, int count);

DrawElements

Renders primitives using indexed vertex data.
Parameters:
  • PrimitiveType type: The type of primitive to draw.
  • int count: The number of indices to draw.
  • IndexType indexType: The data type of the indices (e.g., UnsignedInt, UnsignedShort).
  • const void* indices: Pointer to the index data.
Returns: void
void GraphicsAPI::DrawElements(PrimitiveType type, int count, IndexType indexType, const void* indices);

Tutorials

Dive into practical examples and step-by-step guides to master the Graphics API.

Sample Projects

Download and explore complete projects that demonstrate various Graphics API features.