102 Patches: Detours to the Rescue
C reference for DttR maintainers and modders.
Loading...
Searching...
No Matches
graphics_private.h
Go to the documentation of this file.
1#ifndef GRAPHICS_PRIVATE_H
2#define GRAPHICS_PRIVATE_H
3
4#include <SDL3/SDL.h>
5#include <dttr_mods.h>
6#include <kvec.h>
7#include <stdbool.h>
8#include <stddef.h>
9#include <stdint.h>
10#include <windows.h>
11
12typedef uint32_t DTTR_Texture;
13#define DTTR_INVALID_TEXTURE 0
14typedef kvec_t(int) DTTR_IntVector;
15
24
37
48
55
61
62#define DTTR_D3DTOP_DISABLE 1
63#define DTTR_D3DTOP_SELECTARG1 2
64#define DTTR_D3DTOP_SELECTARG2 3
65#define DTTR_D3DTOP_MODULATE 4
66
67#define DTTR_D3DTA_DIFFUSE 0
68#define DTTR_D3DTA_CURRENT 1
69#define DTTR_D3DTA_TEXTURE 2
70
71typedef struct {
72 float x, y, z;
73 float rhw;
74 float r, g, b, a;
75 float u, v;
77
78#define DTTR_CLEAR_COLOR 0x01
79#define DTTR_CLEAR_DEPTH 0x02
80#define DTTR_CLEAR_STENCIL 0x04
81
82typedef struct {
83 SDL_GPUTexture *gpu_tex;
84 void *pixels;
85 int width;
86 int height;
89 uint32_t update_streak;
90 uint32_t refcount;
92 uint64_t cache_key;
94
96typedef struct {
97 SDL_GPUTransferBuffer *transfer_buffer;
98 uint32_t capacity;
99 bool in_use;
101
102#define DTTR_MAX_STAGED_TEXTURES 4096
103#define DTTR_UPLOAD_POOL_SIZE 256
104
105typedef struct {
106 float mvp[16];
107 float screen_size[2];
108 float is_2d;
110 float color_op;
113 float alpha_op;
117
122
124typedef struct {
125 int x;
126 int y;
127 int w;
128 int h;
130
132
134typedef struct {
139 const uint8_t *pixels,
140 int width,
141 int height,
142 int stride
143 );
146 const char *(*get_driver_name)(const DTTR_BackendState *state);
149
151
153typedef struct {
155
156 union {
157 struct {
158 uint32_t first_vertex;
159 uint32_t vertex_count;
161 SDL_GPUTexture *texture;
162 SDL_GPUSampler *sampler;
168 } draw;
169
170 struct {
171 uint32_t flags;
172 SDL_FColor color;
173 float depth;
174 } clear;
175 };
177
178typedef kvec_t(DTTR_BatchRecord) DTTR_BatchRecordVector;
179
180#define DTTR_BLEND_OFF 0
181#define DTTR_BLEND_ALPHA 1
182#define DTTR_BLEND_ADDITIVE 2
183#define DTTR_PIPELINE_COUNT 12
184#define DTTR_PIPELINE_INDEX(bmode, dtest, dwrite) ((bmode) * 4 + (dtest) * 2 + (dwrite))
185
186#define DTTR_SAMPLER_COUNT 4
187#define DTTR_MAX_ANISOTROPY 16.0f
188#define DTTR_MAT4_SIZE (sizeof(float) * 16)
189#define DTTR_VERTEX_ATTRIBUTE_COUNT 4
190
191#define DTTR_MAX_FRAME_VERTICES 262144
192#define DTTR_VERTEX_SIZE ((uint32_t)sizeof(DTTR_Vertex))
193
195 SDL_ThreadID gpu_thread_id;
196
198 SDL_GPUDevice *device;
199 SDL_GPUShaderFormat shader_format;
200 SDL_GPUSampleCount msaa_sample_count;
201 SDL_GPUCommandBuffer *cmd;
202 SDL_GPUTexture *swapchain_tex;
203 SDL_GPURenderPass *render_pass;
204
205 SDL_GPUGraphicsPipeline *pipelines[DTTR_PIPELINE_COUNT];
206
207 SDL_GPUSampler *samplers[DTTR_SAMPLER_COUNT];
208 SDL_GPUTexture *dummy_texture;
209 SDL_GPUTexture *depth_texture;
210 SDL_GPUTexture *msaa_render_target;
211 SDL_GPUTexture *render_target;
212 SDL_GPUTexture *video_texture;
215 SDL_GPUBuffer *vertex_buffer;
216 SDL_GPUTransferBuffer *transfer_buffer;
219 int width;
223
224 SDL_FColor clear_color;
225 SDL_GPUTexture *bound_texture;
245 float proj[16];
246 float view[16];
247 float model[16];
248 DTTR_BatchRecordVector batch_records;
251 uint64_t frame_index;
252
256 SDL_Mutex *texture_mutex;
258
270
274
277};
278
280
281HWND dttr_graphics_init();
284SDL_GPUDevice *dttr_graphics_get_device();
285void dttr_graphics_handle_window_resize(int width, int height);
287 const uint8_t *pixels,
288 int width,
289 int height,
290 int stride
291);
292
295
300 bool transformed,
301 bool depth_test,
302 bool blend_enabled
303);
306 DTTR_Vertex *verts,
307 uint32_t count,
308 int logical_width,
309 int logical_height,
310 int render_width,
311 int render_height
312);
313
314#ifdef DTTR_MODS_ENABLED
320 uint32_t game_x,
321 uint32_t game_y,
322 uint32_t game_w,
323 uint32_t game_h,
324 bool imgui_frame_active,
325 bool overlay_rendered
326);
329 uint32_t game_x,
330 uint32_t game_y,
331 uint32_t game_w,
332 uint32_t game_h,
333 bool imgui_frame_active,
334 bool overlay_rendered
335);
344#else
346
348
350
353 uint32_t,
354 uint32_t,
355 uint32_t,
356 uint32_t,
357 bool,
358 bool
359) {}
360
363 uint32_t,
364 uint32_t,
365 uint32_t,
366 uint32_t,
367 bool,
368 bool
369) {}
370
372
374
376
378
380
382
384
386#endif
387
389 int dst_w,
390 int dst_h,
391 int src_w,
392 int src_h,
393 bool stretch,
394 bool integer_fit,
395 float fallback_scale
396);
397
400 const DTTR_PresentRect *present
401);
404 const DTTR_PresentRect *present,
405 bool overlay_rendered
406);
407
409
410int dttr_graphics_calc_mip_levels(int w, int h);
411void dttr_graphics_mat4_identity(float *m);
412
413const char *dttr_graphics_shader_format_name(SDL_GPUShaderFormat format);
414SDL_GPUShaderFormat dttr_graphics_requested_shader_formats();
415SDL_GPUShaderFormat dttr_graphics_select_shader_format(SDL_GPUShaderFormat formats);
416SDL_GPUShaderFormat dttr_graphics_shader_format_for_driver(const char *driver);
418 const char *driver,
419 SDL_GPUShaderFormat formats
420);
421
422void dttr_graphics_set_logical_resolution(int width, int height);
424
427
428#endif
DTTR_Graphics_COM_Direct3DDevice7 void *status DTTR_Graphics_COM_Direct3DDevice7 DWORD DWORD void DWORD DWORD f DTTR_Graphics_COM_Direct3DDevice7 DWORD void DWORD st
DTTR_Graphics_COM_Direct3DDevice7 void DWORD flags DWORD count
size_t stride
const DTTR_BackendState * state
const DTTR_PrimitiveType type
DTTR_Graphics_COM_DirectDraw7 *self DWORD DWORD h
DTTR_Graphics_COM_DirectDraw7 *self DWORD w
struct SDL_Window SDL_Window
Definition dttr_mods.h:19
SDL_Window * dttr_graphics_get_window()
Definition graphics.c:578
static void dttr_graphics_mod_device_destroying(DTTR_BackendState *)
void dttr_graphics_fill_mesh_seams(DTTR_Vertex *verts, uint32_t count, int logical_width, int logical_height, int render_width, int render_height)
Expands triangle-list vertices by a tiny physical-pixel amount to hide mesh cracks.
Definition util.c:280
void dttr_graphics_mat4_identity(float *m)
Definition util.c:116
#define DTTR_PIPELINE_COUNT
void dttr_graphics_set_logical_resolution(int width, int height)
Definition graphics.c:562
#define DTTR_SAMPLER_COUNT
DTTR_BlendFactor
@ DTTR_BLEND_DESTCOLOR
@ DTTR_BLEND_INVSRCALPHA
@ DTTR_BLEND_ZERO
@ DTTR_BLEND_SRCCOLOR
@ DTTR_BLEND_INVDESTCOLOR
@ DTTR_BLEND_SRCALPHA
@ DTTR_BLEND_ONE
@ DTTR_BLEND_DESTALPHA
@ DTTR_BLEND_INVDESTALPHA
@ DTTR_BLEND_INVSRCCOLOR
SDL_GPUDevice * dttr_graphics_get_device()
Definition graphics.c:583
SDL_GPUShaderFormat dttr_graphics_select_shader_format_for_driver(const char *driver, SDL_GPUShaderFormat formats)
Definition util.c:169
DTTR_CullMode
@ DTTR_CULL_CCW
@ DTTR_CULL_NONE
@ DTTR_CULL_CW
bool dttr_graphics_ensure_staged_texture(DTTR_BackendState *state, DTTR_StagedTexture *st)
Definition util.c:14
HWND dttr_graphics_init()
Definition graphics.c:482
typedef kvec_t(int) DTTR_IntVector
bool dttr_graphics_is_gpu_thread()
Definition util.c:182
bool dttr_graphics_opengl_init(DTTR_BackendState *state)
#define DTTR_UPLOAD_POOL_SIZE
SDL_GPUShaderFormat dttr_graphics_requested_shader_formats()
Definition util.c:136
static void dttr_graphics_mod_window_resized(DTTR_BackendState *)
DTTR_BackendType
@ DTTR_BACKEND_OPENGL
@ DTTR_BACKEND_SDL_GPU
DTTR_CompareFunc
@ DTTR_CMP_GREATER
@ DTTR_CMP_LESS
@ DTTR_CMP_LESSEQUAL
@ DTTR_CMP_EQUAL
@ DTTR_CMP_ALWAYS
@ DTTR_CMP_NOTEQUAL
@ DTTR_CMP_NEVER
@ DTTR_CMP_GREATEREQUAL
static void dttr_graphics_mod_device_lost(DTTR_BackendState *)
static void dttr_graphics_mod_before_game_frame(DTTR_BackendState *)
bool dttr_graphics_should_fill_mesh_seams(DTTR_PrimitiveType type, bool transformed, bool depth_test, bool blend_enabled)
Returns true when a draw call should receive subpixel logical-scaling seam fill.
Definition util.c:198
bool dttr_graphics_sdl3gpu_init(DTTR_BackendState *state)
uint32_t DTTR_Texture
void dttr_graphics_handle_window_resize(int width, int height)
Definition graphics.c:588
void dttr_graphics_surface_texture_cache_reset()
static void dttr_graphics_mod_device_restored(DTTR_BackendState *)
SDL_GPUShaderFormat dttr_graphics_select_shader_format(SDL_GPUShaderFormat formats)
Definition util.c:140
void dttr_graphics_mod_present_rect_before(DTTR_BackendState *state, const DTTR_PresentRect *present)
Definition graphics.c:424
void dttr_graphics_mod_present_rect_after(DTTR_BackendState *state, const DTTR_PresentRect *present, bool overlay_rendered)
Definition graphics.c:439
SDL_GPUShaderFormat dttr_graphics_shader_format_for_driver(const char *driver)
Definition util.c:152
DTTR_BatchRecordType
@ DTTR_BATCH_DRAW
@ DTTR_BATCH_CLEAR
static void dttr_graphics_mod_before_present(DTTR_BackendState *, uint32_t, uint32_t, uint32_t, uint32_t, bool, bool)
static void dttr_graphics_mod_device_created(DTTR_BackendState *)
DTTR_PresentRect dttr_graphics_compute_present_rect(int dst_w, int dst_h, int src_w, int src_h, bool stretch, bool integer_fit, float fallback_scale)
Definition util.c:48
DTTR_TextureAddress
@ DTTR_TEXADDR_CLAMP
@ DTTR_TEXADDR_WRAP
@ DTTR_TEXADDR_BORDER
@ DTTR_TEXADDR_MIRROR
static void dttr_graphics_mod_window_destroying(DTTR_BackendState *)
DTTR_BackendState dttr_backend
Definition util.c:8
static void dttr_graphics_mod_window_created(DTTR_BackendState *)
void dttr_graphics_begin_frame()
Definition graphics.c:599
const char * dttr_graphics_shader_format_name(SDL_GPUShaderFormat format)
Definition util.c:121
static void dttr_graphics_mod_after_present(DTTR_BackendState *, uint32_t, uint32_t, uint32_t, uint32_t, bool, bool)
static void dttr_graphics_mod_after_game_frame(DTTR_BackendState *)
static void dttr_graphics_mod_frame_begin(DTTR_BackendState *)
static void dttr_graphics_mod_frame_end(DTTR_BackendState *)
DTTR_PrimitiveType
@ DTTR_PRIM_POINTLIST
@ DTTR_PRIM_LINELIST
@ DTTR_PRIM_LINESTRIP
@ DTTR_PRIM_TRIANGLEFAN
@ DTTR_PRIM_TRIANGLELIST
@ DTTR_PRIM_TRIANGLESTRIP
#define DTTR_MAX_STAGED_TEXTURES
void dttr_graphics_cleanup()
Definition graphics.c:638
bool dttr_graphics_present_video_frame_bgra(const uint8_t *pixels, int width, int height, int stride)
Definition graphics.c:621
int dttr_graphics_calc_mip_levels(int w, int h)
Definition util.c:104
void dttr_graphics_end_frame()
Definition graphics.c:610
uint32_t perf_mips_skipped_accum
SDL_GPUShaderFormat shader_format
uint32_t perf_sampler_binds_accum
DTTR_UploadPoolSlot upload_pool[DTTR_UPLOAD_POOL_SIZE]
DTTR_Texture bound_texture_handle
uint32_t perf_frame_accum_count
SDL_GPUTexture * video_texture
uint64_t perf_upload_bytes_accum
SDL_GPUTexture * depth_texture
SDL_GPUTexture * render_target
SDL_GPUTexture * dummy_texture
SDL_GPUSampler * samplers[DTTR_SAMPLER_COUNT]
uint32_t perf_pipeline_binds_accum
SDL_GPUBuffer * vertex_buffer
SDL_GPUTexture * msaa_render_target
SDL_ThreadID gpu_thread_id
SDL_GPUTexture * swapchain_tex
DTTR_BackendType backend_type
SDL_GPUGraphicsPipeline * pipelines[DTTR_PIPELINE_COUNT]
SDL_GPUTransferBuffer * transfer_buffer
DTTR_StagedTexture staged_textures[DTTR_MAX_STAGED_TEXTURES]
uint32_t perf_upload_textures_accum
SDL_GPUDevice * device
DTTR_BatchRecordVector batch_records
SDL_GPURenderPass * render_pass
SDL_Mutex * texture_mutex
DTTR_TextureAddress addr_v
DTTR_IntVector pending_upload_indices
DTTR_TextureAddress addr_u
DTTR_BlendFactor blend_dst
SDL_GPUSampleCount msaa_sample_count
SDL_GPUCommandBuffer * cmd
const DTTR_RendererVtbl * renderer
SDL_GPUTexture * bound_texture
uint32_t perf_mips_generated_accum
A recorded clear or draw command replayed during frame submission.
SDL_GPUSampler * sampler
SDL_GPUTexture * texture
DTTR_Uniforms uniforms
DTTR_BatchRecordType type
Game-image placement within the present target, in target pixels.
Backend-specific operations dispatched through function pointers.
bool(* present_video_frame_bgra)(DTTR_BackendState *state, const uint8_t *pixels, int width, int height, int stride)
void(* cleanup)(DTTR_BackendState *state)
void(* end_frame)(DTTR_BackendState *state)
void(* begin_frame)(DTTR_BackendState *state)
bool(* resize)(DTTR_BackendState *state, int width, int height)
void(* defer_texture_destroy)(DTTR_BackendState *state, int texture_index)
SDL_GPUTexture * gpu_tex
One reusable upload slot holding a transfer buffer for texture uploads.
SDL_GPUTransferBuffer * transfer_buffer