An open API service indexing awesome lists of open source software.

https://github.com/andrestubbe/fastgraphics

WIP, High-Performance GPU-Accelerated Graphics2D for Java (600% Faster than Java2D). WIP - Building a complete Graphics2D replacement with DirectX 11 backend
https://github.com/andrestubbe/fastgraphics

2d directx gaming gpu graphics graphics2d java performance rendering

Last synced: 3 months ago
JSON representation

WIP, High-Performance GPU-Accelerated Graphics2D for Java (600% Faster than Java2D). WIP - Building a complete Graphics2D replacement with DirectX 11 backend

Awesome Lists containing this project

README

          

# FastGraphics β€” High-Performance GPU-Accelerated Graphics2D (600% Faster than Java2D)

> **🎨 MAJOR UPDATE** - Alpha Transparency & Rounded Rectangles now fully implemented! See [TODO.md](TODO.md) for remaining features.

**⚑ Ultra-fast GPU-accelerated Graphics2D replacement for Java β€” 600% faster than java.awt.Graphics2D / Java2D**

[![Build](https://img.shields.io/badge/build-passing-brightgreen.svg)]()
[![Java](https://img.shields.io/badge/Java-17+-blue.svg)](https://www.java.com)
[![Platform](https://img.shields.io/badge/Platform-Windows%2010+-lightgrey.svg)]()
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

```java
// Quick Start β€” Ultra-fast 2D rendering
FastGraphics2D g = new FastGraphics2D(hwnd);
g.setColor(Color.RED);
g.fillRect(10, 10, 100, 50);
g.setColor(Color.BLUE);
g.fillOval(200, 100, 30, 30);
g.present(); // 1 Draw Call fΓΌr alles!

// NEW: Alpha transparency support!
g.setColor(new Color(255, 0, 0, 128)); // 50% transparent red
g.fillOval(100, 100, 200, 200);

// NEW: Rounded rectangles!
g.setColor(new Color(0, 200, 255));
g.fillRoundRect(300, 200, 150, 100, 20, 20);
```

FastGraphics is a **high-performance GPU-accelerated 2D rendering library** that replaces `java.awt.Graphics2D` with a **native DirectX 11 backend**. Built for **real-time games**, **data visualization**, **scientific applications**, and **high-frequency UI rendering** where Java2D performance bottlenecks.

**Keywords:** java graphics2d alternative, gpu accelerated 2d rendering, directx java rendering, fast fillRect, java game engine 2d, hardware accelerated graphics, batch rendering java, instanced rendering 2d, 600fps java graphics

If you need **thousands of shapes at 60fps+**, **batch rendering**, or **GPU-accelerated 2D**, FastGraphics delivers native-level DirectX 11 performance with Java simplicity.

---

## Table of Contents

- [Why FastGraphics?](#why-fastgraphics)
- [Performance Benchmarks](#performance-benchmarks)
- [FastGraphics vs java.awt.Graphics2D](#fastgraphics-vs-javaawtgraphics2d)
- [Quick Start](#quick-start)
- [API Reference](#api-reference)
- [TV Test Pattern Demo](#tv-test-pattern-demo)
- [Build from Source](#build-from-source)
- [Platform Support](#platform-support)
- [License](#license)

---

## Why FastGraphics?

`java.awt.Graphics2D` is convenient but slow. Its immediate-mode API creates CPU bottlenecks, and Java2D's software rasterizer limits performance to ~100-200 simple shapes per frame.

FastGraphics solves this with:
- **Batch Rendering** β€” hundreds/thousands of shapes in a single GPU draw call
- **Instanced Rendering** β€” 76% less data transfer than vertex expansion
- **DirectX 11 backend** β€” native GPU performance, zero Java2D overhead
- **Zero GC pressure** β€” direct ByteBuffers, pooled resources
- **Drop-in API** β€” familiar Graphics2D-style methods

---

## Performance Benchmarks

| Test | Java2D (AWT) | FastGraphics (DX11) | Speedup |
|------|--------------|---------------------|---------|
| 1,000 Rectangles | ~120 FPS | **5,335 FPS** | **44Γ—** |
| 5,000 Rectangles | ~60 FPS | **6,056 FPS** | **100Γ—** |
| 10,000 Rectangles | ~40 FPS | **4,585 FPS** | **114Γ—** |
| 50,000 Rectangles | ~5 FPS | **1,060 FPS** | **212Γ—** |
| Batch Overhead | High | **Minimal** | **600%+** |

*Measured on Windows 11, RTX 3070, Java 17, 360Hz display. Tests use fillRect() with varying counts.*

---

## FastGraphics vs java.awt.Graphics2D

| Feature | java.awt.Graphics2D | FastGraphics |
|---------|---------------------|--------------|
| Rendering Backend | Java2D (CPU) | DirectX 11 (GPU) |
| Max Shapes @ 60fps | ~1,000 | **50,000+** |
| Batch Rendering | ❌ No | βœ… Yes (Automatic) |
| Instanced Rendering | ❌ No | βœ… Yes (76% less bandwidth) |
| Memory Pressure | High (GC) | **Zero (Direct Buffers)** |
| Cross-Platform | βœ… All platforms | Windows (DX11) |

---

## Quick Start

```java
import fastgraphics.FastGraphics2D;
import javax.swing.JFrame;
import java.awt.Color;

public class QuickStart {
public static void main(String[] args) {
// Create window
JFrame frame = new JFrame("FastGraphics Demo");
frame.setSize(800, 600);
frame.setVisible(true);

// Get native window handle
long hwnd = FastGraphics2D.findWindow("FastGraphics Demo");

// Create FastGraphics context
FastGraphics2D g = new FastGraphics2D(hwnd);

// Render loop
while (frame.isVisible()) {
g.setColor(Color.BLACK);
g.clear();

g.setColor(Color.RED);
g.fillRect(10, 10, 100, 50);

g.setColor(Color.BLUE);
g.fillRect(200, 100, 80, 80);

g.present(); // Single GPU draw call!
}
}
}
```

---

## TV Test Pattern Demo

FastGraphics includes a classic **80s TV Test Pattern** demo for visual validation:

The test renders identical patterns in both FastGraphics and Java2D for pixel-perfect comparison. This validates:
- βœ… Color accuracy (Color Bars)
- βœ… Geometric precision (Convergence Circles)
- βœ… Gradient rendering (Shade Bars)
- βœ… Text rendering (Station ID)

Run the test:
```bash
java -cp out demo.Comparator
```

---

## API Reference

### Core Methods

| Method | Description | Status |
|--------|-------------|--------|
| `new FastGraphics2D(hwnd)` | Create rendering context | βœ… Implemented |
| `setColor(Color c)` | Set current drawing color | βœ… Implemented |
| `fillRect(x, y, w, h)` | Fill rectangle (batched) | βœ… Implemented |
| `fillOval(x, y, w, h)` | Fill oval/circle | βœ… Implemented |
| `drawRect(x, y, w, h)` | Draw rectangle outline | βœ… Implemented |
| `drawOval(x, y, w, h)` | Draw oval/circle outline | βœ… Implemented |
| `drawLine(x1, y1, x2, y2)` | Draw line | βœ… Implemented |
| `drawPolygon(xPoints, yPoints)` | Draw polygon outline | βœ… Implemented |
| `fillPolygon(xPoints, yPoints)` | Fill polygon (convex) | βœ… Implemented |
| `drawArc(x, y, w, h, startAngle, arcAngle)` | Draw arc | βœ… Implemented |
| `fillArc(x, y, w, h, startAngle, arcAngle)` | Fill arc | βœ… Implemented |
| `drawRoundRect(x, y, w, h, arcWidth, arcHeight)` | Draw rounded rectangle | βœ… Implemented |
| `fillRoundRect(x, y, w, h, arcWidth, arcHeight)` | Fill rounded rectangle | βœ… Implemented |
| `translate(tx, ty)` | Translate coordinate system | βœ… Implemented |
| `scale(sx, sy)` | Scale coordinate system | βœ… Implemented |
| `rotate(angle)` | Rotate coordinate system | βœ… Implemented |
| `resetTransform()` | Reset transformations | βœ… Implemented |
| `setStroke(float lineWidth)` | Set line width for drawing | βœ… Implemented |
| `setRenderingHint(key, value)` | Set rendering hint | ⚠️ Stub (stores only) |
| `getRenderingHint(key)` | Get rendering hint | βœ… Implemented |
| `setClip(x, y, w, h)` | Set clipping rectangle | ⚠️ Stub (stores only) |
| `resetClip()` | Reset clipping | ⚠️ Stub (stores only) |
| `drawString(str, x, y)` | Draw text string | ⚠️ Stub (no effect) |
| `drawImage(img, x, y, w, h)` | Draw image (GPU-accelerated with caching) | βœ… Implemented |
| `clear()` / `clear(Color c)` | Clear background | βœ… Implemented |
| `present()` | Display rendered frame | βœ… Implemented |

### Known Limitations

- **AntiAliasing**: RenderingHints.KEY_ANTIALIASING is supported via API, but DirectX 11 MSAA requires Swap Chain configuration (not runtime-switchable)
- **Clipping**: setClip()/resetClip() store values but don't apply clipping (requires Scissor Rects or Stencil Buffer)
- **Line Width**: βœ… Now implemented! Use `setStroke(width)` for thick lines
- **Text Rendering**: drawString() is a stub (requires textured shaders and font rendering)
- **Image Rendering**: drawImage() is fully implemented with GPU texture caching
- **Alpha Transparency**: Now fully supported! Use `new Color(r, g, b, alpha)` for transparent shapes

### State Management

FastGraphics automatically batches operations. No manual `beginBatch()` / `endBatch()` needed!

```java
g.setColor(Color.RED);
g.fillRect(0, 0, 50, 50); // Queued

// No color change = same batch
g.fillRect(60, 0, 50, 50); // Same batch

// Color change = auto-flush, new batch
g.setColor(Color.BLUE);
g.fillRect(120, 0, 50, 50); // New batch

g.present(); // All batches rendered in 1-2 draw calls
```

---

## Build from Source

### Prerequisites
- Windows 10/11
- Java JDK 17+
- Visual Studio 2022 (C++ workload)

### Build
```powershell
# PowerShell
.\build.ps1

# Or manual:
javac -d out src\fastgraphics\*.java src\demo\*.java
cl /O2 /EHsc /LD /Fe:out\FastGraphics.dll native\FastGraphics.cpp \
/I"%JAVA_HOME%\include" /I"%JAVA_HOME%\include\win32" \
d3d11.lib d3dcompiler.lib user32.lib
```

### Run
```bash
cd out
java -cp . -Djava.library.path=. demo.DemoApp
```

---

## Platform Support

| Platform | Status | Notes |
|----------|--------|-------|
| Windows 10/11 | βœ… Full Support | DirectX 11 native |
| Linux | ❌ Not Supported | Would need OpenGL/Vulkan port |
| macOS | ❌ Not Supported | Would need Metal port |

---

## License

MIT License β€” Free for commercial and personal use.

See [LICENSE](LICENSE) for details.

---

**Built with ❀️** β€” Making Java fast again!