first commit

This commit is contained in:
murdle
2026-03-01 02:38:58 +02:00
commit 19250b9db4
19111 changed files with 4358159 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,147 @@
#include "stdafx.h" // 4J
// gdraw_d3d11.cpp - author: Fabian Giesen - copyright 2011 RAD Game Tools
//
// This implements the Iggy graphics driver layer for D3D 11.
// GDraw consists of several components that interact fairly loosely with each other;
// e.g. the resource management, drawing and filtering parts are all fairly independent
// of each other. If you want to modify some aspect of GDraw - say the texture allocation
// logic - your best bet is usually to just look for one of the related entry points,
// e.g. MakeTextureBegin, and take it from there. There's a bunch of code in this file,
// but none of it is really complicated.
//
// The one bit you might want to change that's not that localized is to integrate
// GDraw with an existing state caching system. The following bits all modify D3D state
// in some way:
// - The rendering helpers (set_viewport_raw, set_projection_raw, set_*_renderstate)
// - RenderTile*/TextureDrawBuffer* may change the active rendertarget and depth/stencil surface,
// as do D3D1X_(NoMoreGDrawThisFrame) and set_render_target
// - set_texture
// - set_renderstate and set_renderstate_full. These are the main places where render state changes occur;
// you should probably start here.
// - DrawIndexedTriangles sets the active vertex/index buffers and vertex declaration
// - Most of the functions in the "filter effects" section modify D3D state, mostly
// pixel shader constants and textures
#define GDRAW_ASSERTS
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
// We temporarily disable this warning for the shared interface portions
#pragma warning (push)
#pragma warning (disable: 4201) // nonstandard extension used : nameless struct/union
#include <windows.h>
#include <d3d11_x.h> // 4J changed to use monolithic version
#include "gdraw.h"
#include "iggy.h"
#include <string.h>
#include <math.h>
#include "gdraw_d3d11.h"
#pragma warning (pop)
// Some macros to allow as much sharing between D3D10 and D3D11 code as possible.
#define D3D1X_(id) D3D11_##id
#define ID3D1X(id) ID3D11##id
#define gdraw_D3D1X_(id) gdraw_D3D11_##id
#define GDRAW_D3D1X_(id) GDRAW_D3D11_##id
typedef ID3D11Device ID3D1XDevice;
typedef ID3D11DeviceContext ID3D1XContext;
typedef F32 ViewCoord;
typedef gdraw_d3d11_resourcetype gdraw_resourcetype;
static void report_d3d_error(HRESULT hr, char *call, char *context);
static void *map_buffer(ID3D1XContext *ctx, ID3D11Buffer *buf, bool discard)
{
D3D11_MAPPED_SUBRESOURCE msr;
HRESULT hr = ctx->Map(buf, 0, discard ? D3D11_MAP_WRITE_DISCARD : D3D11_MAP_WRITE_NO_OVERWRITE, 0, &msr);
if (FAILED(hr)) {
report_d3d_error(hr, "Map", "of buffer");
return NULL;
} else
return msr.pData;
}
static void unmap_buffer(ID3D1XContext *ctx, ID3D11Buffer *buf)
{
ctx->Unmap(buf, 0);
}
static RADINLINE void set_pixel_shader(ID3D11DeviceContext *ctx, ID3D11PixelShader *shader)
{
ctx->PSSetShader(shader, NULL, 0);
}
static RADINLINE void set_vertex_shader(ID3D11DeviceContext *ctx, ID3D11VertexShader *shader)
{
ctx->VSSetShader(shader, NULL, 0);
}
static ID3D11BlendState *create_blend_state(ID3D11Device *dev, BOOL blend, D3D11_BLEND src, D3D11_BLEND dst)
{
D3D11_BLEND_DESC desc = {};
desc.RenderTarget[0].BlendEnable = blend;
desc.RenderTarget[0].SrcBlend = src;
desc.RenderTarget[0].DestBlend = dst;
desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].SrcBlendAlpha = (src == D3D11_BLEND_DEST_COLOR ) ? D3D11_BLEND_DEST_ALPHA : src;
desc.RenderTarget[0].DestBlendAlpha = dst;
desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
ID3D11BlendState *res;
HRESULT hr = dev->CreateBlendState(&desc, &res);
if (FAILED(hr)) {
report_d3d_error(hr, "CreateBlendState", "");
res = NULL;
}
return res;
}
#define GDRAW_SHADER_FILE "gdraw_d3d10_shaders.inl"
#include "gdraw_d3d1x_shared.inl"
static void create_pixel_shader(ProgramWithCachedVariableLocations *p, ProgramWithCachedVariableLocations *src)
{
*p = *src;
if(p->bytecode) {
HRESULT hr = gdraw->d3d_device->CreatePixelShader(p->bytecode, p->size, NULL, &p->pshader);
if (FAILED(hr)) {
report_d3d_error(hr, "CreatePixelShader", "");
p->pshader = NULL;
return;
}
}
}
static void create_vertex_shader(ProgramWithCachedVariableLocations *p, ProgramWithCachedVariableLocations *src)
{
*p = *src;
if(p->bytecode) {
HRESULT hr = gdraw->d3d_device->CreateVertexShader(p->bytecode, p->size, NULL, &p->vshader);
if (FAILED(hr)) {
report_d3d_error(hr, "CreateVertexShader", "");
p->vshader = NULL;
return;
}
}
}
GDrawFunctions *gdraw_D3D11_CreateContext(ID3D11Device *dev, ID3D11DeviceContext *ctx, S32 w, S32 h)
{
return create_context(dev, ctx, w, h);
}
// 4J added - interface so we can set the viewport back to the one that Iggy last set up
void gdraw_D3D11_setViewport_4J()
{
set_viewport();
}

