void uniform_image_sampler_base::apply_value(const render_context& context, const program& p) { const opengl::gl_core& glapi = context.opengl_api(); #if SCM_GL_CORE_USE_EXT_DIRECT_STATE_ACCESS if (_bound_unit >= 0) { glapi.glProgramUniform1i(p.program_id(), _location, _bound_unit); } else if ( glapi.extension_ARB_bindless_texture && _resident_handle != 0ull) { glapi.glProgramUniformHandleui64ARB(p.program_id(), _location, _resident_handle); } #else if (_bound_unit >= 0) { glapi.glUniform1i(_location, _bound_unit); } else if ( glapi.extension_ARB_bindless_texture && _resident_handle != 0ull) { glapi.glUniformHandleui64ARB(_location, _resident_handle); } #endif gl_assert(glapi, leaving uniform_1f::apply_value()); }
bool load_program(const char * asset_program, program & result) { auto data = assets_storage::instance().read<program_data>(asset_program); if(!data.v_shader() || !data.f_shader()) return false; shader v_shader(data.v_shader(), shader_type::VERTEX); if(!v_shader.compile()) return false; shader f_shader(data.f_shader(), shader_type::FRAGMENT); if(!f_shader.compile()) return false; result.attach(v_shader); result.attach(f_shader); if(!result.link()) return false; for(const auto & location : data.a_locations()) result.attribute_location(location.name, location.index); for(const auto & location : data.u_locations()) result.uniform_location(location.name, location.index); return true; }
void vertex_layout::setup_program(program& program, const std::string& fragcolor_name) noexcept { auto i = uint32_t{0}; for (auto& attribute : vertex_attributes_) { program.bind_attribute_location(attribute.name, i); ++i; } program.bind_frag_data_location(fragcolor_name, 0); }
//----------------------------------------------------------------------------- kernel::kernel( const program& program_ref, const std::string& name ) : generic_kernel( name ), opencl_object< cl_kernel >( program_ref.get_opencl() ) { if( !opencl_.loaded() ) { throw library_exception( CL_INVALID_PROGRAM ); } cl_int error_code; cl_kernel krnl = opencl_.clCreateKernel( program_ref.get_id(), name.c_str(), &error_code ); if( error_code != CL_SUCCESS ) { throw library_exception( error_code ); } set_id( krnl ); }
/// Constructor. Extracts a backend::kernel instance from backend::program. kernel(const command_queue &queue, const program &P, const std::string &name, std::function<size_t(size_t)> smem ) : ctx(queue.context()), P(P), smem(0) { cuda_check( cuModuleGetFunction(&K, P.raw(), name.c_str()) ); config(queue, smem); }
/// Constructor. Extracts a backend::kernel instance from backend::program. kernel(const command_queue &queue, const program &P, const std::string &name, size_t smem_per_thread = 0 ) : ctx(queue.context()), P(P), smem(0) { cuda_check( cuModuleGetFunction(&K, P.raw(), name.c_str()) ); config(queue, [smem_per_thread](size_t wgs){ return wgs * smem_per_thread; }); }
bool program::add_data_file(program& other) { if(num_predicates() < other.num_predicates()) { return false; } for(size_t i(0); i < other.num_predicates(); ++i) { predicate *mine(predicates[i]); predicate *oth(other.get_predicate(i)); if(*mine != *oth) { cerr << "Predicates " << *mine << " and " << *oth << " are different" << endl; return false; } } assert(rules.size() > 0); assert(other.rules.size() > 0); data_rule = other.rules[0]; other.rules.erase(other.rules.begin()); other.number_rules--; vm::rule *init_rule(rules[0]); byte_code init_code(init_rule->get_bytecode()); init_code += init_rule->get_codesize(); init_code -= (RETURN_BASE + NEXT_BASE + RETURN_DERIVED_BASE + MOVE_BASE + ptr_size); assert(fetch(init_code) == MOVE_INSTR); assert(val_is_ptr(move_from(init_code))); assert(val_is_reg(move_to(init_code))); *move_to_ptr(init_code) = VAL_PCOUNTER; *((ptr_val *)(init_code + MOVE_BASE)) = (ptr_val)data_rule->get_bytecode(); //instrs_print(init_rule->get_bytecode(), init_rule->get_codesize(), 0, this, cout); return true; }
virtual void build() { jit_value reg = insn_load(get_param(0)); for(size_t pc = 0; pc < prog.size(); pc++) { const instruction& ins = prog[pc]; if (ins.label != "") { insn_label(label_map[ins.label]); } switch(ins.op) { case op_out: { jit_value x = insn_load_relative(reg, ins.lreg * 2, jit_type_short); jit_value_t xr = x.raw(); insn_call_native("output", (void*)output, output_signature, &xr, 1, 0); } break; case op_in: { jit_value x = insn_call_native("input", (void*)input, input_signature, NULL, 0, 0); insn_store_relative(reg, ins.lreg * 2, x); } break; case op_add: case op_mul: case op_sub: case op_div: case op_load: { jit_value x = insn_load_relative(reg, ins.lreg * 2, jit_type_short); jit_value y; jit_value z; if (ins.rtype == rtype_register) y = insn_load_relative(reg, ins.rreg * 2, jit_type_short); else y = new_constant(ins.rimm); switch(ins.op) { case op_add: z = x + y; break; case op_mul: z = x * y; break; case op_sub: z = x - y; break; case op_div: z = x / y; break; case op_load: z = y; break; default: assert(!"IMPOSSIBLE"); }; z = insn_convert(z, jit_type_short, 0); insn_store_relative(reg, ins.lreg * 2, z); break; } case op_jpos: { jit_value x = insn_load_relative(reg, ins.lreg * 2, jit_type_short); jit_value cmp = x > new_constant((short)0); insn_branch_if(cmp, label_map[ins.jump_label]); break; } case op_invalid: cerr<<"Unsupported opcode\n"; return; }; } insn_return(); }
static void hpxcl_single_initialize( hpx::naming::id_type node_id, size_t vector_size) { // Query all devices on local node std::vector<device> devices = get_devices( node_id, CL_DEVICE_TYPE_GPU, "OpenCL 1.1" ).get(); /* // print devices hpx::cout << "Devices:" << hpx::endl; for(cl_uint i = 0; i < devices.size(); i++) { device cldevice = devices[i]; // Query name std::string device_name = device::device_info_to_string( cldevice.get_device_info(CL_DEVICE_NAME)); std::string device_vendor = device::device_info_to_string( cldevice.get_device_info(CL_DEVICE_VENDOR)); hpx::cout << i << ": " << device_name << " (" << device_vendor << ")" << hpx::endl; } // Lets you choose a device size_t device_num; hpx::cout << "Choose device: " << hpx::endl; std::cin >> device_num; if(device_num < 0 || device_num >= devices.size()) exit(0); // Select a device hpxcl_single_device = devices[device_num]; */ size_t device_id = 0; // print device hpx::cout << "Device:" << hpx::endl; { device cldevice = devices[device_id]; // Query name std::string device_name = device::device_info_to_string( cldevice.get_device_info(CL_DEVICE_NAME)); std::string device_vendor = device::device_info_to_string( cldevice.get_device_info(CL_DEVICE_VENDOR)); hpx::cout << " " << device_name << " (" << device_vendor << ")" << hpx::endl; } // Select a device hpxcl_single_device = devices[device_id]; // Create program hpxcl_single_program = hpxcl_single_device.create_program_with_source( gpu_code); // Build program hpxcl_single_program.build(); // Create kernels hpxcl_single_log_kernel = hpxcl_single_program.create_kernel("logn"); hpxcl_single_exp_kernel = hpxcl_single_program.create_kernel("expn"); hpxcl_single_mul_kernel = hpxcl_single_program.create_kernel("mul"); hpxcl_single_add_kernel = hpxcl_single_program.create_kernel("add"); hpxcl_single_dbl_kernel = hpxcl_single_program.create_kernel("dbl"); // Generate buffers hpxcl_single_buffer_a = hpxcl_single_device.create_buffer( CL_MEM_READ_ONLY, vector_size * sizeof(float)); hpxcl_single_buffer_b = hpxcl_single_device.create_buffer( CL_MEM_READ_ONLY, vector_size * sizeof(float)); hpxcl_single_buffer_c = hpxcl_single_device.create_buffer( CL_MEM_READ_ONLY, vector_size * sizeof(float)); hpxcl_single_buffer_m = hpxcl_single_device.create_buffer( CL_MEM_READ_WRITE, vector_size * sizeof(float)); hpxcl_single_buffer_n = hpxcl_single_device.create_buffer( CL_MEM_READ_WRITE, vector_size * sizeof(float)); hpxcl_single_buffer_o = hpxcl_single_device.create_buffer( CL_MEM_READ_WRITE, vector_size * sizeof(float)); hpxcl_single_buffer_p = hpxcl_single_device.create_buffer( CL_MEM_READ_WRITE, vector_size * sizeof(float)); hpxcl_single_buffer_z = hpxcl_single_device.create_buffer( CL_MEM_WRITE_ONLY, vector_size * sizeof(float)); // Initialize a list of future events for asynchronous set_arg calls std::vector<shared_future<void>> set_arg_futures; // set kernel args for exp set_arg_futures.push_back( hpxcl_single_exp_kernel.set_arg_async(0, hpxcl_single_buffer_m)); set_arg_futures.push_back( hpxcl_single_exp_kernel.set_arg_async(1, hpxcl_single_buffer_b)); // set kernel args for add set_arg_futures.push_back( hpxcl_single_add_kernel.set_arg_async(0, hpxcl_single_buffer_n)); set_arg_futures.push_back( hpxcl_single_add_kernel.set_arg_async(1, hpxcl_single_buffer_a)); set_arg_futures.push_back( hpxcl_single_add_kernel.set_arg_async(2, hpxcl_single_buffer_m)); // set kernel args for dbl set_arg_futures.push_back( hpxcl_single_dbl_kernel.set_arg_async(0, hpxcl_single_buffer_o)); set_arg_futures.push_back( hpxcl_single_dbl_kernel.set_arg_async(1, hpxcl_single_buffer_c)); // set kernel args for mul set_arg_futures.push_back( hpxcl_single_mul_kernel.set_arg_async(0, hpxcl_single_buffer_p)); set_arg_futures.push_back( hpxcl_single_mul_kernel.set_arg_async(1, hpxcl_single_buffer_n)); set_arg_futures.push_back( hpxcl_single_mul_kernel.set_arg_async(2, hpxcl_single_buffer_o)); // set kernel args for log set_arg_futures.push_back( hpxcl_single_log_kernel.set_arg_async(0, hpxcl_single_buffer_z)); set_arg_futures.push_back( hpxcl_single_log_kernel.set_arg_async(1, hpxcl_single_buffer_p)); // wait for function calls to trigger BOOST_FOREACH(shared_future<void> & future, set_arg_futures) { future.wait(); } }
/** * \brief Check if the given program is currently in use. * * This is a pointless function, as I ended up adding the interogative * to \ref gfx::program "program" as a public function anyway. */ inline void uniform::check_program( program const& prgm ) { if ( not prgm.in_use() ) { throw std::logic_error( "Cannot load uniform data into program that is not in use." ); } }
example_mandelbrot(void) : offset_x(-0.5f) , offset_y(0.0f) , scale(1.0f) , aspect(1.0f) { shader vs(GL.vertex_shader); vs.source(glsl_literal( "#version 130\n" "uniform vec2 Offset;\n" "uniform vec2 Scale;\n" "in vec2 Position;\n" "in vec2 Coord;\n" "out vec2 vertCoord;\n" "void main(void)\n" "{\n" " vertCoord = Coord*Scale+Offset;\n" " gl_Position = vec4(Position, 0.0, 1.0);\n" "}\n" )); vs.compile(); shader fs(GL.fragment_shader); fs.source(glsl_literal( "#version 130\n" "uniform sampler1D gradient;\n" "in vec2 vertCoord;\n" "out vec4 fragColor;\n" "void main(void)\n" "{\n" " vec2 z = vec2(0.0, 0.0);\n" " vec2 c = vertCoord;\n" " int i = 0, max = 256;\n" " while((i != max) && (distance(z, c) < 2.0))\n" " {\n" " vec2 zn = vec2(\n" " z.x * z.x - z.y * z.y + c.x,\n" " 2.0 * z.x * z.y + c.y\n" " );\n" " z = zn;\n" " ++i;\n" " }\n" " float a = float(i)/float(max);\n" " fragColor = texture(gradient, a+sqrt(length(c))*0.1);\n" "} \n" )); fs.compile(); prog.attach(vs); prog.attach(fs); prog.link(); gl.use(prog); gl.query_location(offset_loc, prog, "Offset"); gl.query_location(scale_loc, prog, "Scale"); gl.uniform(offset_loc, offset_x, offset_y); gl.bind(vao); GLfloat position_data[4*2] = { -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f }; gl.bind(GL.array_buffer, positions); gl.buffer_data(GL.array_buffer, position_data, GL.static_draw); vertex_attrib_location va_p; gl.query_location(va_p, prog, "Position"); gl.vertex_array_attrib_pointer( va_p, 2, GL.float_, false, 0, nullptr ); gl.enable_vertex_array_attrib(va_p); GLfloat coord_data[4*2] = { -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f }; gl.bind(GL.array_buffer, coords); gl.buffer_data(GL.array_buffer, coord_data, GL.static_draw); vertex_attrib_location va_c; gl.query_location(va_c, prog, "Coord"); gl.vertex_array_attrib_pointer( va_c, 2, GL.float_, false, 0, nullptr ); gl.enable_vertex_array_attrib(va_c); GLfloat gradient_data[8*3]; for(int i=0; i<8*3; ++i) { gradient_data[i] = (std::rand() % 10000) / 10000.f; } gl.bind(GL.texture_1d, gradient); gl.texture_min_filter(GL.texture_1d, GL.linear); gl.texture_mag_filter(GL.texture_1d, GL.linear); gl.texture_wrap( GL.texture_1d, GL.texture_wrap_s, GL.repeat ); gl.texture_image_1d( GL.texture_1d, 0, GL.rgb, 8, 0, GL.rgb, GL.float_, const_memory_block{gradient_data} ); gl.disable(GL.depth_test); }