// MIT License // // Copyright (C) 2018-2023, Tellusim Technologies Inc. https://tellusim.com/ // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. #include #include #include #include #include /* */ using namespace Tellusim; /* */ int32_t main(int32_t argc, char **argv) { DECLARE_WINDOW // create window String title = String::format("%s Tellusim::Samples", window.getPlatformName()); if(!window.create(title) || !window.setHidden(false)) return 1; // create device Device device(window); if(!device) return 1; // texture flags Texture::Flags flags = Texture::FlagTarget | Texture::FlagSource; uint32_t samples = device.getFeatures().maxTextureSamples; if(samples >= 8) flags |= Texture::FlagMultisample8; else if(samples >= 4) flags |= Texture::FlagMultisample4; else if(samples >= 2) flags |= Texture::FlagMultisample2; else { TS_LOG(Error, "multisample is not supported\n"); return 0; } // create texture Texture texture = device.createTexture2D(FormatRGBAf16, window.getWidth() / 2, window.getHeight() / 2, flags); if(!texture) return 1; // window title title += String::format(" %ux", texture.getMultisample()); // create render pipeline Pipeline render_pipeline = device.createPipeline().setUniformMask(0, Shader::MaskFragment); render_pipeline.addAttribute(Pipeline::AttributePosition, FormatRGf32, 0, sizeof(float32_t) * 0, sizeof(float32_t) * 4); render_pipeline.addAttribute(Pipeline::AttributeTexCoord, FormatRGf32, 0, sizeof(float32_t) * 2, sizeof(float32_t) * 4); render_pipeline.setColorFormat(texture.getFormat()); render_pipeline.setMultisample(texture.getMultisample()); render_pipeline.setSampleShading(true); render_pipeline.setSampleMask((1 << (texture.getMultisample() - 1)) - 1); if(!render_pipeline.loadShaderGLSL(Shader::TypeVertex, "main.shader", "RENDER_TARGET=1; VERTEX_SHADER=1")) return 1; if(!render_pipeline.loadShaderGLSL(Shader::TypeFragment, "main.shader", "RENDER_TARGET=1; FRAGMENT_SHADER=1")) return 1; if(!render_pipeline.create()) return 1; // create window pipeline Pipeline window_pipeline = device.createPipeline().setTextureMask(0, Shader::MaskFragment); window_pipeline.addAttribute(Pipeline::AttributePosition, FormatRGf32, 0, sizeof(float32_t) * 0, sizeof(float32_t) * 4); window_pipeline.addAttribute(Pipeline::AttributeTexCoord, FormatRGf32, 0, sizeof(float32_t) * 2, sizeof(float32_t) * 4); window_pipeline.setColorFormat(window.getColorFormat()); window_pipeline.setDepthFormat(window.getDepthFormat()); if(!window_pipeline.loadShaderGLSL(Shader::TypeVertex, "main.shader", "VERTEX_SHADER=1")) return 1; if(!window_pipeline.loadShaderGLSL(Shader::TypeFragment, "main.shader", "FRAGMENT_SHADER=1; SAMPLES=%u", texture.getMultisample())) return 1; if(!window_pipeline.create()) return 1; // create targets Target render_target = device.createTarget(); Target window_target = device.createTarget(window); render_target.setColorTexture(texture, Target::OpClearStore); // quad vertices static const float32_t vertex_data[] = { 3.0f, -1.0f, 2.0f, 0.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 3.0f, 0.0f, 2.0f, }; // main loop DECLARE_GLOBAL window.run([&]() -> bool { DECLARE_COMMON Window::update(); if(!window.render()) return false; // window title if(fps > 0.0f) window.setTitle(String::format("%s %.1f FPS", title.get(), fps)); // render target render_target.setClearColor(1.0f, 1.0f, 1.0f, 1.0f); render_target.begin(); { // create command list Command command = device.createCommand(render_target); // set pipeline command.setPipeline(render_pipeline); // uniform parameters command.setUniform(0, time); // draw geometry command.setVertexData(0, vertex_data, sizeof(vertex_data)); command.drawArrays(3); } render_target.end(); // flush texture device.flushTexture(texture); // window target window_target.begin(); { // create command list Command command = device.createCommand(window_target); // set pipeline command.setPipeline(window_pipeline); // draw texture command.setTexture(0, texture); command.setVertexData(0, vertex_data, sizeof(vertex_data)); command.drawArrays(3); } window_target.end(); if(!window.present()) return false; if(!device.check()) return false; return true; }); // finish context window.finish(); return 0; }