Skip to content

Instantly share code, notes, and snippets.

@jrmuizel
Created January 28, 2021 16:33
Show Gist options
  • Save jrmuizel/9e57b0eccc664cb53acc0997a0640b73 to your computer and use it in GitHub Desktop.
Save jrmuizel/9e57b0eccc664cb53acc0997a0640b73 to your computer and use it in GitHub Desktop.
copy region scatter bench
#pragma comment(lib, "d3d11.lib")
#include <d3d11.h>
#include <stdio.h>
#include <windows.h>
#include <assert.h>
#define WIDTH 8192
#define HEIGHT 8192
int buf[WIDTH * HEIGHT];
int buf2[WIDTH * HEIGHT];
void delupms(void *b, size_t length)
{
int sum = 0;
char *buf = (char *)b;
char *end = buf + length;
while (buf < end)
{
sum += *buf;
buf += 0x1000;
}
printf("sum: %d\n", sum);
}
double ticksToMS;
int main(int argc, char **argv)
{
int size;
ID3D11Device *device;
ID3D11DeviceContext *context;
D3D_FEATURE_LEVEL level;
int creation_flags = 0; // D3D11_CREATE_DEVICE_DEBUG;
D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL,
creation_flags,
NULL, 0, D3D11_SDK_VERSION, &device,
&level, &context);
printf("%p\n", device);
if (argc > 1)
size = strtol(argv[1], 0, NULL);
else
size = D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION;
int size_low = 0;
int trial_size;
HRESULT result;
size_t byte_size = sizeof(buf);
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
printf("frequency %lld\n", freq.QuadPart);
ticksToMS = 1000. / freq.QuadPart;
trial_size = size;
result = 0x800000;
D3D11_TEXTURE2D_DESC desc;
desc.Width = WIDTH;
desc.Height = HEIGHT;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.CPUAccessFlags = 0;
desc.MiscFlags = 0;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
{
LARGE_INTEGER before;
LARGE_INTEGER after;
LONGLONG time;
ID3D11Texture2D *src_texture;
D3D11_TEXTURE2D_DESC small_desc = desc;
//small_desc.Height = 1;
result = device->CreateTexture2D(&small_desc, NULL, &src_texture);
assert(result == S_OK);
QueryPerformanceCounter(&before);
D3D11_BOX box;
box.left = 0;
box.right = WIDTH;
box.top = 0;
box.bottom = 1;
box.front = 0;
box.back = 1;
printf("before updatesubresource time\n");
context->UpdateSubresource(src_texture, 0, &box, buf, WIDTH * 4, sizeof(buf));
QueryPerformanceCounter(&after);
time = after.QuadPart - before.QuadPart;
printf("%x updatesubresource time: %.1fms\n", result, time * ticksToMS);
ID3D11Texture2D *dst_texture;
result = device->CreateTexture2D(&desc, NULL, &dst_texture);
while (true) {
D3D11_QUERY_DESC query_desc = {};
query_desc.Query = D3D11_QUERY_TIMESTAMP_DISJOINT;
ID3D11Query *query;
device->CreateQuery(&query_desc, &query);
D3D11_QUERY_DESC time_query_desc = {};
time_query_desc.Query = D3D11_QUERY_TIMESTAMP;
ID3D11Query *time_before_query;
device->CreateQuery(&time_query_desc, &time_before_query);
ID3D11Query *time_after_query;
device->CreateQuery(&time_query_desc, &time_after_query);
context->Begin(query);
context->Begin(time_before_query);
context->End(time_before_query);
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
D3D11_BOX box;
box.left = 0;
box.right = 1;
box.top = 0;
box.bottom = 1;
box.bottom = 1;
box.front = 0;
box.back = 1;
context->CopySubresourceRegion(dst_texture, x, y, 0, 0, src_texture, 0, &box);
}
}
context->Begin(time_after_query);
context->End(time_after_query);
context->End(query);
D3D11_QUERY_DATA_TIMESTAMP_DISJOINT data;
do
{
result = context->GetData(query, &data, sizeof(data), 0);
} while (result != 0);
UINT64 time_before;
UINT64 time_after;
do
{
result = context->GetData(time_after_query, &time_after, sizeof(time_after), 0);
} while (result != 0);
QueryPerformanceCounter(&after);
do
{
result = context->GetData(time_before_query, &time_before, sizeof(time_before), 0);
} while (result != 0);
assert(!data.Disjoint);
printf("copy time: %fms\n", (time_after - time_before) * 1000. / data.Frequency);
time_before_query->Release();
time_after_query->Release();
query->Release();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment