summaryrefslogtreecommitdiff
path: root/utils/remoteshadercompile/remoteshadercompile.cpp
blob: 81438a1f235256bc51daaa3ca82d24edeb4d2478 (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Defines the entry point for the console application
//
//===============================================================================

//#include <sys/stat.h>
//#include <time.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <conio.h >
#include "tier1/utlvector.h"
#include "tier0/icommandline.h"
#include "tier2/tier2.h"
#include "tier2/fileutils.h"
#include "../../dx9sdk/include/d3d9.h"
#include "../../dx9sdk/include/d3dx9.h"

#define DEFAULT_PORT			"20000"
#define DEFAULT_SEND_BUF_LEN	40000
#define DEFAULT_RECV_BUF_LEN	40000

char g_pPathBase[MAX_PATH];
bool g_bPrintDisassembly;

// This guy just spins and compiles when a command comes in from the game
void ServerThread( void * )
{
	WSADATA wsaData;
	if( WSAStartup( 0x101, &wsaData ) != 0 )
		return;

	struct addrinfo *result = NULL, hints;

	ZeroMemory( &hints, sizeof(hints) );
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;
	hints.ai_flags = AI_PASSIVE;

	// Resolve the server address and port
	int nResult = getaddrinfo( NULL, DEFAULT_PORT, &hints, &result );
	if ( nResult != 0 )
	{
		printf( "getaddrinfo failed: %d\n", nResult );
		WSACleanup();
		return;
	}

	// Create a SOCKET for connecting to server
	SOCKET ListenSocket = socket( result->ai_family, result->ai_socktype, result->ai_protocol );
	if (ListenSocket == INVALID_SOCKET) {
		printf("socket failed: %ld\n", WSAGetLastError());
		freeaddrinfo(result);
		WSACleanup();
		return;
	}

	// Setup the TCP listening socket
	nResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen );
	if (nResult == SOCKET_ERROR)
	{
		printf("bind failed: %d\n", WSAGetLastError());
		freeaddrinfo(result);
		closesocket(ListenSocket);
		WSACleanup();
		return;
	}

	freeaddrinfo(result);


	nResult = listen( ListenSocket, SOMAXCONN );
	if ( nResult == SOCKET_ERROR )
	{
		printf( "listen failed: %d\n", WSAGetLastError() );
		closesocket( ListenSocket );
		WSACleanup();
		return;
	}

	printf( "Waiting for initial connection...\n" );

	// Accept a client socket
	SOCKET ClientSocket = accept( ListenSocket, NULL, NULL );
	if ( ClientSocket == INVALID_SOCKET )
	{
		printf( "accept failed: %d\n", WSAGetLastError() );
		closesocket( ListenSocket );
		WSACleanup();
		return;
	}

	// First connection
	printf( "Game connected\n" );

	char pRecbuf[DEFAULT_RECV_BUF_LEN];		// Text in command file from game
	uint32 pSendbuf[DEFAULT_SEND_BUF_LEN];	// Error string or binary shader blob in reply

	while ( true )
	{
		nResult = recv( ClientSocket, pRecbuf, DEFAULT_RECV_BUF_LEN, 0 );

		if ( nResult > 0 )
		{
			char *pShaderFilename = strtok ( pRecbuf, "\n");
			char pFullFilename[MAX_PATH];

			// If we took in a path on the commandline, we concatenate the incoming filenames with it
			if ( V_strlen( g_pPathBase ) > 0 )
			{
				V_strncpy( pFullFilename, g_pPathBase, MAX_PATH ); // base path
				V_strncat( pFullFilename, pShaderFilename, MAX_PATH );
				pShaderFilename = pFullFilename;
			}
			char *pShaderModel = strtok ( NULL, "\n");
			int nSendBufLen = 0;

			// Only try to compile if we have a recognized profile
			if ( !stricmp( pShaderModel, "vs_2_0" ) || !stricmp( pShaderModel, "ps_2_0" ) || !stricmp( pShaderModel, "ps_2_b" ) )
			{
				char *pNumMacros = strtok ( NULL, "\n");
				int nNumMacros = atoi( pNumMacros );

				// Read macros from the command file
				CUtlVector<D3DXMACRO> macros;
				D3DXMACRO macro;
				for ( int i=0; i<nNumMacros-1; i++ ) // The last one is the (null) one, so don't bother reading it
				{
					// Allocate and populate strings
					macro.Name = strtok( NULL, "\n");
					macro.Definition = strtok( NULL, "\n");
					macros.AddToTail( macro );
				}

				// Null macro at the end
				macro.Name = NULL;
				macro.Definition = NULL;
				macros.AddToTail( macro );

				LPD3DXBUFFER pShader, pErrorMessages;

				// This is the shader compiler we use for pre-ps30 shaders.
				// This utility needs to change if we want to do ps30 shaders (see logic in vertexshaderdx8.cpp)
				HRESULT hr = D3DXCompileShaderFromFile( pShaderFilename, macros.Base(), NULL /* LPD3DXINCLUDE */, "main",
														pShaderModel, 0, &pShader, &pErrorMessages,
														NULL /* LPD3DXCONSTANTTABLE *ppConstantTable */ );
				if ( hr != D3D_OK )
				{
					pSendbuf[0] = 0;
					
					printf( "Compilation error in %s\n", pShaderFilename );

					if ( pErrorMessages )
					{
						memcpy( pSendbuf+1, pErrorMessages->GetBufferPointer(), pErrorMessages->GetBufferSize() ); // Null-terminated string
						printf("%s\n", (const char*)(pSendbuf + 1 ));
						nSendBufLen = pErrorMessages->GetBufferSize() + 4; // account for uint32 at front of the buffer
					}
					else
					{
						((uint8*)(pSendbuf+1))[0] = '?';
						((uint8*)(pSendbuf+1))[1] = '\0';
						nSendBufLen = 2 + 4; // account for uint32 at front of the buffer
					}
					
					
				}
				else  // Success!
				{
//					printf( "Compiled %s\n", pShaderFilename );
					pSendbuf[0] = pShader->GetBufferSize();
					memcpy( pSendbuf+1, pShader->GetBufferPointer(), pShader->GetBufferSize() );
					nSendBufLen = pShader->GetBufferSize() + 4; // account for uint32 at front of the buffer

					if ( g_bPrintDisassembly )
					{
						printf( "Filename: %s\n", pShaderFilename );
						printf( "Shader model: %s\n", pShaderModel );
						printf( "Macros: " );
						for ( int i = 0; i < nNumMacros - 1; i++ )
							printf( "  %s\n", macros[i].Name );
						LPD3DXBUFFER pDisassembly = NULL;
						D3DXDisassembleShader( (const DWORD*)pShader->GetBufferPointer(), FALSE, "", &pDisassembly );
						if ( pDisassembly )
						{
							printf( "Disassembled shader:\n");
							puts( (const char*)pDisassembly->GetBufferPointer() );
							printf("\n");

							pDisassembly->Release();
						}
					}
				}

				if (pErrorMessages)
				{
					pErrorMessages->Release();
				}

				if (pShader)
				{
					pShader->Release();
				}

				// Purge the macro buffer
				macros.RemoveMultipleFromTail( nNumMacros );
			}
			else // Not a supported shader profile
			{
				pSendbuf[0] = 0;
				char *pCharSendbuff = (char *) (pSendbuf+1);
				V_snprintf( pCharSendbuff, DEFAULT_SEND_BUF_LEN, "Unsupported shader profile: %s\n", pShaderModel );
				nSendBufLen = strlen( pCharSendbuff ) + 4; // account for uint32 at front of the buffer
			}

			// Send the compiled shader back to the game
			int nSendResult = send( ClientSocket, (const char *)pSendbuf, nSendBufLen, 0 );
			if ( nSendResult == SOCKET_ERROR )
			{
				printf( "send failed: %d\n", WSAGetLastError() );
				closesocket( ClientSocket );
				WSACleanup();
				return;
			}
		}
		else // We had a game talking to us but it went away
		{
			printf( "Game went away, waiting for new connection...\n" );

			// Block again waiting to accept a connection
			ClientSocket = accept( ListenSocket, NULL, NULL );

			printf( "Game connected\n" );

			if ( ClientSocket == INVALID_SOCKET )
			{
				printf( "accept failed: %d\n", WSAGetLastError() );
				Assert( 0 );
				closesocket( ListenSocket );
				WSACleanup();
				return;
			}
		}
	}

	nResult = shutdown( ClientSocket, SD_SEND );
	if ( nResult == SOCKET_ERROR )
	{
		printf("shutdown failed: %d\n", WSAGetLastError());
		closesocket( ClientSocket );
		WSACleanup();
		return;
	}

	// cleanup
	closesocket( ClientSocket );
	WSACleanup();
}


