summaryrefslogtreecommitdiffstats
path: root/src/rw/RwPS2AlphaTest.cpp
blob: c0d683552f48500a9e789d8a9a4d833f44e5039d (plain) (blame)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#ifndef LIBRW

#define WITHD3D
#include "common.h"
#ifdef PS2_ALPHA_TEST
#include "rwcore.h"

extern "C" {
RwBool _rwD3D8RenderStateIsVertexAlphaEnable(void);
RwBool _rwD3D8RenderStateVertexAlphaEnable(RwBool enable);
RwRaster *_rwD3D8RWGetRasterStage(RwUInt32 stage);
}

extern bool gPS2alphaTest;

void
_rxD3D8DualPassRenderCallback(RwResEntry *repEntry, void *object, RwUInt8 type, RwUInt32 flags)
{
	RxD3D8ResEntryHeader *resEntryHeader;
	RxD3D8InstanceData *instancedData;
	RwInt32 numMeshes;
	RwBool lighting;
	RwBool vertexAlphaBlend;
	RwBool forceBlack;
	RwUInt32 ditherEnable;
	RwUInt32 shadeMode;
	void *lastVertexBuffer;

	/* Get lighting state */
	RwD3D8GetRenderState(D3DRS_LIGHTING, &lighting);

	forceBlack = FALSE;

	if (lighting) {
		if (flags & rxGEOMETRY_PRELIT) {
			/* Emmisive color from the vertex colors */
			RwD3D8SetRenderState(D3DRS_COLORVERTEX, TRUE);
			RwD3D8SetRenderState(D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_COLOR1);
		} else {
			/* Emmisive color from material, set to black in the submit node */
			RwD3D8SetRenderState(D3DRS_COLORVERTEX, FALSE);
			RwD3D8SetRenderState(D3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_MATERIAL);
		}
	} else {
		if ((flags & rxGEOMETRY_PRELIT) == 0) {
			forceBlack = TRUE;

			RwD3D8GetRenderState(D3DRS_DITHERENABLE, &ditherEnable);
			RwD3D8GetRenderState(D3DRS_SHADEMODE, &shadeMode);

			RwD3D8SetRenderState(D3DRS_TEXTUREFACTOR, 0xff000000);
			RwD3D8SetRenderState(D3DRS_DITHERENABLE, FALSE);
			RwD3D8SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT);
		}
	}

	/* Enable clipping */
	if (type == rpATOMIC) {
		RpAtomic *atomic;
		RwCamera *cam;

		atomic = (RpAtomic *)object;

		cam = RwCameraGetCurrentCamera();
		// RWASSERT(cam);

		if (RwD3D8CameraIsSphereFullyInsideFrustum(cam, RpAtomicGetWorldBoundingSphere(atomic))) {
			RwD3D8SetRenderState(D3DRS_CLIPPING, FALSE);
		} else {
			RwD3D8SetRenderState(D3DRS_CLIPPING, TRUE);
		}
	} else {
		RpWorldSector *worldSector;
		RwCamera *cam;

		worldSector = (RpWorldSector *)object;

		cam = RwCameraGetCurrentCamera();
		// RWASSERT(cam);

		if (RwD3D8CameraIsBBoxFullyInsideFrustum(cam, RpWorldSectorGetTightBBox(worldSector))) {
			RwD3D8SetRenderState(D3DRS_CLIPPING, FALSE);
		} else {
			RwD3D8SetRenderState(D3DRS_CLIPPING, TRUE);
		}
	}

	/* Set texture to NULL if hasn't any texture flags */
	if ((flags & (rxGEOMETRY_TEXTURED | rpGEOMETRYTEXTURED2)) == 0) {
		RwD3D8SetTexture(NULL, 0);

		if (forceBlack) {
			RwD3D8SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
			RwD3D8SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_TFACTOR);

			RwD3D8SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
		}
	}

	/* Get vertex alpha Blend state */
	vertexAlphaBlend = _rwD3D8RenderStateIsVertexAlphaEnable();

	/* Set Last vertex buffer to force the call */
	lastVertexBuffer = (void *)0xffffffff;

	/* Get the instanced data */
	resEntryHeader = (RxD3D8ResEntryHeader *)(repEntry + 1);
	instancedData = (RxD3D8InstanceData *)(resEntryHeader + 1);

	/*
	 * Data shared between meshes
	 */

	/*
	 * Set the Default Pixel shader
	 */
	RwD3D8SetPixelShader(0);

	/*
	 * Vertex shader
	 */
	RwD3D8SetVertexShader(instancedData->vertexShader);

	/* Get the number of meshes */
	numMeshes = resEntryHeader->numMeshes;
	while (numMeshes--) {
		// RWASSERT(instancedData->material != NULL);

		if ((flags & (rxGEOMETRY_TEXTURED | rpGEOMETRYTEXTURED2))) {
			RwD3D8SetTexture(instancedData->material->texture, 0);

			if (forceBlack) {
				/* Only change the colorop, we need to use the texture alpha channel */
				RwD3D8SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
				RwD3D8SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_TFACTOR);
			}
		}

		if (instancedData->vertexAlpha || (0xFF != instancedData->material->color.alpha)) {
			if (!vertexAlphaBlend) {
				vertexAlphaBlend = TRUE;

				_rwD3D8RenderStateVertexAlphaEnable(TRUE);
			}
		} else {
			if (vertexAlphaBlend) {
				vertexAlphaBlend = FALSE;

				_rwD3D8RenderStateVertexAlphaEnable(FALSE);
			}
		}

		if (lighting) {
			if (instancedData->vertexAlpha) {
				RwD3D8SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1);
			} else {
				RwD3D8SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL);
			}

			RwD3D8SetSurfaceProperties(&instancedData->material->color, &instancedData->material->surfaceProps, (flags & rxGEOMETRY_MODULATE));
		}

		/*
		 * Render
		 */

		/* Set the stream source */
		if (lastVertexBuffer != instancedData->vertexBuffer) {
			RwD3D8SetStreamSource(0, instancedData->vertexBuffer, instancedData->stride);

			lastVertexBuffer = instancedData->vertexBuffer;
		}
		if (!gPS2alphaTest) {
			/* Set the Index buffer */
			if (instancedData->indexBuffer != NULL) {
				RwD3D8SetIndices(instancedData->indexBuffer, instancedData->baseIndex);

				/* Draw the indexed primitive */
				RwD3D8DrawIndexedPrimitive((D3DPRIMITIVETYPE)instancedData->primType, 0, instancedData->numVertices, 0, instancedData->numIndices);
			} else {
				RwD3D8DrawPrimitive((D3DPRIMITIVETYPE)instancedData->primType, instancedData->baseIndex, instancedData->numVertices);
			}
		} else {
			RwD3D8SetIndices(instancedData->indexBuffer, instancedData->baseIndex);

			int hasAlpha, alphafunc, alpharef, zwrite;
			RwD3D8GetRenderState(D3DRS_ALPHABLENDENABLE, &hasAlpha);
			RwD3D8GetRenderState(D3DRS_ZWRITEENABLE, &zwrite);
			if (hasAlpha && zwrite) {
				RwD3D8GetRenderState(D3DRS_ALPHAFUNC, &alphafunc);
				RwD3D8GetRenderState(D3DRS_ALPHAREF, &alpharef);

				RwD3D8SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);
				RwD3D8SetRenderState(D3DRS_ALPHAREF, 128);

				if (instancedData->indexBuffer)
					RwD3D8DrawIndexedPrimitive(instancedData->primType, 0, instancedData->numVertices, 0, instancedData->numIndices);
				else
					RwD3D8DrawPrimitive(instancedData->primType, instancedData->baseIndex, instancedData->numVertices);

				RwD3D8SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_LESS);
				RwRenderStateSet(rwRENDERSTATEZWRITEENABLE, FALSE);

				if (instancedData->indexBuffer)
					RwD3D8DrawIndexedPrimitive(instancedData->primType, 0, instancedData->numVertices, 0, instancedData->numIndices);
				else
					RwD3D8DrawPrimitive(instancedData->primType, instancedData->baseIndex, instancedData->numVertices);

				RwD3D8SetRenderState(D3DRS_ALPHAFUNC, alphafunc);
				RwD3D8SetRenderState(D3DRS_ALPHAREF, alpharef);
				RwRenderStateSet(rwRENDERSTATEZWRITEENABLE, (void *)TRUE);
			} else {
				if (instancedData->indexBuffer)
					RwD3D8DrawIndexedPrimitive(instancedData->primType, 0, instancedData->numVertices, 0, instancedData->numIndices);
				else
					RwD3D8DrawPrimitive(instancedData->primType, instancedData->baseIndex, instancedData->numVertices);
			}
		}

		/* Move onto the next instancedData */
		instancedData++;
	}

	if (forceBlack) {
		RwD3D8SetRenderState(D3DRS_DITHERENABLE, ditherEnable);
		RwD3D8SetRenderState(D3DRS_SHADEMODE, shadeMode);

		if (_rwD3D8RWGetRasterStage(0)) {
			RwD3D8SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
			RwD3D8SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
		} else {
			RwD3D8SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
			RwD3D8SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
		}
	}
}

void
ReplaceAtomicPipeCallback()
{
	RxD3D8AllInOneSetRenderCallBack(RxPipelineFindNodeByName(RXPIPELINEGLOBAL(platformAtomicPipeline), RxNodeDefinitionGetD3D8AtomicAllInOne()->name, nil, nil),
	                                _rxD3D8DualPassRenderCallback);
}

#endif // PS2_ALPHA_TEST

#endif // !LIBRW