= DKComputeShader 101 = == 1. Get the ComputeCommandQueue from the Device == for sure {{{#!highlight C++ DKObject device = DKGraphicsDevice::SharedInstance(); DKObject computeQueue = device->CreateCommandQueue(DKCommandQueue::Compute); }}} == 2. Prepare the Gpu resources == === 1. Texture === Texture usage decription should has one of these flags. {{{#!highlight C++ DKTexture::UsageStorage // Recommended DKTexture::UsageShaderWrite // Could be deprecated soon }}} Texutre Decriptor Example {{{#!highlight C++ DKTextureDescriptor texDesc = {}; texDesc.textureType = DKTexture::Type2D; texDesc.pixelFormat = DKPixelFormat::BGRA8Unorm; texDesc.width = w; texDesc.height = h; texDesc.depth = 1; texDesc.mipmapLevels = 1; texDesc.sampleCount = 1; texDesc.arrayLength = 1; texDesc.usage = DKTexture::UsageStorage // For Compute Shader | DKTexture::UsageSampled | DKTexture::UsageShaderRead; // For Fragment Shader textureTarget = device->CreateTexture(texDesc); }}} === 2. Shader === In DKGL2, we use same class for all type of shaders. === 1. LoadResourceData From the resource path === {{{#!highlight C++ DKObject computeShaderData = resourcePool.LoadResourceData("shaders/ComputeShader.comp.spv"); }}} === 2. Create Function from data above step === {{{#!highlight C++ DKGraphicsDevice* device = queue->Device(); DKShader shader(shaderData); DKObject shaderModule = device->CreateShaderModule(&shader); DKObject shaderFunc = shaderModule->CreateFunction(shaderModule->FunctionNames().Value(0)); }}} == 3. Prepare Uniformbuffers and BindSet == TODO == 4. Prepare the Pipeline == Ususally compute shader need only setting for compute function. Set the shader function to the Pipeline Descriptor {{{#!highlight C++ DKComputePipelineDescriptor computePipelineDescriptor; computePipelineDescriptor.computeFunction = csFunction; // csFunction as DKShaderFunction auto computePipeLine = device->CreateComputePipeline(computePipelineDescriptor); }}} == 5. CommandBuffer Encoding == {{{#!highlight C++ DKObject computeCmdbuffer = computeQueue->CreateCommandBuffer(); DKObject computeEncoder = computeCmdbuffer->CreateComputeCommandEncoder(); if (computeEncoder) { if (computebindSet) { computebindSet->SetTexture(0, texture); computebindSet->SetTexture(1, targetTex); } computeEncoder->SetComputePipelineState(computePipeLine); computeEncoder->SetResources(0, computebindSet); computeEncoder->Dispatch(width / 16, height / 16, 1); computeEncoder->EndEncoding(); } }}}