Direct Compute


Direct Compute

Microsoft Direct Compute is part of Direct X. This technology allows code to run on the Graphics Processing Unit. The main advantage of Direct Compute is that the same code runs on different brands of GPUs. However, Direct Compute only works on computers running the Microsoft Windows operating system.
Microsoft Direct Compute es parte de Direct X. Ésta tecnología permite ejecutar código en la Unidad de Procesamiento de Gráficos. La principal ventaja de Direct Compute es que el mismo código funciona en diferentes marcas de GPU. Sin embargo, Direct Compute sólo funciona en los computadores con el sistema operativo Microsoft Windows. DEFINITION Compute ShadersCompute Shaders are codes that run on the GPU. They are written in the HLSL language, which is very similar to the C++ language. It is recommended to distribute the compiled files of the Compute Shaders instead of the original codes written in HLSL,
Los Compute Shaders son códigos que se ejecutan en la GPU. Éstos están escritos en el lenguaje HLSL, el cual es muy similar a lenguaje C++. Se recomienda distribuir los archivos compilados de los Compute Shaders en lugar de los códigos originales escritos en HLSL,

Problem 1
Create a Dialog application called Fast to perform some computation using Direct Compute. After creating the project, edit the files Fast.h, Fast.cpp and stdafx.h as shown.
Cree una aplicación de diálogo llamada Fast para realizar algunos cálculos usando Direct Compute. Después de crear el proyecto, edite los archivos Fast.h, Fast.cpp y stdafx.h como se muestra.

FastRun

Fast.h
#pragma once //______________________________________ Fast.h
#include "Resource.h"
class Fast: public Win::Dialog
{
public:
     Fast()
     {
     }
     ~Fast()
     {
     }
     const wchar_t* ArrayOperation();
     wstring s;
     . . .
};


Fast.cpp
. . .
void Fast::Window_Open(Win::Event& e)
{
     const wchar_t* errorDesc = ArrayOperation();
     if (errorDesc != nullptr) this->MessageBox(errorDesc, L"Program", MB_OK | MB_ICONERROR);
}

const wchar_t* Fast::ArrayOperation()
{
     const wchar_t* p = nullptr;
     DC11::Device device;
     p = device.Create(hWnd, false); if (p != nullptr) { s = p; return s.c_str(); }

     Sample a[NUM_ELEMENTS];
     Sample b[NUM_ELEMENTS];
     for (int i = 0; i < NUM_ELEMENTS; i++)
     {
          a[i].count = i;
          a[i].weight = (float)i;
          //
          b[i].count = i;
          b[i].weight = (float)i;
     }
     //____________________________________________________ 1. Inputs: A and B
     DC11::GpuInput A, B;
     A.Setup(device, L"A", sizeof(Sample), NUM_ELEMENTS, false);
     B.Setup(device, L"B", sizeof(Sample), NUM_ELEMENTS, false);
     p = A.UploadToGPU(a); if (p != nullptr) { s = p; return s.c_str(); }
     p = B.UploadToGPU(b); if (p != nullptr) { s = p; return s.c_str(); }
     //____________________________________________________3. Output: Result
     DC11::GpuData Result;
     p = Result.Create(device, L"C", sizeof(Sample), NUM_ELEMENTS, nullptr); if (p != nullptr) { s = p; return s.c_str(); }
     //____________________________________________________ 4. Constant Data: C
     DC11::ConstantData C;
     ContextData c;
     c.m = 2.0f;
     c.n = 3.0f;
     c.o = 0.0f;
     c.p = 0.0f;
     p = C.Create(device, L"C", &c, sizeof(ContextData)); if (p != nullptr) { s = p; return s.c_str(); }
     //____________________________________________________5. C = m*A + n*B
     DC11::ComputeShader cs;
     p = cs.CompileAndSave(device, L"LinearOp.hlsl", "main", L"LinearOp.bin"); if (p != nullptr) { s = p; return s.c_str(); }
     p = cs.Load(device, L"LinearOp.bin"); if (p != nullptr) { s = p; return s.c_str(); }
     p = cs.Run(A, B, Result, C, NUM_ELEMENTS, 1, 1); if (p != nullptr) { s = p; return s.c_str(); }
     //____________________________________________________6. Display result
     Sample result[NUM_ELEMENTS];
     p = Result.CopyToCpu((void*)result); if (p != nullptr) { s = p; return s.c_str(); }
     wstring text;
     wchar_t tmp[32];
     for (int i = 0; i < NUM_ELEMENTS; i++)
     {
          _snwprintf_s(tmp, 32, _TRUNCATE, L"result[%d] = %d, %f\r\n", i, result[i].count, result[i].weight);
          text += tmp;
     }
     this->MessageBox(text, L"Result", MB_OK);
     return nullptr;
}


stdafx.h
. . .
//__________________________________________________________ 1. Sample
struct Sample
{
int count;
float weight;
};
//__________________________________________________________ 2. ContextData (struct size >=16 bytes)
struct ContextData
{
float m;
float n;
float o;
float p;
};
#define NUM_ELEMENTS 32


LinearOp.hlsl
//__________________________________________________________ 1. Sample
struct Sample
{
int count;
float weight;
};
//__________________________________________________________ 2. ContextData (struct size >=16 bytes)
cbuffer ContextData : register(b0)
{
float m;
float n;
float o;
float p;
};
//__________________________________________________________ 3. main
StructuredBuffer<Sample> A : register(t0);
StructuredBuffer<Sample> B : register(t1);
RWStructuredBuffer<Sample> C : register(u0);
[numthreads(1, 1, 1)]
void main(uint3 i : SV_DispatchThreadID)
{
C[i.x].count = A[i.x].count + B[i.x].count;
C[i.x].weight = m*A[i.x].weight + n*B[i.x].weight;
}


© Copyright 2000-2026 Wintempla selo. All Rights Reserved. abr. 17 2026. Home