-
Notifications
You must be signed in to change notification settings - Fork 226
/
CustomEventProvider.cpp
324 lines (242 loc) · 7.7 KB
/
CustomEventProvider.cpp
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
317
318
319
320
321
322
323
324
//--------------------------------------------------------------------------------------
// CustomEventProvider.cpp
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "CustomEventProvider.h"
#include "EtwScopedEvent.h"
#include "ATGColors.h"
#include "ReadData.h"
extern void ExitSample() noexcept;
using namespace DirectX;
using namespace DirectX::SimpleMath;
using Microsoft::WRL::ComPtr;
// Need to define the sequence number used to generate unique event IDs.
UINT32 EtwScopedEvent::g_Sequence = 0;
Sample::Sample() :
m_frame(0)
{
// Renders only 2D, so no need for a depth buffer.
m_deviceResources = std::make_unique<DX::DeviceResources>(DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_UNKNOWN);
}
// Initialize the Direct3D resources required to run.
void Sample::Initialize(IUnknown* window)
{
// Register the CEP_Main event provider
EventRegisterCEP_Main();
m_gamePad = std::make_unique<GamePad>();
m_deviceResources->SetWindow(window);
m_deviceResources->CreateDeviceResources();
CreateDeviceDependentResources();
m_deviceResources->CreateWindowSizeDependentResources();
CreateWindowSizeDependentResources();
// Sample code
m_stressThread = nullptr;
m_threadCount = 0;
m_quitStress = FALSE;
// Lock us to Core 0
SetThreadAffinityMask(GetCurrentThread(), DWORD_PTR(1 << 0));
// Create a work thread and lock to Core 1
m_stressThread = CreateThread(nullptr, 0, (LPTHREAD_START_ROUTINE)Sample::StressThreadEntry, (LPVOID)this, 0, nullptr);
DX::ThrowIfFailed((m_stressThread == nullptr) ? HRESULT_FROM_WIN32(GetLastError()) : S_OK);
SetThreadAffinityMask(m_stressThread, DWORD_PTR(1 << 1));
CloseHandle(m_stressThread);
// Spin until stress thread activates
while (m_threadCount < 1)
{
Sleep(10);
}
}
//--------------------------------------------------------------------------------------
// Name: StressThread
// Desc: Code we can run on another core to simulate doing some real work.
//--------------------------------------------------------------------------------------
DWORD WINAPI Sample::StressThreadEntry( LPVOID lpParam )
{
assert( lpParam != nullptr );
( (Sample*)lpParam )->StressThread();
return 0;
}
void Sample::StressThread()
{
m_threadCount.fetch_add(1);
while( !m_quitStress )
{
{
ETWScopedEvent( L"StressSleep" );
Sleep( 1 );
}
Load1();
}
m_threadCount.fetch_sub(1);
}
void Sample::Load1()
{
ETWScopedEvent( L"Load1" );
volatile UINT64 sum = 0;
for ( UINT64 i = 0; i < 1000000; ++i )
{
sum = sum + 1;
}
Load2();
}
void Sample::Load2()
{
ETWScopedEvent( L"Load2" );
volatile UINT64 sum = 0;
for ( UINT64 i = 0; i < 1000000; ++i )
{
sum = sum + 1;
}
}
#pragma region Frame Update
// Executes basic render loop.
void Sample::Tick()
{
PIXBeginEvent(PIX_COLOR_DEFAULT, L"Frame %llu", m_frame);
m_timer.Tick([&]()
{
Update(m_timer);
});
Render();
PIXEndEvent();
m_frame++;
}
// Updates the world.
void Sample::Update(DX::StepTimer const& /*timer*/)
{
PIXBeginEvent(PIX_COLOR_DEFAULT, L"Update");
ETWScopedEvent(L"Update");
EventWriteMark(L"Sample::Update");
Child1();
auto pad = m_gamePad->GetState(0);
if (pad.IsConnected())
{
m_gamePadButtons.Update(pad);
if (pad.IsViewPressed())
{
ExitSample();
}
}
else
{
m_gamePadButtons.Reset();
}
PIXEndEvent();
}
#pragma endregion
//--------------------------------------------------------------------------------------
// Name: Child1()
// Desc: Simulated CPU load.
//--------------------------------------------------------------------------------------
void Sample::Child1()
{
ETWScopedEvent(L"Child1");
Sleep(1);
Child2();
Sleep(2);
}
//--------------------------------------------------------------------------------------
// Name: Child2()
// Desc: Simulated nested CPU load.
//--------------------------------------------------------------------------------------
void Sample::Child2()
{
ETWScopedEvent(L"Child2");
Sleep(3);
}
#pragma region Frame Render
// Draws the scene.
void Sample::Render()
{
// Don't try to render anything before the first Update.
if (m_timer.GetFrameCount() == 0)
{
return;
}
ETWScopedEvent(L"Render");
EventWriteMark(L"Sample::Render");
Child1();
// Prepare the command list to render a new frame.
m_deviceResources->Prepare();
Clear();
auto context = m_deviceResources->GetD3DDeviceContext();
PIXBeginEvent(context, PIX_COLOR_DEFAULT, L"Render");
context->OMSetBlendState(m_states->Opaque(), nullptr, 0xFFFFFFFF);
context->OMSetDepthStencilState(m_states->DepthNone(), 0);
context->RSSetState(m_states->CullNone());
m_effect->Apply(context);
context->IASetInputLayout(m_inputLayout.Get());
m_batch->Begin();
VertexPositionColor v1(Vector3(0.f, 0.5f, 0.5f), Colors::Yellow);
VertexPositionColor v2(Vector3(0.5f, -0.5f, 0.5f), Colors::Yellow);
VertexPositionColor v3(Vector3(-0.5f, -0.5f, 0.5f), Colors::Yellow);
m_batch->DrawTriangle(v1, v2, v3);
m_batch->End();
PIXEndEvent(context);
// Synthesize some data
UINT32 syntheticData = rand();
EventWriteBlockCulled(syntheticData);
// Show the new frame.
PIXBeginEvent(PIX_COLOR_DEFAULT, L"Present");
m_deviceResources->Present();
m_graphicsMemory->Commit();
PIXEndEvent();
}
// Helper method to clear the back buffers.
void Sample::Clear()
{
auto context = m_deviceResources->GetD3DDeviceContext();
PIXBeginEvent(context, PIX_COLOR_DEFAULT, L"Clear");
// Clear the views.
auto renderTarget = m_deviceResources->GetRenderTargetView();
context->ClearRenderTargetView(renderTarget, ATG::Colors::Background);
context->OMSetRenderTargets(1, &renderTarget, nullptr);
// Set the viewport.
auto viewport = m_deviceResources->GetScreenViewport();
context->RSSetViewports(1, &viewport);
PIXEndEvent(context);
}
#pragma endregion
#pragma region Message Handlers
// Message handlers
void Sample::OnSuspending()
{
auto context = m_deviceResources->GetD3DDeviceContext();
context->Suspend(0);
}
void Sample::OnResuming()
{
auto context = m_deviceResources->GetD3DDeviceContext();
context->Resume();
m_timer.ResetElapsedTime();
m_gamePadButtons.Reset();
}
#pragma endregion
#pragma region Direct3D Resources
// These are the resources that depend on the device.
void Sample::CreateDeviceDependentResources()
{
auto device = m_deviceResources->GetD3DDevice();
auto context = m_deviceResources->GetD3DDeviceContext();
m_graphicsMemory = std::make_unique<GraphicsMemory>(device, m_deviceResources->GetBackBufferCount());
m_states = std::make_unique<CommonStates>(device);
m_effect = std::make_unique<BasicEffect>(device);
m_effect->SetVertexColorEnabled(true);
void const* shaderByteCode;
size_t byteCodeLength;
m_effect->GetVertexShaderBytecode(&shaderByteCode, &byteCodeLength);
DX::ThrowIfFailed(
device->CreateInputLayout(VertexPositionColor::InputElements,
VertexPositionColor::InputElementCount,
shaderByteCode, byteCodeLength,
m_inputLayout.ReleaseAndGetAddressOf()));
m_batch = std::make_unique<PrimitiveBatch<VertexPositionColor>>(context);
}
// Allocate all memory resources that change on a window SizeChanged event.
void Sample::CreateWindowSizeDependentResources()
{
}
#pragma endregion