diff options
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/render-test/main.cpp | 20 | ||||
| -rw-r--r-- | tools/render-test/options.cpp | 4 | ||||
| -rw-r--r-- | tools/render-test/options.h | 3 | ||||
| -rw-r--r-- | tools/render-test/render-d3d11.cpp | 240 | ||||
| -rw-r--r-- | tools/render-test/render-gl.cpp | 208 | ||||
| -rw-r--r-- | tools/render-test/render.h | 9 | ||||
| -rw-r--r-- | tools/render-test/shader-input-layout.cpp | 88 | ||||
| -rw-r--r-- | tools/render-test/shader-input-layout.h | 14 | ||||
| -rw-r--r-- | tools/slang-test/main.cpp | 56 |
9 files changed, 428 insertions, 214 deletions
diff --git a/tools/render-test/main.cpp b/tools/render-test/main.cpp index d3247e55f..cb0eb927d 100644 --- a/tools/render-test/main.cpp +++ b/tools/render-test/main.cpp @@ -37,13 +37,14 @@ struct Vertex { float position[3]; float color[3]; + float uv[2]; }; static const int kVertexCount = 3; static const Vertex kVertexData[kVertexCount] = { - { { 0, 0, 0.5 }, {1, 0, 0} }, - { { 0, 1, 0.5 }, {0, 0, 1} }, - { { 1, 0, 0.5 }, {0, 1, 0} }, + { { 0, 0, 0.5 }, {1, 0, 0} , {0, 0} }, + { { 0, 1, 0.5 }, {0, 0, 1} , {1, 0} }, + { { 1, 0, 0.5 }, {0, 1, 0} , {1, 1} }, }; @@ -65,9 +66,9 @@ static char const* computeEntryPointName = "computeMain"; // "Profile" to use when compiling for HLSL targets // TODO: does this belong here? -static char const* vertexProfileName = "vs_4_0"; -static char const* fragmentProfileName = "ps_4_0"; -static char const* computeProfileName = "cs_4_0"; +static char const* vertexProfileName = "vs_5_0"; +static char const* fragmentProfileName = "ps_5_0"; +static char const* computeProfileName = "cs_5_0"; Error initializeShaders( ShaderCompiler* shaderCompiler) @@ -101,7 +102,7 @@ Error initializeShaders( ShaderCompileRequest compileRequest; compileRequest.source = sourceInfo; - if (gOptions.shaderType == ShaderProgramType::Graphics) + if (gOptions.shaderType == ShaderProgramType::Graphics || gOptions.shaderType == ShaderProgramType::GraphicsCompute) { compileRequest.vertexShader.source = sourceInfo; compileRequest.vertexShader.name = vertexEntryPointName; @@ -157,9 +158,10 @@ Error initializeInner( InputElementDesc inputElements[] = { { "A", 0, Format::RGB_Float32, offsetof(Vertex, position) }, { "A", 1, Format::RGB_Float32, offsetof(Vertex, color) }, + { "A", 2, Format::RG_Float32, offsetof(Vertex, uv) }, }; - gInputLayout = renderer->createInputLayout(&inputElements[0], 2); + gInputLayout = renderer->createInputLayout(&inputElements[0], sizeof(inputElements)/sizeof(inputElements[0])); if(!gInputLayout) return Error::Unexpected; @@ -409,7 +411,7 @@ int main( // If we are in a mode where output is requested, we need to snapshot the back buffer here if (gOptions.outputPath) { - if (gOptions.shaderType == ShaderProgramType::Compute) + if (gOptions.shaderType == ShaderProgramType::Compute || gOptions.shaderType == ShaderProgramType::GraphicsCompute) renderer->serializeOutput(gBindingState, gOptions.outputPath); else renderer->captureScreenShot(gOptions.outputPath); diff --git a/tools/render-test/options.cpp b/tools/render-test/options.cpp index 89a68b27f..629016155 100644 --- a/tools/render-test/options.cpp +++ b/tools/render-test/options.cpp @@ -100,6 +100,10 @@ void parseOptions(int* argc, char** argv) { gOptions.shaderType = ShaderProgramType::Graphics; } + else if (strcmp(arg, "-gcompute") == 0) + { + gOptions.shaderType = ShaderProgramType::GraphicsCompute; + } else { fprintf(stderr, "unknown option '%s'\n", arg); diff --git a/tools/render-test/options.h b/tools/render-test/options.h index a36e0a809..2c48c4ceb 100644 --- a/tools/render-test/options.h +++ b/tools/render-test/options.h @@ -34,7 +34,8 @@ enum enum class ShaderProgramType { Graphics, - Compute + Compute, + GraphicsCompute }; struct Options diff --git a/tools/render-test/render-d3d11.cpp b/tools/render-test/render-d3d11.cpp index 8f73b4dd4..ee34445d0 100644 --- a/tools/render-test/render-d3d11.cpp +++ b/tools/render-test/render-d3d11.cpp @@ -54,6 +54,7 @@ struct D3DBinding struct D3DBindingState { List<D3DBinding> bindings; + int numRenderTargets = 0; }; // @@ -338,8 +339,8 @@ public: { hr = D3D11CreateDeviceAndSwapChain_( NULL, // adapter (use default) - D3D_DRIVER_TYPE_WARP, -// D3D_DRIVER_TYPE_HARDWARE, + D3D_DRIVER_TYPE_REFERENCE, + //D3D_DRIVER_TYPE_HARDWARE, NULL, // software deviceFlags, &featureLevels[ii], @@ -392,8 +393,6 @@ public: dxRenderTargetTextures.Add(texture); } - // We immediately bind the back-buffer render target view, and we aren't - // going to switch. We don't bother with a depth buffer. dxImmediateContext->OMSetRenderTargets( dxRenderTargetViews.Count(), dxRenderTargetViews.Buffer(), @@ -452,7 +451,6 @@ public: struct D3DBuffer { - ID3D11UnorderedAccessView * view = nullptr; ID3D11Buffer * buffer = nullptr; }; @@ -470,19 +468,10 @@ public: break; case BufferFlavor::Vertex: - dxBufferDesc.Usage = D3D11_USAGE_DEFAULT; + dxBufferDesc.Usage = D3D11_USAGE_DYNAMIC; dxBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; - dxBufferDesc.CPUAccessFlags = 0; + dxBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; break; - - case BufferFlavor::Storage: - dxBufferDesc.Usage = D3D11_USAGE_DEFAULT; - dxBufferDesc.BindFlags = D3D11_BIND_UNORDERED_ACCESS; - dxBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE; - dxBufferDesc.StructureByteStride = sizeof(float); - dxBufferDesc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED; - break; - default: return nullptr; } @@ -500,17 +489,6 @@ public: D3DBuffer * rs = new D3DBuffer(); rs->buffer = dxBuffer; - if (desc.flavor == BufferFlavor::Storage) - { - D3D11_UNORDERED_ACCESS_VIEW_DESC viewDesc; - memset(&viewDesc, 0, sizeof(viewDesc)); - viewDesc.Buffer.FirstElement = 0; - viewDesc.Buffer.NumElements = 512; - viewDesc.Buffer.Flags = 0; - viewDesc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER; - viewDesc.Format = DXGI_FORMAT_UNKNOWN; - dxDevice->CreateUnorderedAccessView(dxBuffer, &viewDesc, &rs->view); - } return (Buffer*) rs; } @@ -520,7 +498,8 @@ public: { case Format::RGB_Float32: return DXGI_FORMAT_R32G32B32_FLOAT; - + case Format::RG_Float32: + return DXGI_FORMAT_R32G32_FLOAT; default: return DXGI_FORMAT_UNKNOWN; } @@ -556,7 +535,9 @@ public: case Format::RGB_Float32: typeName = "float3"; break; - + case Format::RG_Float32: + typeName = "float2"; + break; default: return nullptr; } @@ -570,7 +551,7 @@ public: hlslCursor += sprintf(hlslCursor, "\n) : SV_Position { return 0; }"); - auto dxVertexShaderBlob = compileHLSLShader("inputLayout", hlslBuffer, "main", "vs_4_0"); + auto dxVertexShaderBlob = compileHLSLShader("inputLayout", hlslBuffer, "main", "vs_5_0"); if(!dxVertexShaderBlob) return nullptr; @@ -711,18 +692,6 @@ public: (UINT) startSlot, (UINT) slotCount, &dxConstantBuffers[0]->buffer); } - virtual void setStorageBuffers(UInt startSlot, UInt slotCount, Buffer* const* buffers, UInt const* offsets) override - { - auto dxContext = dxImmediateContext; - - // TODO: actually use those offsets - - auto dxStorageBuffers = (D3DBuffer* const*)buffers; - dxContext->CSSetUnorderedAccessViews( - (UINT)startSlot, (UINT)slotCount, &dxStorageBuffers[0]->view, 0); - } - - virtual void draw(UInt vertexCount, UInt startVertex) override { auto dxContext = dxImmediateContext; @@ -805,15 +774,13 @@ public: desc.ByteWidth = bufferData.Count() * sizeof(unsigned int); if (bufferDesc.type == InputBufferType::ConstantBuffer) { - desc.Usage = D3D11_USAGE_DYNAMIC; + desc.Usage = D3D11_USAGE_DEFAULT; desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER | D3D11_BIND_SHADER_RESOURCE; - desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; } else { desc.Usage = D3D11_USAGE_DEFAULT; desc.BindFlags = D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE; - desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE; if (bufferDesc.stride != 0) { desc.StructureByteStride = bufferDesc.stride; @@ -852,94 +819,33 @@ public: void createInputTexture(const InputTextureDesc & inputDesc, ID3D11ShaderResourceView * &viewOut) { - int arrLen = inputDesc.arrayLength; - if (arrLen == 0) - arrLen = 1; + TextureData texData; + generateTextureData(texData, inputDesc); List<D3D11_SUBRESOURCE_DATA> subRes; - List<List<unsigned int>> dataBuffer; - int arraySize = arrLen; - if (inputDesc.isCube) - arraySize *= 6; - int textureSize = inputDesc.size; - int textureMipLevels = Math::Log2Floor(textureSize) + 1; - subRes.SetSize(textureMipLevels * arraySize); - dataBuffer.SetSize(subRes.Count()); - - auto iteratePixels = [&](int dimension, int size, unsigned int * buffer, auto f) + for (int i = 0; i < texData.arraySize; i++) { - if (dimension == 1) - for (int i = 0; i < size; i++) - buffer[i] = f(i, 0, 0); - else if (dimension == 2) - for (int i = 0; i < size; i++) - for (int j = 0; j < size; j++) - buffer[i*size + j] = f(j, i, 0); - else if (dimension == 3) - for (int i = 0; i < size; i++) - for (int j = 0; j < size; j++) - for (int k = 0; k < size; k++) - buffer[i*size*size + j*size + k] = f(k, j, i); - }; - - int slice = 0; - for (int i = 0; i < arraySize; i++) - { - for (int j = 0; j < textureMipLevels; j++) + int slice = 0; + for (int j = 0; j < texData.mipLevels; j++) { - int size = textureSize >> j; - int bufferLen = size; - if (inputDesc.dimension == 2) - bufferLen *= size; - else if (inputDesc.dimension == 3) - bufferLen *= size*size; - dataBuffer[slice].SetSize(bufferLen); - subRes[slice].pSysMem = dataBuffer[slice].Buffer(); - subRes[slice].SysMemPitch = sizeof(unsigned int) * size; - subRes[slice].SysMemSlicePitch = sizeof(unsigned int) * size * size; - - iteratePixels(inputDesc.dimension, size, dataBuffer[slice].Buffer(), [&](int x, int y, int z) -> unsigned int - { - if (inputDesc.content == InputTextureContent::Zero) - { - return 0x0; - } - else if (inputDesc.content == InputTextureContent::One) - { - return 0xFFFFFFFF; - } - else if (inputDesc.content == InputTextureContent::Gradient) - { - unsigned char r = (unsigned char)(x / (float)(size - 1) * 255.0f); - unsigned char g = (unsigned char)(y / (float)(size - 1) * 255.0f); - unsigned char b = (unsigned char)(z / (float)(size - 1) * 255.0f); - return 0xFF000000 + r + (g << 8) + (b << 16); - } - else if (inputDesc.content == InputTextureContent::ChessBoard) - { - unsigned int xSig = x < (size >> 1) ? 1 : 0; - unsigned int ySig = y < (size >> 1) ? 1 : 0; - unsigned int zSig = z < (size >> 1) ? 1 : 0; - auto sig = xSig ^ ySig ^ zSig; - if (sig) - return 0xFFFFFFFF; - else - return 0xFF808080; - } - return 0x0; - }); + int size = texData.textureSize >> j; + D3D11_SUBRESOURCE_DATA res; + res.pSysMem = texData.dataBuffer[slice].Buffer(); + res.SysMemPitch = sizeof(unsigned int) * size; + res.SysMemSlicePitch = sizeof(unsigned int) * size * size; + subRes.Add(res); slice++; } } if (inputDesc.dimension == 1) { D3D11_TEXTURE1D_DESC desc = { 0 }; - desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET | D3D11_BIND_UNORDERED_ACCESS; - desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE; + desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; + desc.CPUAccessFlags = 0; desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; - desc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS; - desc.MipLevels = textureMipLevels; - desc.ArraySize = arraySize; - desc.Width = textureSize; + desc.MiscFlags = 0; + desc.MipLevels = texData.mipLevels; + desc.ArraySize = texData.arraySize; + desc.Width = texData.textureSize; desc.Usage = D3D11_USAGE_DEFAULT; ID3D11Texture1D * texture; @@ -949,31 +855,32 @@ public: viewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1D; if (inputDesc.arrayLength != 0) viewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE1DARRAY; - viewDesc.Texture1D.MipLevels = textureMipLevels; + viewDesc.Texture1D.MipLevels = texData.mipLevels; viewDesc.Texture1D.MostDetailedMip = 0; - viewDesc.Texture1DArray.ArraySize = arraySize; + viewDesc.Texture1DArray.ArraySize = texData.arraySize; viewDesc.Texture1DArray.FirstArraySlice = 0; - viewDesc.Texture1DArray.MipLevels = textureMipLevels; + viewDesc.Texture1DArray.MipLevels = texData.mipLevels; viewDesc.Texture1DArray.MostDetailedMip = 0; + viewDesc.Format = desc.Format; dxDevice->CreateShaderResourceView(texture, &viewDesc, &viewOut); } else if (inputDesc.dimension == 2) { D3D11_SHADER_RESOURCE_VIEW_DESC viewDesc; D3D11_TEXTURE2D_DESC desc = { 0 }; - desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET | D3D11_BIND_UNORDERED_ACCESS; - desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE; + desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; + desc.CPUAccessFlags = 0; desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; - desc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS; - desc.MipLevels = textureMipLevels; - desc.ArraySize = arraySize; + desc.MiscFlags = 0; + desc.MipLevels = texData.mipLevels; + desc.ArraySize = texData.arraySize; if (inputDesc.isCube) { desc.MiscFlags |= D3D11_RESOURCE_MISC_TEXTURECUBE; viewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE; - viewDesc.TextureCube.MipLevels = textureMipLevels; + viewDesc.TextureCube.MipLevels = texData.mipLevels; viewDesc.TextureCube.MostDetailedMip = 0; - viewDesc.TextureCubeArray.MipLevels = textureMipLevels; + viewDesc.TextureCubeArray.MipLevels = texData.mipLevels; viewDesc.TextureCubeArray.MostDetailedMip = 0; viewDesc.TextureCubeArray.First2DArrayFace = 0; viewDesc.TextureCubeArray.NumCubes = inputDesc.arrayLength; @@ -981,20 +888,21 @@ public: else { viewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; - viewDesc.Texture2D.MipLevels = textureMipLevels; + viewDesc.Texture2D.MipLevels = texData.mipLevels; viewDesc.Texture2D.MostDetailedMip = 0; - viewDesc.Texture2DArray.ArraySize = arraySize; + viewDesc.Texture2DArray.ArraySize = texData.arraySize; viewDesc.Texture2DArray.FirstArraySlice = 0; - viewDesc.Texture2DArray.MipLevels = textureMipLevels; + viewDesc.Texture2DArray.MipLevels = texData.mipLevels; viewDesc.Texture2DArray.MostDetailedMip = 0; } if (inputDesc.arrayLength != 0) viewDesc.ViewDimension = (D3D11_SRV_DIMENSION)(int)(viewDesc.ViewDimension + 1); - desc.Width = textureSize; - desc.Height = textureSize; + desc.Width = texData.textureSize; + desc.Height = texData.textureSize; desc.Usage = D3D11_USAGE_DEFAULT; desc.SampleDesc.Count = 1; desc.SampleDesc.Quality = 0; + viewDesc.Format = desc.Format; ID3D11Texture2D * texture; dxDevice->CreateTexture2D(&desc, subRes.Buffer(), &texture); dxDevice->CreateShaderResourceView(texture, &viewDesc, &viewOut); @@ -1003,22 +911,21 @@ public: { D3D11_SHADER_RESOURCE_VIEW_DESC viewDesc; D3D11_TEXTURE3D_DESC desc = { 0 }; - desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET | D3D11_BIND_UNORDERED_ACCESS; - desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE; + desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; + desc.CPUAccessFlags = 0; desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; - desc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS; - desc.MipLevels = textureMipLevels; + desc.MiscFlags = 0; + desc.MipLevels = 1; viewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D; - desc.Width = textureSize; - desc.Height = textureSize; - desc.Depth = textureSize; + desc.Width = texData.textureSize; + desc.Height = texData.textureSize; + desc.Depth = texData.textureSize; desc.Usage = D3D11_USAGE_DEFAULT; ID3D11Texture3D * texture; dxDevice->CreateTexture3D(&desc, subRes.Buffer(), &texture); - if (inputDesc.arrayLength != 0) - viewDesc.ViewDimension = (D3D11_SRV_DIMENSION)(int)(viewDesc.ViewDimension + 1); - viewDesc.Texture3D.MipLevels = textureMipLevels; + viewDesc.Texture3D.MipLevels = 1; viewDesc.Texture3D.MostDetailedMip = 0; + viewDesc.Format = desc.Format; dxDevice->CreateShaderResourceView(texture, &viewDesc, &viewOut); } } @@ -1032,17 +939,21 @@ public: { desc.ComparisonFunc = D3D11_COMPARISON_LESS_EQUAL; desc.Filter = D3D11_FILTER_MIN_LINEAR_MAG_MIP_POINT; + desc.MinLOD = desc.MaxLOD = 0.0f; } else + { desc.Filter = D3D11_FILTER_ANISOTROPIC; - desc.MaxAnisotropy = 16; - desc.MinLOD = 0.0f; - desc.MaxLOD = 100.0f; + desc.MaxAnisotropy = 8; + desc.MinLOD = 0.0f; + desc.MaxLOD = 100.0f; + } dxDevice->CreateSamplerState(&desc, &stateOut); } virtual BindingState * createBindingState(const ShaderInputLayout & layout) { D3DBindingState * rs = new D3DBindingState(); + rs->numRenderTargets = layout.numRenderTargets; for (auto & entry : layout.entries) { D3DBinding rsEntry; @@ -1091,8 +1002,8 @@ public: if (isCompute) dxContext->CSSetUnorderedAccessViews(binding.binding, 1, &binding.uav, nullptr); else - dxContext->OMSetRenderTargetsAndUnorderedAccessViews(D3D11_KEEP_RENDER_TARGETS_AND_DEPTH_STENCIL, - nullptr, nullptr, binding.binding, 1, &binding.uav, nullptr); + dxContext->OMSetRenderTargetsAndUnorderedAccessViews(currentBindings->numRenderTargets, + dxRenderTargetViews.Buffer(), nullptr, binding.binding, 1, &binding.uav, nullptr); } else { @@ -1151,30 +1062,41 @@ public: { auto dxContext = dxImmediateContext; auto dxBindingState = (D3DBindingState*)state; - FILE * f = fopen(fileName, "wt"); + FILE * f = fopen(fileName, "wb"); + int id = 0; for (auto & binding : dxBindingState->bindings) { if (binding.isOutput) { if (binding.buffer) { - auto ptr = (unsigned int *)map(binding.buffer, MapFlavor::HostRead); + // create staging buffer + ID3D11Buffer* stageBuf; + D3D11_BUFFER_DESC bufDesc; + memset(&bufDesc, 0, sizeof(bufDesc)); + bufDesc.BindFlags = 0; + bufDesc.ByteWidth = binding.bufferLength; + bufDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + bufDesc.Usage = D3D11_USAGE_STAGING; + dxDevice->CreateBuffer(&bufDesc, nullptr, &stageBuf); + dxContext->CopyResource(stageBuf, binding.buffer); + auto ptr = (unsigned int *)map(stageBuf, MapFlavor::HostRead); for (auto i = 0u; i < binding.bufferLength / sizeof(unsigned int); i++) fprintf(f, "%X\n", ptr[i]); - unmap(binding.buffer); + unmap(stageBuf); + stageBuf->Release(); } else { - throw "not implemented"; + printf("invalid output type at %d.\n"); } } + id++; } fclose(f); } }; - - Renderer* createD3D11Renderer() { return new D3D11Renderer(); diff --git a/tools/render-test/render-gl.cpp b/tools/render-test/render-gl.cpp index ff9d54eb6..9563ced96 100644 --- a/tools/render-test/render-gl.cpp +++ b/tools/render-test/render-gl.cpp @@ -7,7 +7,7 @@ #include <assert.h> #include <stdio.h> #include <stdlib.h> - +#include "core/basic.h" #include "external/stb/stb_image_write.h" // TODO(tfoley): eventually we should be able to run these @@ -59,8 +59,15 @@ F(glDisableVertexAttribArray, PFNGLDISABLEVERTEXATTRIBARRAYPROC) \ F(glDebugMessageCallback, PFNGLDEBUGMESSAGECALLBACKPROC) \ F(glDispatchCompute, PFNGLDISPATCHCOMPUTEPROC) \ + F(glActiveTexture, PFNGLACTIVETEXTUREPROC) \ + F(glCreateSamplers, PFNGLCREATESAMPLERSPROC) \ + F(glBindSampler, PFNGLBINDSAMPLERPROC) \ + F(glTexImage3D, PFNGLTEXIMAGE3DPROC) \ + F(glSamplerParameteri, PFNGLSAMPLERPARAMETERIPROC) \ /* end */ +using namespace Slang; + namespace renderer_test { class GLRenderer : public Renderer, public ShaderCompiler @@ -280,6 +287,7 @@ public: case Format::NAME: do { VertexAttributeFormat result = {COUNT, TYPE, NORMALIZED}; return result; } while (0) CASE(RGB_Float32, 3, GL_FLOAT, GL_FALSE); + CASE(RG_Float32, 2, GL_FLOAT, GL_FALSE); #undef CASE @@ -402,12 +410,6 @@ public: bindBufferImpl(GL_UNIFORM_BUFFER, startSlot, slotCount, buffers, offsets); } - - virtual void setStorageBuffers(UInt startSlot, UInt slotCount, Buffer* const* buffers, UInt const* offsets) override - { - bindBufferImpl(GL_SHADER_STORAGE_BUFFER, startSlot, slotCount, buffers, offsets); - } - void flushStateForDraw() { auto layout = this->boundInputLayout; @@ -614,19 +616,205 @@ public: glDispatchCompute(x, y, z); } + struct GLBindingEntry + { + ShaderInputType type; + GLuint handle; + List<int> binding; + int bindTarget; + int bufferSize; + bool isOutput = false; + }; + struct GLBindingState + { + List<GLBindingEntry> entries; + }; + + void createInputBuffer(GLBindingEntry & rs, InputBufferDesc bufDesc, List<unsigned int> & bufferData) + { + rs.bindTarget = (bufDesc.type == InputBufferType::StorageBuffer ? GL_SHADER_STORAGE_BUFFER : GL_UNIFORM_BUFFER); + glGenBuffers(1, &rs.handle); + glBindBuffer(rs.bindTarget, rs.handle); + glBufferData(rs.bindTarget, bufferData.Count() * sizeof(unsigned int), bufferData.Buffer(), GL_STATIC_READ); + glBindBuffer(rs.bindTarget, 0); + } + + void createInputTexture(GLBindingEntry & rs, InputTextureDesc texDesc, InputSamplerDesc samplerDesc) + { + TextureData texData; + generateTextureData(texData, texDesc); + glGenTextures(1, &rs.handle); + switch (texDesc.dimension) + { + case 1: + if (texDesc.arrayLength > 0) + { + rs.bindTarget = GL_TEXTURE_1D_ARRAY; + glBindTexture(rs.bindTarget, rs.handle); + int slice = 0; + for (int i = 0; i < texData.arraySize; i++) + for (int j = 0; j < texData.mipLevels; j++) + { + glTexImage2D(rs.bindTarget, j, GL_RGBA8, texData.textureSize, i, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData.dataBuffer[slice].Buffer()); + slice++; + } + } + else + { + rs.bindTarget = GL_TEXTURE_1D; + glBindTexture(rs.bindTarget, rs.handle); + for (int i = 0; i < texData.mipLevels; i++) + glTexImage1D(rs.bindTarget, i, GL_RGBA8, texData.textureSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData.dataBuffer[i].Buffer()); + } + break; + case 2: + if (texDesc.arrayLength > 0) + { + if (texDesc.isCube) + rs.bindTarget = GL_TEXTURE_CUBE_MAP_ARRAY; + else + rs.bindTarget = GL_TEXTURE_2D_ARRAY; + glBindTexture(rs.bindTarget, rs.handle); + for (auto i = 0u; i < texData.dataBuffer.Count(); i++) + glTexImage3D(rs.bindTarget, i % texData.mipLevels, GL_RGBA8, texData.textureSize, texData.textureSize, i, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData.dataBuffer[i].Buffer()); + } + else + { + if (texDesc.isCube) + { + rs.bindTarget = GL_TEXTURE_CUBE_MAP; + glBindTexture(rs.bindTarget, rs.handle); + for (int j = 0; j < 6; j++) + { + for (int i = 0; i < texData.mipLevels; i++) + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + j, i, GL_RGBA8, texData.textureSize, texData.textureSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData.dataBuffer[i + j*texData.mipLevels].Buffer()); + } + } + else + { + rs.bindTarget = GL_TEXTURE_2D; + glBindTexture(rs.bindTarget, rs.handle); + for (int i = 0; i < texData.mipLevels; i++) + glTexImage2D(rs.bindTarget, i, GL_RGBA8, texData.textureSize, texData.textureSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData.dataBuffer[i].Buffer()); + } + } + break; + case 3: + rs.bindTarget = GL_TEXTURE_3D; + glBindTexture(rs.bindTarget, rs.handle); + for (int i = 0; i < texData.mipLevels; i++) + glTexImage3D(rs.bindTarget, i, GL_RGBA8, texData.textureSize, texData.textureSize, texData.textureSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData.dataBuffer[i].Buffer()); + break; + } + glTexParameteri(rs.bindTarget, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(rs.bindTarget, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(rs.bindTarget, GL_TEXTURE_WRAP_R, GL_REPEAT); + + if (samplerDesc.isCompareSampler) + { + glTexParameteri(rs.bindTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(rs.bindTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameterf(rs.bindTarget, GL_TEXTURE_MAX_ANISOTROPY_EXT, 8.0f); + } + else + { + glTexParameteri(rs.bindTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(rs.bindTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(rs.bindTarget, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); + glTexParameteri(rs.bindTarget, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); + } + } + + void createInputSampler(GLBindingEntry & rs, InputSamplerDesc samplerDesc) + { + glCreateSamplers(1, &rs.handle); + glSamplerParameteri(rs.handle, GL_TEXTURE_WRAP_S, GL_REPEAT); + glSamplerParameteri(rs.handle, GL_TEXTURE_WRAP_T, GL_REPEAT); + glSamplerParameteri(rs.handle, GL_TEXTURE_WRAP_R, GL_REPEAT); + + if (samplerDesc.isCompareSampler) + { + glSamplerParameteri(rs.handle, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glSamplerParameteri(rs.handle, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glSamplerParameteri(rs.handle, GL_TEXTURE_MAX_ANISOTROPY_EXT, 8.0f); + } + else + { + glSamplerParameteri(rs.handle, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glSamplerParameteri(rs.handle, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glSamplerParameteri(rs.handle, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); + glSamplerParameteri(rs.handle, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); + } + } + virtual BindingState * createBindingState(const ShaderInputLayout & layout) { - return nullptr; + GLBindingState * rs = new GLBindingState(); + for (auto & entry : layout.entries) + { + GLBindingEntry rsEntry; + rsEntry.isOutput = entry.isOutput; + rsEntry.binding = entry.glslBinding; + rsEntry.type = entry.type; + switch (entry.type) + { + case ShaderInputType::Buffer: + createInputBuffer(rsEntry, entry.bufferDesc, entry.bufferData); + break; + case ShaderInputType::Texture: + createInputTexture(rsEntry, entry.textureDesc, InputSamplerDesc()); + break; + case ShaderInputType::CombinedTextureSampler: + createInputTexture(rsEntry, entry.textureDesc, entry.samplerDesc); + break; + case ShaderInputType::Sampler: + createInputSampler(rsEntry, entry.samplerDesc); + break; + } + rs->entries.Add(rsEntry); + } + return (BindingState*)rs; } virtual void setBindingState(BindingState * state) { - + GLBindingState * glState = (GLBindingState*)state; + for (auto & entry : glState->entries) + { + switch (entry.type) + { + case ShaderInputType::Buffer: + glBindBufferBase(entry.bindTarget, entry.binding[0], entry.handle); + break; + case ShaderInputType::Sampler: + for (auto b : entry.binding) + glBindSampler(b, entry.handle); + break; + case ShaderInputType::Texture: + case ShaderInputType::CombinedTextureSampler: + glActiveTexture(GL_TEXTURE0 + entry.binding[0]); + glBindTexture(entry.bindTarget, entry.handle); + break; + } + } } virtual void serializeOutput(BindingState* state, const char * fileName) { - + GLBindingState * glState = (GLBindingState*)state; + FILE * f = fopen(fileName, "wt"); + for (auto & entry : glState->entries) + { + if (entry.isOutput) + { + glBindBuffer(entry.bindTarget, entry.handle); + auto ptr = (unsigned int *)glMapBuffer(entry.bindTarget, GL_READ_ONLY); + for (auto i = 0u; i < entry.bufferSize / sizeof(unsigned int); i++) + fprintf(f, "%X\n", ptr[i]); + glUnmapBuffer(entry.bindTarget); + } + } + fclose(f); } }; diff --git a/tools/render-test/render.h b/tools/render-test/render.h index 706868e9a..dec48cda4 100644 --- a/tools/render-test/render.h +++ b/tools/render-test/render.h @@ -43,13 +43,13 @@ enum class Format { Unknown, RGB_Float32, + RG_Float32, }; enum class BufferFlavor { Constant, - Vertex, - Storage, + Vertex }; struct BufferDesc @@ -113,15 +113,10 @@ public: virtual void setShaderProgram(ShaderProgram* program) = 0; virtual void setConstantBuffers(UInt startSlot, UInt slotCount, Buffer* const* buffers, UInt const* offsets) = 0; - virtual void setStorageBuffers(UInt startSlot, UInt slotCount, Buffer* const* buffers, UInt const* offsets) = 0; inline void setConstantBuffer(UInt slot, Buffer* buffer, UInt offset = 0) { setConstantBuffers(slot, 1, &buffer, &offset); } - inline void setStorageBuffer(UInt slot, Buffer* buffer, UInt offset = 0) - { - setStorageBuffers(slot, 1, &buffer, &offset); - } virtual void draw(UInt vertexCount, UInt startVertex = 0) = 0; virtual void dispatchCompute(int x, int y, int z) = 0; }; diff --git a/tools/render-test/shader-input-layout.cpp b/tools/render-test/shader-input-layout.cpp index b606a1a13..ef78fe3d5 100644 --- a/tools/render-test/shader-input-layout.cpp +++ b/tools/render-test/shader-input-layout.cpp @@ -171,8 +171,8 @@ namespace renderer_test else break; } + parser.Read(")"); } - parser.Read(")"); // parse bindings if (parser.LookAhead(":")) { @@ -190,11 +190,13 @@ namespace renderer_test { parser.ReadToken(); parser.Read("("); - entry.glslBinding = entry.glslLocation = parser.ReadInt(); - if (parser.LookAhead(",")) + while (!parser.IsEnd() && !parser.LookAhead(")")) { - parser.Read(","); - entry.glslLocation = parser.ReadInt(); + entry.glslBinding.Add(parser.ReadInt()); + if (parser.LookAhead(",")) + parser.Read(","); + else + break; } parser.Read(")"); } @@ -218,4 +220,80 @@ namespace renderer_test } + void generateTextureData(TextureData & output, const InputTextureDesc & inputDesc) + { + int arrLen = inputDesc.arrayLength; + if (arrLen == 0) + arrLen = 1; + List<List<unsigned int>> & dataBuffer = output.dataBuffer; + int arraySize = arrLen; + if (inputDesc.isCube) + arraySize *= 6; + output.arraySize = arraySize; + output.textureSize = inputDesc.size; + output.mipLevels = Math::Log2Floor(output.textureSize) + 1; + output.dataBuffer.SetSize(output.mipLevels * output.arraySize); + auto iteratePixels = [&](int dimension, int size, unsigned int * buffer, auto f) + { + if (dimension == 1) + for (int i = 0; i < size; i++) + buffer[i] = f(i, 0, 0); + else if (dimension == 2) + for (int i = 0; i < size; i++) + for (int j = 0; j < size; j++) + buffer[i*size + j] = f(j, i, 0); + else if (dimension == 3) + for (int i = 0; i < size; i++) + for (int j = 0; j < size; j++) + for (int k = 0; k < size; k++) + buffer[i*size*size + j*size + k] = f(k, j, i); + }; + + int slice = 0; + for (int i = 0; i < arraySize; i++) + { + for (int j = 0; j < output.mipLevels; j++) + { + int size = output.textureSize >> j; + int bufferLen = size; + if (inputDesc.dimension == 2) + bufferLen *= size; + else if (inputDesc.dimension == 3) + bufferLen *= size*size; + dataBuffer[slice].SetSize(bufferLen); + + iteratePixels(inputDesc.dimension, size, dataBuffer[slice].Buffer(), [&](int x, int y, int z) -> unsigned int + { + if (inputDesc.content == InputTextureContent::Zero) + { + return 0x0; + } + else if (inputDesc.content == InputTextureContent::One) + { + return 0xFFFFFFFF; + } + else if (inputDesc.content == InputTextureContent::Gradient) + { + unsigned char r = (unsigned char)(x / (float)(size - 1) * 255.0f); + unsigned char g = (unsigned char)(y / (float)(size - 1) * 255.0f); + unsigned char b = (unsigned char)(z / (float)(size - 1) * 255.0f); + return 0xFF000000 + r + (g << 8) + (b << 16); + } + else if (inputDesc.content == InputTextureContent::ChessBoard) + { + unsigned int xSig = x < (size >> 1) ? 1 : 0; + unsigned int ySig = y < (size >> 1) ? 1 : 0; + unsigned int zSig = z < (size >> 1) ? 1 : 0; + auto sig = xSig ^ ySig ^ zSig; + if (sig) + return 0xFFFFFFFF; + else + return 0xFF808080; + } + return 0x0; + }); + slice++; + } + } + } }
\ No newline at end of file diff --git a/tools/render-test/shader-input-layout.h b/tools/render-test/shader-input-layout.h index f2258b7b9..9602e4fe8 100644 --- a/tools/render-test/shader-input-layout.h +++ b/tools/render-test/shader-input-layout.h @@ -46,9 +46,19 @@ namespace renderer_test InputSamplerDesc samplerDesc; bool isOutput = false; int hlslBinding = -1; - int glslBinding = -1; - int glslLocation = -1; + Slang::List<int> glslBinding; + }; + + struct TextureData + { + Slang::List<Slang::List<unsigned int>> dataBuffer; + int textureSize; + int mipLevels; + int arraySize; + }; + void generateTextureData(TextureData & output, const InputTextureDesc & desc); + class ShaderInputLayout { public: diff --git a/tools/slang-test/main.cpp b/tools/slang-test/main.cpp index cdea6b4dc..5b4337d12 100644 --- a/tools/slang-test/main.cpp +++ b/tools/slang-test/main.cpp @@ -1116,7 +1116,7 @@ TestResult runGLSLComparisonTest(TestInput& input) return kTestResult_Pass; } -TestResult doComputeComparisonTestRunImpl(TestInput& input, const char * langOption, String referenceOutput) +TestResult runComputeComparisonImpl(TestInput& input, const char * langOption, String referenceOutput) { // TODO: delete any existing files at the output path(s) to avoid stale outputs leading to a false pass auto filePath999 = input.filePath; @@ -1138,32 +1138,36 @@ TestResult doComputeComparisonTestRunImpl(TestInput& input, const char * langOpt if (spawnAndWait(outputStem, spawner) != kOSError_None) { + printf("error spawning render-test\n"); return kTestResult_Fail; } auto actualOutput = getOutput(spawner); - auto expectedOutput = getExpectedOutput(outputStem); - if(actualOutput != expectedOutput) - { - String actualOutputPath = outputStem + ".actual"; - Slang::File::WriteAllText(actualOutputPath, actualOutput); - - maybeDumpOutput(expectedOutput, actualOutput); - - return kTestResult_Fail; - } - // check against reference output - if (!File::Exists(outputStem + ".actual.txt")) + if (!File::Exists(outputStem + ".actual.txt")) + { + printf("render-test not producing expected outputs.\n"); + printf("render-test output:\n%s\n", actualOutput.Buffer()); return kTestResult_Fail; - if (!File::Exists(referenceOutput)) + } + if (!File::Exists(referenceOutput)) + { + printf("referenceOutput %s not found.\n", referenceOutput.Buffer()); return kTestResult_Fail; - - auto actualProgramOutput = Split(File::ReadAllText(outputStem + ".actual.txt"), '\n'); + } + auto actualOutputContent = File::ReadAllText(outputStem + ".actual.txt"); + auto actualProgramOutput = Split(actualOutputContent, '\n'); auto referenceProgramOutput = Split(File::ReadAllText(referenceOutput), '\n'); - if (actualProgramOutput.Count() < referenceProgramOutput.Count()) + auto printOutput = [&]() + { + printf("output mismatch! actual output: {\n%s\n}, \n%s\n", actualOutputContent.Buffer(), actualOutput.Buffer()); + }; + if (actualProgramOutput.Count() < referenceProgramOutput.Count()) + { + printOutput(); return kTestResult_Fail; + } for (int i = 0; i < (int)referenceProgramOutput.Count(); i++) { auto reference = referenceProgramOutput[i]; @@ -1174,7 +1178,10 @@ TestResult doComputeComparisonTestRunImpl(TestInput& input, const char * langOpt auto val = StringToFloat(reference); auto uval = String((unsigned int)FloatAsInt(val), 16).ToUpper(); if (actual != uval) + { + printOutput(); return kTestResult_Fail; + } else return kTestResult_Pass; } @@ -1182,9 +1189,14 @@ TestResult doComputeComparisonTestRunImpl(TestInput& input, const char * langOpt return kTestResult_Pass; } -TestResult doSlangComputeComparisonTest(TestInput& input) +TestResult runSlangComputeComparisonTest(TestInput& input) +{ + return runComputeComparisonImpl(input, "-slang -compute", input.outputStem + ".expected.txt"); +} + +TestResult runSlangRenderComputeComparisonTest(TestInput& input) { - return doComputeComparisonTestRunImpl(input, "-slang -compute", input.outputStem + ".expected.txt"); + return runComputeComparisonImpl(input, "-slang -gcompute", input.outputStem + ".expected.txt"); } TestResult doRenderComparisonTestRun(TestInput& input, char const* langOption, char const* outputKind, String* outOutput) @@ -1385,16 +1397,18 @@ TestResult runTest( { "COMPARE_HLSL_RENDER", &runHLSLRenderComparisonTest }, { "COMPARE_HLSL_CROSS_COMPILE_RENDER", &runHLSLCrossCompileRenderComparisonTest}, { "COMPARE_HLSL_GLSL_RENDER", &runHLSLAndGLSLComparisonTest }, - { "COMPARE_COMPUTE", &doSlangComputeComparisonTest}, + { "COMPARE_COMPUTE", runSlangComputeComparisonTest}, + { "COMPARE_RENDER_COMPUTE", &runSlangRenderComputeComparisonTest }, + #else { "COMPARE_HLSL", &skipTest }, { "COMPARE_HLSL_RENDER", &skipTest }, { "COMPARE_HLSL_CROSS_COMPILE_RENDER", &skipTest}, { "COMPARE_HLSL_GLSL_RENDER", &skipTest }, { "COMPARE_COMPUTE", &skipTest}, + { "COMPARE_RENDER_COMPUTE", &skipTest }, #endif { "COMPARE_GLSL", &runGLSLComparisonTest }, - { "CROSS_COMPILE", &runCrossCompilerTest }, { "EVAL", &runEvalTest }, { nullptr, nullptr }, |