View File

@@ -0,0 +1,137 @@
#pragma once
// gdraw_d3d11.h - author: Fabian Giesen - copyright 2011 RAD Game Tools
//
// Interface for creating a D3D11 GDraw driver.
#define IDOC
//idoc(parent,GDraw_d3d11)
typedef enum gdraw_d3d11_resourcetype
{
GDRAW_D3D11_RESOURCE_rendertarget,
GDRAW_D3D11_RESOURCE_texture,
GDRAW_D3D11_RESOURCE_vertexbuffer,
GDRAW_D3D11_RESOURCE_dynbuffer, // Streaming buffer for dynamic vertex/index data (handle count ignored)
GDRAW_D3D11_RESOURCE__count,
} gdraw_d3d11_resourcetype;
IDOC extern int gdraw_D3D11_SetResourceLimits(gdraw_d3d11_resourcetype type, S32 num_handles, S32 num_bytes);
/* This sets how large the memory pool for a given resource types is, and how many handles
GDraw should allocate for it. GDraw keeps track of allocations in each pool, and will free
old resources in a LRU manner to make space if one of the limits is about to be exceeded.
Returns 1 if value successfully changed, 0 on error.
You need to call IggyPlayerFlushAll on all active Iggys before you do this to make
them flush their resources since changing the resource limits invalidates all handles.
You also need to call IggyFlushInstalledFonts if you have any installed fonts.
*/
IDOC extern GDrawFunctions * gdraw_D3D11_CreateContext(ID3D11Device *dev, ID3D11DeviceContext *ctx, S32 w, S32 h);
/* Creates a GDraw context for rendering using D3D. You need to pass in the D3D device,
the device context to use for rendering, and the width/height of render target textures.
The width/height is used solely for sizing internal rendertargets. They will be
allocated to the larger of this size and the size of any rendered tiles (with padding).
In other words, you can pass in (0,0) and the rendertargets will be allocated to the
right size. However, if you draw multiple Iggy files or tiles of different sizes,
they might first be allocated too small; it's best to pass in the correct size initially
to avoid unnecessary allocation/deallocation of too-small rendertargets.
There can only be one D3D GDraw context active at any one time.
If initialization fails for some reason (the main reason would be an out of memory condition),
NULL is returned. Otherwise, you can pass the return value to IggySetGDraw. */
IDOC extern void gdraw_D3D11_DestroyContext(void);
/* Destroys the current GDraw context, if any. */
IDOC extern void gdraw_D3D11_SetErrorHandler(void (__cdecl *error_handler)(HRESULT hr));
/* Sets the GDraw D3D error handler.
This will get called with the respective D3D error code if GDraw encounters an error
that it can't handle by itself (e.g. running out of state objects). */
IDOC extern void gdraw_D3D11_SetRendertargetSize(S32 w, S32 h);
/* Changes the current render target size (and recreates all rendertargets if necessary).
This allows you to shrink the rendertargets if the new needed size is smaller
than it was previously. As with $gdraw_D3D11_CreateContext, the width and
height specified here are only minimums; GDraw will reallocate larger rendertargets
as needed. */
IDOC extern void gdraw_D3D11_SetTileOrigin(ID3D11RenderTargetView *main_rt, ID3D11DepthStencilView *main_ds,
ID3D11ShaderResourceView *non_msaa_rt, S32 x, S32 y);
/* This sets the main rendertarget and matching depth/stencil buffer that GDraw
should render to and the x/y position of the output location of the top-left
of the current tile (allowing you to finely-position content, or to do tiled
rendering).
If your rendertarget uses multisampling, you also need to specify a shader
resource view for a non-MSAA rendertarget texture (identically sized to main_rt)
in non_msaa_rt. This is only used if the Flash content includes non-standard
blend modes which have to use a special blend shader, so you can leave it NULL
if you forbid such content.
You need to call this before Iggy calls any rendering functions. */
IDOC extern void gdraw_D3D11_NoMoreGDrawThisFrame(void);
/* Tells GDraw that no more rendering operations will occur this frame. This triggers
some end-of-frame processing; most importantly, GDraw uses this call as a marker to
detect thrashing (and react accordingly), so please do not forget to call this
every frame! (As long as Iggy does any rendering, that is) */
IDOC extern void gdraw_D3D11_PreReset(void);
/* Call this before D3D device Reset(); it will free all default pool resources allocated
by GDraw. */
IDOC extern void gdraw_D3D11_PostReset(void);
/* Call after D3D device Reset(). */
IDOC extern void RADLINK gdraw_D3D11_BeginCustomDraw_4J(IggyCustomDrawCallbackRegion *Region, F32 mat[16]);
IDOC extern void RADLINK gdraw_D3D11_CalculateCustomDraw_4J(IggyCustomDrawCallbackRegion *Region, F32 mat[16]);
IDOC extern void RADLINK gdraw_D3D11_BeginCustomDraw(IggyCustomDrawCallbackRegion *Region, F32 mat[4][4]);
/* Call at the beginning of Iggy custom draw callback to clear any odd render states GDraw has
set on the D3D device, and to get the current 2D object-to-world transformation. */
IDOC extern void RADLINK gdraw_D3D11_EndCustomDraw(IggyCustomDrawCallbackRegion *Region);
/* Call at the end of Iggy custom draw callback so GDraw can restore its render states. */
IDOC extern void RADLINK gdraw_D3D11_GetResourceUsageStats(gdraw_d3d11_resourcetype type, S32 *handles_used, S32 *bytes_used);
/* D3D only: Get resource usage stats for last frame.
This can be used to get an estimate of how much graphics memory got used by GDraw
during the last frame.
For the dynbuffer, this always returns 0 in handles_used and the *size of the largest
single allocation* in bytes_used. It needs to be sized so that this allocation fits;
make it smaller and it won't work, but if you make it much larger (say more than 2x
as big), it's just a waste of memory. That said, we still recommend to make it no
smaller than 64k, and the default is 256k.
Caveat: This counts the number of bytes that GDraw knows about. 3D hardware usually
has its own management overhead, alignment requirements, allocation granularity
and so on. In short, this is not an accurate estimate of how much memory is actually
used by the GPU - it is a lower bound, though, and makes for a useful ballpark estimate. */
IDOC extern GDrawTexture *gdraw_D3D11_WrappedTextureCreate(ID3D11ShaderResourceView *tex_view);
/* Create a wrapped texture from a shader resource view.
A wrapped texture can be used to let Iggy draw using the contents of a texture
you create and manage on your own. For example, you might render to this texture,
or stream video into it. Wrapped textures take up a handle. They will never be
freed or otherwise modified by GDraw; nor will GDraw change any reference counts.
All this is up to the application. */
IDOC extern void gdraw_D3D11_WrappedTextureChange(GDrawTexture *tex, ID3D11ShaderResourceView *tex_view);
/* Switch an existing GDrawTexture * that represents a wrapped texture to use
a new underlying D3D view. For example, you might internally double-buffer
a dynamically updated texture. As above, GDraw will leave this texture alone
and not touch any reference counts. */
IDOC extern void gdraw_D3D11_WrappedTextureDestroy(GDrawTexture *tex);
/* Destroys the GDraw wrapper for a wrapped texture object. This will free up
a GDraw texture handle but not release the associated D3D texture; that is
up to you. */
GDrawTexture * RADLINK gdraw_D3D11_MakeTextureFromResource(U8 *resource_file, S32 length, IggyFileTextureRaw *texture);
void RADLINK gdraw_D3D11_DestroyTextureFromResource(GDrawTexture *tex);
// 4J added
extern void RADLINK gdraw_D3D11_setViewport_4J();

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff