1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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 nullptr;
} 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, nullptr, 0);
}
static RADINLINE void set_vertex_shader(ID3D11DeviceContext *ctx, ID3D11VertexShader *shader)
{
ctx->VSSetShader(shader, nullptr, 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 = nullptr;
}
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, nullptr, &p->pshader);
if (FAILED(hr)) {
report_d3d_error(hr, "CreatePixelShader", "");
p->pshader = nullptr;
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, nullptr, &p->vshader);
if (FAILED(hr)) {
report_d3d_error(hr, "CreateVertexShader", "");
p->vshader = nullptr;
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();
}
|