void CheckPath( char *pPath )
{
	int len = V_strlen( pPath );

	// If we don't have a path separator at the end of the path, put one there
	if ( ( pPath[len-1] != '\\' ) && ( pPath[len-1] != '/' ) )
	{
		V_strncat( pPath, CORRECT_PATH_SEPARATOR_S, MAX_PATH );
	}
}


int main(int argc, char* argv[])
{
	if ( argc < 2 )
	{
		printf( "============================================================\n" );
		printf( " Please provide full path to shader directory.  For example:\n" );
		printf( "    U:\\piston\\staging\\src\\materialsystem\\stdshaders\\ \n");
		printf( "============================================================\n" );
		printf( " remoteshadercompiler will now exit!!! \n" );
		printf( "============================================================\n" );
		return 0;
	}

	printf( "========================================================\n");
	printf( "Remote shader compiler is running.  Press ESCAPE to exit\n" );
	printf( "========================================================\n");

	// If we have a path specified on the commandline, we expect
	// that the remote machine is going to send base filenames only
	// and that we'll want to strcat this path onto the filename from the worker.
	//
	// For example, if you have your shader source on your Windows machine, you can use something like this:
	//
	//   U:\piston\staging\src\materialsystem\stdshaders\
	//
	strcpy(	g_pPathBase, argv[1] );

	if ( argc == 3 )
	{
		g_bPrintDisassembly = true;
	}

	CheckPath( g_pPathBase );

	// Kick off compile server thread
	_beginthread( ServerThread, 0, NULL );

	// Spin until escape
	while( _getch() != 27 )
		;

	return 0;
}